【firewalld】CentOS7下firewall的ipset配置使用详解

本文详细介绍了ipset与iptables的关系,ipset在处理大量IP地址和端口时的高性能优势,以及如何在CentOS7上使用firewall-cmd进行ipset的创建、修改和删除等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、ipset概述


ipset与iptable

· iptables是在Linux内核中配置防火墙规则的用户空间工具。在内核版本更新到2.4以来,iptable一直作为系统中主要的防火墙解决方案。CentOS7将原来的iptable替换为firewall,而firewall提供了对ipset的支持。
· ipset相当于iptable的扩展,它和iptable 处理方式,iptable通过链表线性存储然后遍历来实现匹配。而ipset通过通过索引的数据结构设计达到快速匹配。这种设计使得当set配置比较庞大的时候,也可以高效地进行匹配。

适用范围

· 存储多个IP地址或端口号,并在一个swoop中与iptables的集合相匹配;
· 在不影响性能的同时,针对IP地址或端口的变化动态更新iptables规则;
· 性能高,用一个iptables规则来表示复杂的IP地址和端口

安装
在CentOS7更新firewalld到最新版本,即可使用ipset,更新安装命令:
yum install firewalld

二、ipset使用


利用firewall-cmd对ipset进行操作

ipset的存储路径:/etc/firewalld/ipsets

命令集合:

获取指定ipset信息

firewall-cmd --info-ipset=[ipset_name]

增加ipset

firewall-cmd --permanent --new-ipset=[ipset_name] --type=[type] --option

其中option可不填,eg:–option=family=inet6指定该ipset使用IPV6地址。

删除指定ipset

firewall-cmd --permanent --delete-ipset=[ipset_name]

删除后,在ipset目录下会有一个备份文件,例如:原来的ipset为test_set.xml,则删除后会生成一个test_set.xml.old.

指定ipset中增加entry

firewall-cmd --permanent --ipset=[ipset_name] --add-entry=[xx.xx.xx.xx]

指定ipset中删除entry

firewall-cmd --permanent --ipset=[ipset_name] --remove-entry=[xx.xx.xx.xx]

删除entry
–permanent参数直接决定是否永久生效 需要执行firewall-cmd –reload,使其生效
不加–permanent是直接生效(runtime environment),不会保存在相应的ipset的XML文件中,重启firewalld服务将会失效

IPSet Options
–get-ipset-types Print the supported ipset types
–new-ipset=<ipset> –type=<ipset type> [–option=<key>[=<value>]]..
Add a new ipset [P only]
–new-ipset-from-file=<filename> [–name=<ipset>]
Add a new ipset from file with optional name [P only]
–delete-ipset=<ipset>
Delete an existing ipset [P only]
–load-ipset-defaults=<ipset>
Load ipset default settings [P only]
–info-ipset=<ipset> Print information about an ipset
–path-ipset=<ipset> Print file path of an ipset [P only]
–get-ipsets Print predefined ipsets
–ipset=<ipset> –set-description=<description>
Set new description to ipset [P only]
–ipset=<ipset> –get-description
Print description for ipset [P only]
–ipset=<ipset> –set-short=<description>
Set new short description to ipset [P only]
–ipset=<ipset> –get-short
Print short description for ipset [P only]
–ipset=<ipset> –add-entry=<entry>
Add a new entry to an ipset [P]
–ipset=<ipset> –remove-entry=<entry>
Remove an entry from an ipset [P]
–ipset=<ipset> –query-entry=<entry>
Return whether ipset has an entry [P]
–ipset=<ipset> –get-entries
List entries of an ipset [P]
–ipset=<ipset> –add-entries-from-file=<entry>
Add a new entries to an ipset [P]
–ipset=<ipset> –remove-entries-from-file=<entry>
Remove entries from an ipset [P]

三、ipset类型的区别

1、hash:ip

2、 hash:ip,mark

3、hash:ip,port

4、hash:ip,port,ip

5、hash:ip,port,net

6、 hash:mac

7、hash:net

8、hash:net,iface

9、hash:net,net

10、hash:net,port

11、hash:net,port,net

参考文献

1、https://www.linuxjournal.com/content/advanced-firewall-configurations-ipset
2、https://firewalld.org/2015/12/ipset-support
3、https://firewalld.org/documentation/man-pages/firewalld.ipset

### 如何在 CentOS 7配置使用 firewalld 防火墙 #### 加载和重启命令差异 `firewall-cmd --reload` 和 `systemctl restart firewalld` 的主要区别在于处理现有连接的方式。前者会重新加载当前的防火墙设置而不中断现有的网络连接,而后者则完全停止并启动服务,这可能会短暂影响正在进行中的通信[^1]。 #### 使用 zone 进行流量管理 Firewalld 提供了一种灵活的方法来管理和分类入站流量——即通过定义不同的区域(zone),可以依据源 IP 地址或网络接口将流量分配给特定的安全级别。每个区域可以根据预设的信任程度自动决定允许哪些类型的流量进入系统[^2]。 #### 基于服务名称而非端口号的操作简化 为了提高易用性和减少错误率,firewalld 支持直接利用已知的服务名称来进行规则设定,而不是手动输入具体的端口与协议组合。这种方式不仅使得配置更加直观简单,同时也避免了因误配而导致的安全风险。 #### 实际操作指南 下面是一些基本指令用于日常维护: - **查看活动状态** ```bash systemctl status firewalld ``` - **启用并开启随开机自启** ```bash systemctl enable firewalld && systemctl start firewalld ``` - **查询默认区域及其策略** ```bash firewall-cmd --get-default-zone firewall-cmd --list-all-zones | less ``` - **更改默认区域** ```bash firewall-cmd --set-default-zone=public ``` - **添加新规则(例如开放 SSH 访问)** ```bash firewall-cmd --reload ``` 这些基础命令可以帮助管理员快速上手,在实际环境中根据需求调整相应的安全政策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值