iptables之ipset集群工具

ipset介绍 
ipset是iptables的扩展,它允许你创建 匹配整个地址集合的规则。而不像普通的iptables链只能单IP匹配, ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以进行高效的查找,除了一些常用的情况,比如阻止一些危险主机访问本机,从而减少系统资源占用或网络拥塞,IPsets也具备一些新防火墙设计方法,并简化了配置。

创建ipset:ipset -n或者ipset create

ipset -n,create SETNAME TYPENAME

SETNAME是创建的ipset的名称,TYPENAME是ipset的类型

TYPENAME := method:datatype[,datatype[,datatype]]

method指定ipset中的entry存放的方式,随后的datatype约定了每个entry的格式

可以使用的method:bitmap, hash, list

可以使用的datatype:ip, net, mac, port, iface

综上  ipset create 自定义名字 ipset类型

添加记录

ipset add用于在ipset中添加记录:add SETNAME ADD-ENTRY [ ADD-OPTIONS ]

向ipset中添加entry的时候,加入的entry的格式必须与创建ipset是指定的格式匹配

$ipset creat foo hash:ip,port,ip
$ipset add foo ipaddr,portnum,ipaddr
 
$ipset list foo
Name: foo
Type: hash:ip,port,ip
Revision: 2
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 16584
References: 0
Members:
192.168.1.2,tcp:80,192.168.1.3

删除记录

ipset del用于从ipset中删除记录del SETNAME DEL-ENTRY [ DEL-OPTIONS ]

查询记录

ipset test可以检查目标entry是否在ipset中:test SETNAME TEST-ENTRY [ TEST-OPTIONS ]

ipset list可以查看ipset的所有内容:list [ SETNAME ] [ OPTIONS ]

导出导入

ipset save可以导出所有的ipset:save [ SETNAME ]

在iptables中使用ipset

在iptables中可以使用-m set启用ipset模块:-A INPUT -p tcp -m set --match-set cdn src  -j ACCEPT

以下内容是安装的简要步骤

# ipset 安装
yum install ipset -y
#创建配置文件
touch /etc/sysconfig/ipset
#启动服务
service ipset start
# ipset创建set
ipset create cdn hash:net
# ipset给set添加ip
ipset add cdn 172.16.0.0/16

#保存
service ipset save
service ipset restart

# 查看ipset规则
ipset list
ipset list cdn
# 增加iptables规则
-A INPUT -p tcp -m set --match-set cdn src (--destination-port 443) -j ACCEPT
()里面的内容可以删除.
# 重启iptables service iptables restart 博客内容: http:
//bigsec.net/one/tool/ipset.html http://ipset.netfilter.org/ https://fixatom.com/block-ip-with-ipset/

猜你喜欢

转载自www.cnblogs.com/fengzhongzhuzu/p/9284464.html