ipset 自学笔记

类似于一种面向对象语言的中的类。ipset 定义一种网络类,类可以包含若干具体的地址。

在调用的时候只需使用类名,而不要使用具体的地址。

安装 yum -y install ipset

说明 man ipset

格式 ipset [ OPTIONS ] COMMAND [ COMMAND-OPTIONS ]

COMMANDS := { create | add | del | test | destroy | list | save | restore | flush | rename | swap | help | version | - }

OPTIONS := { -exist | -output { plain | save | xml } | -quiet | -resolve | -sorted | -name | -terse | -file filename }

常用命令

ipset create name type 创建一个类

type: hash:[type1,type2] bitmap:[type1,type2] list:[type1,type2]

tyep :ip, net, mac, port, iface

Example: ipset create name hash:ip,net

bitmap和list: 使用固定大小的存储.

hash: 使用hash表来存储元素。但为了避免Hash表键冲突,在ipset会在hash表key用完后,若又有新增条目,则ipset将自动对hash表扩大,假如当前哈希表大小为100条,则它将扩展为200条。当在iptables/ip6tables中使用了ipset hash类型的集合,则该集合将不能再新增条目。

hash的自增

前面说过:bitmap link 的储存方式的集合大小是固定,hash类型的储存大小是可变的

下面回顾一下hash的两个参数

hashsize:指定了创建集合时初始大小

maxelem:指定了集合最大存储记录的数量

Examples:

ipset create foo hash:ip,mac

ipset add foo 1.1.1.1,01:02:03:04:05:06

ipset test foo 1.1.1.1,01:02:03:04:05:06

定义的类 hash:ip,mac 和add 的元素1.1.1.1,01:02:03:04:05:06 要对应

ipset add name ADD-ENTRY [ ADD-OPTIONS ]

向创建的类中添加元素。注意添加的元素要符定义这个类的元素类型。

Example:

ipset create test hash:net

ipset add test 192.168.1.1

ipset add test 192.168.2.0/24

查询 ipset list test

[root@localhost sbin]# ipset list test

Name: test

Type: hash:net

Revision: 6

Header: family inet hashsize 1024 maxelem 65536

Size in memory: 632

References: 0

Number of entries: 2

Members:

192.168.1.1

192.168.2.0/24

ipset destroy test 删除表

ipset del test 192.168.1.1 删除表中元素

ipset flush [test] 清空该类中的所有元素,但不删除表,如果不指定具体的类 ipset flush 则清空所有类中的所有元素

添加选项[ COMMAND-OPTIONS ]

timeout 超时时间

timeout设置超时时间(单位为秒),如果设置为0,表示永久生效,超时时间可以通过 -exist来进行修改

Example: ipset create test hash:ip timeout 0


v2-e70adc199d241c6b2c38b95568accc6e_b.jpg

counters, packets, bytes (所有集合适用)

如果指定了该选项,则使用每个元素支持的包和字节计数器创建集合。当元素(重新)添加到集合中时,除非包和字节选项显式指定包和字节计数器值,否则包和字节计数器将初始化为零。

Example:

ipset create foo hash:ip counters

ipset add foo 192.168.1.1 packets 42 bytes 1024


comment 备注

Example:

ipset add foo 1.1.1.1 comment "this comment is \"bad\""


skbinfo, skbmark, skbprio, skbqueue (所有集合适用)

这个扩展允许您存储每个条目的metainfo(防火墙标记、tc类和硬件队列),并使用SET netfilter target和——map- SET选项将其映射到包。skbmark选项格式:MARK或MARK/MASK,其中MARK和MASK为32位十六进制数字,前缀为0x。如果只指定标记,则使用掩码0xffffffff。skbprio选项有tc类格式:MAJOR:MINOR,其中MAJOR和MINOR号是十六进制,没有0x前缀。skbqueue选项只是一个小数。

Example:

ipset create foo hash:ip skbinfo

ipset add foo skbmark 0x1111/0xff00ffff skbprio 1:10 skbqueue 10


hashsize 集合的初始哈希大小

它定义了集合的初始哈希大小,默认值为1024。哈希大小必须是2的幂,内核会自动舍入两个哈希大小的非幂到第一个正确的值

Example: ipset create test hash:ip hashsize 512


family { inet | inet6 } IPv4/IPv6 (适用hash集合(hash:mac除外))

ipaddr := { ip | fromaddr-toaddr | ip/cidr }

netaddr := { fromaddr-toaddr | ip/cidr }

Example: ipset create test hash:ip family inet6


nomatch (hash:net适用 ):

可以存储网络数据类型的哈希集类型(即hash:net)在添加条目时支持可选的nomatch选项。当匹配集合中的元素时,将跳过标记为nomatch的条目,就好像这些条目没有添加到集合中一样,这使得在异常情况下构建集合成为可能。参见下面的hash类型hash:net中的示例。当ipset测试元素时,会考虑nomatch标志。如果想要测试集合中使用nomatch标记的元素是否存在,那么也必须指定该标志。也就是说,这个通常与hash:net搭配使用,用来跳过 hash:net指定的ip netmask address.

forceadd 集合满时,随机删除(所有集合适用)

Example: ipset create foo hash:ip forceadd


======================================================================

在iptables中使用ipset,只要加上-m set --match-set即可。

iptables -I INPUT -s 192.168.100.36 -m set --match-set bbb dst -j DROP

iptables -I INPUT -m set --match-set aaa src -d 192.168.100.36 -j DROP

iptables -I INPUT -m set --match-set aaa src -m set --match-set bbb dst -j DROP


======================================================================

参考:blog.csdn.net/gymaisyl/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

期待未来的男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值