当前位置:首页 > 操作系统 > 正文内容

Linux中firewalld中引用 IPset 并配置IPset

李云召6个月前 (11-06)操作系统369

Linux中firewalld中引用 IPset 并配置IPset

只是为了记录下自己的操作,方便后续用起来方便,也找了几个方法,最后发现这个可行:

ipset是ip地址的集合,firewalld使用ipset可以在一条规则中处理多个ip地址,后续对ip地址集合的管理也更方便,所以就采用了这方法来禁用,另外需要注意的是:iptables所用的ipset命令的不同,不要混合使用firewall-cmd的ipset参数与linux平台上的ipset命令,避免引起冲突,irewalld的ipset会记录到/etc/firewalld/ipsets/目录下

系统环境:Linux 8(Centos)

禁用的IP地址192.168.0.10

一、firewalld中ipset的操作例子:

1.新建一个set

#–new-ipset=blacklist: 指定新ipset的名字为:blacklist

#–type=hash:ip    指定类型为 hash:ip,这种形式不允许重复而且只有一个ip

[root@Centos ipsets]# firewall-cmd --permanent --new-ipset=blacklist --type=hash:ip
success

查看ipset文件是否已生成:

说明:默认的目录是:/etc/firewalld/ipsets

[root@Centos ipsets]# more /etc/firewalld/ipsets/blacklist.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

2.在set中添加ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=blacklist --add-entry=192.168.0.10
success

查看添加ip的效果

[root@Centos ipsets]# more /etc/firewalld/ipsets/blacklist.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
  <entry>192.168.0.10</entry>
</ipset>

3.从set中删除ip

[root@Centos ipsets]# firewall-cmd --permanent --ipset=blacklist --remove-entry=192.168.0.10
success

查看删除ip的效果

[root@Centos ipsets]# more /etc/firewalld/ipsets/blacklist.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

4.删除一个set

[root@Centos ipsets]# firewall-cmd --permanent --delete-ipset=blacklist
success

查看blacklist这个set的配置文件是否还存在?

[root@Centos ipsets]# more /etc/firewalld/ipsets/blacklist.xml
more: stat of /etc/firewalld/ipsets/blacklist.xml failed: No such file or directory

5.打印一个set的文件路径:

[root@Centos ipsets]# firewall-cmd --permanent --path-ipset=blacklist
/etc/firewalld/ipsets/blacklist.xml

6.打印一个set的内容:

[root@Centos ipsets]# firewall-cmd --permanent --info-ipset=blacklist
blacklist
  type: hash:ip
  options:
  entries: 192.168.0.10

7.列出一个set下的所有entry

[root@Centos ipsets]# firewall-cmd --permanent --ipset=blacklist --get-entries
192.168.0.10

8.判断一个ip是否存在于set中?

[root@Centos ipsets]# firewall-cmd --permanent --ipset=blacklist --query-entry=1.1.1.1
no
[root@Centos ipsets]# firewall-cmd --permanent --ipset=blacklist --query-entry=192.168.0.10
yes

9.列出所有的ipsets

[root@Centos ipsets]# firewall-cmd --permanent --get-ipsets
blacklist

10.得到所有的默认ipset类型 

[root@Centos ipsets]# firewall-cmd --get-ipset-types
hash:ip hash:ip,mark hash:ip,port hash:ip,port,ip hash:ip,port,net hash:mac hash:net hash:net,iface hash:net,net hash:net,port hash:net,port,net

二、firewalld中使用ipset

1.把一个ipset加入到禁止的规则

[root@Centos ipsets]# firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="blacklist" drop'
success

使生效

[root@Centos ipsets]# firewall-cmd --reload
success

查看xml中的记录:

[root@Centos ipsets]# more /etc/firewalld/zones/public.xml
...
  <rule family="ipv4">
    <source ipset="blacklist"/>
    <drop/>
  </rule>
...

2.把ip地址中ipset中删除

注意:没写入到磁盘

