5.12. 使用 firewalld设置和控制 IP 集
要查看
firewalld 所支持的 IP 集设置类型列表,请以 root 用户身份输入以下命令。
firewall-cmd --get-ipset-types
~]# 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
5.12.1. 使用命令行客户端配置 IP 设置选项 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
IP 集可以在
firewalld 区中用作源,也可以用作富规则中的源。在 Red Hat Enterprise Linux 7 中,首选的方法是使用在直接规则中使用 firewalld 创建的 IP 集。
要列出 permanent 环境中
firewalld 已知的 IP 集,请以 root 用户身份运行以下命令:
firewall-cmd --permanent --get-ipsets
~]# firewall-cmd --permanent --get-ipsets
要添加新的 IP 集,请以
root 用户身份使用 permanent 环境来运行以下命令:
firewall-cmd --permanent --new-ipset=test --type=hash:net
~]# firewall-cmd --permanent --new-ipset=test --type=hash:net
success
上面的命令为
IPv4 创建了一个名为 test 和 hash:net 类型的新 IP 集。要创建用于 IPv6 的 IP 集,请添加 --option=family=inet6 选项。要使新设置在运行时环境中有效,请重新加载 firewalld。以 root 身份使用以下命令列出新 IP 集:
firewall-cmd --permanent --get-ipsets
~]# firewall-cmd --permanent --get-ipsets
test
要获得有关 IP 集的更多信息,请以
root 身份运行以下命令:
firewall-cmd --permanent --info-ipset=test
~]# firewall-cmd --permanent --info-ipset=test
test
type: hash:net
options:
entries:
请注意,IP 集目前没有任何条目。要向 test IP 集中添加一个条目,请以
root 身份运行以下命令:
firewall-cmd --permanent --ipset=test --add-entry=192.168.0.1
~]# firewall-cmd --permanent --ipset=test --add-entry=192.168.0.1
success
前面的命令将 IP 地址 192.168.0.1 添加到 IP 集合中。要获取 IP 集合中当前条目的列表,请以
root 身份运行以下命令:
firewall-cmd --permanent --ipset=test --get-entries
~]# firewall-cmd --permanent --ipset=test --get-entries
192.168.0.1
生成包含 IP 地址列表的文件,例如:
包含 IP 集合 IP 地址列表的文件应该每行包含一个条目。以 hash、分号或空行开头的行将被忽略。
要从 iplist.txt 文件中添加地址,请以
root 身份运行以下命令:
firewall-cmd --permanent --ipset=test --add-entries-from-file=iplist.txt
~]# firewall-cmd --permanent --ipset=test --add-entries-from-file=iplist.txt
success
要查看 IP 集合的扩展条目列表,请以
root 身份运行以下命令:
要从 IP 集合中删除地址并检查更新的条目列表,请以
root 身份运行以下命令:
firewall-cmd --permanent --ipset=test --remove-entries-from-file=iplist.txt firewall-cmd --permanent --ipset=test --get-entries
~]# firewall-cmd --permanent --ipset=test --remove-entries-from-file=iplist.txt
success
~]# firewall-cmd --permanent --ipset=test --get-entries
192.168.0.1
您可以将 IP 集合作为一个源添加到区,以便处理所有来自 IP 集合中列出的任意地址的网络流量。例如,要将 test IP 集作为源添加到 drop 区来丢弃来自 test IP 集中列出的所有条目的所有数据包,请以
root 身份运行以下命令:
firewall-cmd --permanent --zone=drop --add-source=ipset:test
~]# firewall-cmd --permanent --zone=drop --add-source=ipset:test
success
源中的
ipset: 前缀向 firewalld 表示,源是一个 IP 集,而不是一个 IP 地址或一个地址范围。
创建和删除 IP 集合仅限于永久环境,其它 IP 设置选项也可以用于运行时环境中,而无需
--permanent 选项。
5.12.2. 为 IP 集配置自定义服务 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
将自定义服务配置为在
firewalld 启动前创建和加载 IP 设置结构:
- 以
root用户身份运行的编辑器,按如下所示创建一个文件:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 在 firewalld 中永久使用设置的 IP:
vi /etc/firewalld/direct.xml
~]# vi /etc/firewalld/direct.xml <?xml version="1.0" encoding="utf-8"?> <direct> <rule ipv="ipv4" table="filter" chain="INPUT" priority="0">-m set --match-set <replaceable>ipset_name</replaceable> src -j DROP</rule> </direct>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 需要
firewalld重新加载才能激活更改:这会重新加载防火墙,而不丢失状态信息(TCP 会话不会终止),但在重新加载期间可能会中断服务。firewall-cmd --reload
~]# firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow
警告
红帽不推荐使用不是通过
firewalld 管理的 IP 集。要使用这样的 IP 组,需要一个永久直接规则来引用集合,且必须添加自定义服务来创建这些 IP 组件。这个服务需要在 firewalld 启动前启动,否则 firewalld 无法使用这些集合添加直接规则。您可以使用 /etc/firewalld/direct.xml 文件来添加永久的直接规则。