linux中ipset命令的使用方法详解
ipset介绍
iptables是在linux内核里配置防火墙规则的用户空间工具,它实际上是netfilter框架的一部分.可能因为iptables是netfilter框架里最常见的部分,所以这个框架通常被称为iptables,iptables是linux从2.4版本引入的防火墙解决方案.
ipset是iptables的扩展,它允许你创建匹配整个地址sets(地址集合)的规则。而不像普通的iptables链是线性的存储和过滤,ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以进行高效的查找.
除了一些常用的情况,比如阻止一些危险主机访问本机,从而减少系统资源占用或网络拥塞,IPsets也具备一些新防火墙设计方法,并简化了配置.
官网:http://ipset.netfilter.org/
安装
rpm-ivhlibmnl-devel-1.0.2-3.el6.x86_64.rpmlibmnl-1.0.2-3.el6.x86_64.rpm tarxvfipset-6.24.tar.bz2 cdipset-6.24 ./configure make makeinstall
#注意:
如果在centos6.6或其他情况下安装时候,configure报错如下
configure:error:Invalidkernelsourcedirectory/lib/modules/2.6.32-358.el6.x86_64/source
解决:需要安装内核源码包kernel-devel-2.6.32-358.el6.x86_64.rpm
创建ipset
ipset-n或者ipsetcreate:
n,createSETNAMETYPENAME[CREATE-OPTIONS]
SETNAME是创建的ipset的名称,TYPENAME是ipset的类型:
TYPENAME:=method:datatype[,datatype[,datatype]]
method指定ipset中的entry存放的方式,随后的datatype约定了每个entry的格式。
可以使用的method:
bitmap,hash,list
可以使用的datatype:
ip,net,mac,port,iface
添加记录
ipsetadd用于在ipset中添加记录:
addSETNAMEADD-ENTRY[ADD-OPTIONS]
向ipset中添加entry的时候,加入的entry的格式必须与创建ipset是指定的格式匹配。
$ipsetcreatfoohash:ip,port,ip $ipsetaddfooipaddr,portnum,ipaddr $ipsetlistfoo Name:foo Type:hash:ip,port,ip Revision:2 Header:familyinethashsize1024maxelem65536 Sizeinmemory:16584 References:0 Members: 192.168.1.2,tcp:80,192.168.1.3
删除记录
ipsetdel用于从ipset中删除记录:
delSETNAMEDEL-ENTRY[DEL-OPTIONS]
查询记录
ipsettest可以检查目标entry是否在ipset中:
testSETNAMETEST-ENTRY[TEST-OPTIONS]
ipsetlist可以查看ipset的所有内容:
list[SETNAME][OPTIONS]
导出导入
ipsetsave可以导出所有的ipset:
save[SETNAME]
ipsetrestore则用于将导出的内容导入。
其它
flush[SETNAME] Flushallentriesfromthespecifiedsetorflushallsetsifnoneisgiven. e,renameSETNAME-FROMSETNAME-TO Renameaset.SetidentifiedbySETNAME-TOmustnotexist. w,swapSETNAME-FROMSETNAME-TO Swapthecontentoftwosets,orinanotherwords,exchangethenameoftwosets.Thereferredsetsmustexistandidenticaltypeofsetscanbeswappedonly. help[TYPENAME] PrinthelpandsettypespecifichelpifTYPENAMEisspecified. version Printprogramversion. -Ifadashisspecifiedascommand,thenipsetentersasimpleinteractivemodeandthecommandsarereadfromthestandardinput.Theinteractivemodecanbefinishedbyenteringthe pseudo-commandquit.
在iptables中使用ipset
在iptables中可以使用-mset启用ipset模块,例如。
-APOSTROUTING-mset--match-setfelix-masq-ipam-poolssrc-mset!--match-setfelix-all-ipam-poolsdst-jMASQUERADE
iptables的set模块:
set ThismodulematchesIPsetswhichcanbedefinedbyipset(8). [!]--match-setsetnameflag[,flag]... whereflagsarethecommaseparatedlistofsrcand/ordstspecificationsandtherecanbenomorethansixofthem.Hencethecommand iptables-AFORWARD-mset--match-settestsrc,dst ...
在TARGET中也可以操作ipset:
SET Thismoduleaddsand/ordeletesentriesfromIPsetswhichcanbedefinedbyipset(8). --add-setsetnameflag[,flag...] addtheaddress(es)/port(s)ofthepackettotheset --del-setsetnameflag[,flag...] deletetheaddress(es)/port(s)ofthepacketfromtheset whereflag(s)aresrcand/ordstspecificationsandtherecanbenomorethansixofthem. ...
在maniptables-extensions中可以找到setmodule和SETTARGET的所有选项。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。