[root@Centos ipsets]# firewall-cmd --ipset=blacklist --remove-entry=192.168.0.10
success
[root@Centos ipsets]# firewall-cmd --ipset=blacklist --query-entry=192.168.0.10
no
[root@Centos ipsets]# firewall-cmd --ipset=blacklist --get-entries

可见已删除成功,

如果想永久性的记录下来:写入到磁盘后 reload一次

[root@Centos ipsets]# firewall-cmd --permanent --ipset=blacklist --remove-entry=192.168.0.10
success
[root@Centos ipsets]# more /etc/firewalld/ipsets/blacklist.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>
[root@Centos ipsets]# firewall-cmd --reload
success


原文: https://blog.imgtouch.com/index.php/2023/05/22/centos8linuxfirewalld-yong-ipset-guan-li-ip-di-zhi-de-ji-he/



声明:本文由云召博客收集发布,如有侵权,请联系我们,我们将第一时间进行处理。

本文链接:https://liyunzhao.com/post/132.html

标签: LinuxIPset
分享给朋友:

“Linux中firewalld中引用 IPset 并配置IPset” 的相关文章

性能测试知多少--系统计数器与硬件分析

性能测试知多少--系统计数器与硬件分析

性能计数器(Performance Counter),也叫性能监视器。一个人健康状况如何,我们通过对其做各项体检获得相关的状况指标,如血压、心跳,肺活量等。那么在做性能测试过程中,整个系统的软硬件进行监控也必不可少,监控所获得的数据也是我们分析系统性能的主要依据。在整个系统中,对于不同的软件和硬件,...

对硬盘进行分区时,GPT和MBR有什么区别?

对硬盘进行分区时,GPT和MBR有什么区别?

在Windows 8或8.1中设置新磁盘时,系统会询问你是想要使用MBR还是GPT分区。GPT是一种新的标准,并在逐渐取代MBR。GPT带来了很多新特性,但MBR仍然拥有最好的兼容性。GPT并不是Windows专用的新标准—— Mac OS X,Linux,及其他操作系统同样使用GPT。在使用新磁盘...

Windows server 2008 R2 更新补丁失败进入恢复模式

Windows server 2008 R2 更新补丁失败进入恢复模式

故障现象:由于安全需要,需要再现网的服务器上安装系统补丁,Windows 2008 R2,但是升级补丁后,重启进入了恢复页面,正常进入系统,多次重启仍然无法正常进入系统。解决方法:经过确认,仅仅是修补了相关补丁,导致此问题发生,那么我们按照如下操作进行修复下:1、可使用USB WinPE系统引导,也...

Windows 7 访问部分网站提示非安全链接问题解决

Windows 7 访问部分网站提示非安全链接问题解决

Windows 7 操作系统是一款大家非常熟悉从操作系统,虽然官方已经在2020年1月14日全面停止对Windows 7的支持,然而,直到现在还有很多人在使用,有些程序的兼容性在Windows 7上也非常的优秀,不过在重新安装windows 7后不知大家是否碰到过类似的问题,访问淘宝、京东,甚至百度...

Mac远程Win桌面工具【Microsoft Remote Desktop for mac】

Mac远程Win桌面工具【Microsoft Remote Desktop for mac】

微软官方很早就专为Mac用户提供了一款类Windows mstsc的远程桌面工具——Microsoft Remote Desktop for mac,专门用于局域网内,远程控制Windows桌面。但是。苹果Appstore中国区确是长期屏蔽该软件。长久以来,大部分使用Microsoft Remote...

统信UOS桌面操作系统【汇总目录】

统信UOS桌面操作系统【汇总目录】

随着各类的国产化,桌面操作系统也开始逐渐国产化,使用习惯了Windows,突然转变,还是很有必要去适应下,也从网上整理了下材料(正月十六工作室),以免自己用的时候不好找,后续可能会根据需求持续更新。信创操作系统–统信UOS桌面版(系统安装:分别在虚拟机、物理机和Win10上安装,驱动安装)保姆级教程...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。