介绍
ipset和iptables是两个独立的Linux内核组件,但它们可以协同工作,共同实现Linux系统上的网络安全功能。
ipset是iptables的扩展,它允许你创建 匹配整个地址集合的规则。而不像普通的iptables链只能单IP匹配,ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以进行高效的查找;除了一些常用的情况,比如阻止一些危险主机访问本机;从而减少系统资源占用或网络拥塞,IPsets也具备一些新防火墙设计方法,并简化了配置.官网:http://ipset.netfilter.org/
具体来说,ipset是用于管理大规模IP地址集合的内核模块,可用于在Linux系统上创建、修改和删除多个IP地址集。而iptables是Linux上的一种防火墙框架,它允许管理员在网络数据包传输前、中、后的不同阶段对其进行过滤、修改、转发等操作。
ipset和iptables可以互相协作,使得iptables防火墙规则可以更高效地处理大规模的IP地址集合。通常情况下,iptables规则是基于多个IP地址进行拦截和过滤,而当IP地址的数量特别大时,使用iptables单独处理这些地址可能会导致性能问题。为了解决这个问题,管理员可以使用ipset来创建和管理大规模IP地址集合。然后,通过iptables规则,请参考指定这些IP集合,使管理员可以更加高效、简洁的定义规则。
使用ipset与iptables的组合可以实现比基于iptables的规则更高效、灵活的网络安全策略。
ipset安装
| yum安装: yum install ipset 源代码安装:进官网下载ipset-6.30.tar.bz2 yum -y install libmnl-devel libmnl tar -jxvf ipset-6.30.tar.bz2 && cd ipset-6.30 && ./configure --prefix=/usr/local/ipset && make && make install 完成安装
|
ipset使用
1 2 3 4 5 6 7 8 9 10 11 12
| # 显示当前ipset的内容 ipset --list
# 创建节点数据为ipv6,存储类型为hash:net的名为test_whb_v6集合 ipset create test_whb_v6 hash:net family inet6
# 创建节点数据为ipv4,存储类型为基于hash表专门存储ip的test_whb集合 ipset create test_whb hash:ip
# 保存规则到ipset文件 ipset save
|
ipset del使用
1 2 3 4 5
| ipset del删除规则时,必须重启iptables服务才会生效
ipset del jump_mysql 111.206.110.202 重启iptables才能生效
ipset add 添加规则时,不用重启iptables 就会生效
|
ipset flush
清空所有ipset中的条目
iptables规则文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| [root@localhost ~]# cat /etc/sysconfig/iptables #Generated by iptables-save v1.4.7 on Wed Jul 31 10:21:39 2019 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [10988:6938377] -A INPUT -s 118.32.234.103/32 -j DROP -A INPUT -i lo -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m multiport --dports 80,81,82,443 -m state --state NEW -j ACCEPT -A INPUT -s 211.144.68.140/32 -p tcp -m multiport --dports 10050,3306 -j ACCEPT -A INPUT -p tcp -m set --match-set zabbix_server src -m tcp --dport 10050 -j ACCEPT -A INPUT -p tcp -m set --match-set mysql_server src -m tcp --dport 3306 -j ACCEPT -A INPUT -p tcp -m multiport --dports 570,21,1038 -j ACCEPT -A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 5/sec --limit-burst 10 -j ACCEPT -A INPUT -j DROP COMMIT
|
命令行添加iptables规则并保存
1 2 3 4
| iptables -I INPUT -m set --match-set mysql_server src -p tcp -m multiport --dports 10050,3306 -j ACCEPT iptables -I INPUT -m set --match-set rsync_server src -p tcp --dport 873 -j ACCEPT service iptables save /etc/init.d/iptables save
|