Linux常用命令-ipset

命令

ipset

描述

administration tool for IP sets
管理IP地址端口集合

用法

1
2
3
ipset [ OPTIONS ] COMMAND [ COMMAND-OPTIONS ]
OPTIONS := { -exist | -output { plain | save | xml } | -quiet | -resolve | -sorted | -name | -terse | -file filename }
COMMANDS := { create | add | del | test | destroy | list | save | restore | flush | rename | swap | help | version | - }

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Options:
-!, -exist 忽略错误提示,如创建了相同的集合名称,添加了重复IP等
-o, -output { plain | save | xml } 指定集合显示格式,list命令默认为plain格式显示,save命令默认为save格式
-q, -quiet 静默模式
-r, -resolve 反向解析IP地址为主机名
-s, -sorted 自动排序显示
-n, -name 只显示集合名称
-t, -terse 显示集合名称和头部信息,不显示IP成员列表
-f, -file filename 指定文件名称,使用list或save命令时为保存到文件,使用restore命令时为读取文件

Commands:
create SETNAME TYPENAME [ CREATE-OPTIONS ] 创建集合,create可简写为n
add SETNAME ENTRY [ ADD-OPTIONS ] 添加条目
del SETNAME ENTRY 删除条目
test SETNAME ENTRY 测试条目是否存在
destroy [SETNAME] 删除指定集合或全部集合,可简写为x
list [SETNAME] 显示指定集合或全部集合信息
save [SETNAME] 保存指定集合或全部集合
restore 恢复已保存的集合
flush [SETNAME] 清空指定集合或全部集合
rename FROM-SETNAME TO-SETNAME 重命名集合,可简写为e
swap FROM-SETNAME TO-SETNAME 互换两个集合内容
quit 退出交互模式
- 进入交互模式

Supported set types:
list:set 3 skbinfo support
list:set 2 comment support
hash:mac 0 Initial revision
hash:ip,mac 0 Initial revision
hash:net,iface 6 skbinfo support
hash:net,port 7 skbinfo support
hash:net,port 6 forceadd support
...

Set type syntax:
TYPENAME := method:datatype[,datatype[,datatype]]
method: bitmap, hash, and list
datatype: ip, net, mac, port and iface

create and add command options:
timeout 此参数对所有类型集有效,指定超时参数,超时后自动删除条目,create命令的timeout参数值表示新条目的默认超时值,add命令可以指定非默认超时值
如果timeout参数值为0表示永久有效,可以使用-exist选项重新添加元素并修改超时值,最大超时值为2147483,单位秒
ipset create myset hash:ip timeout 300
ipset add myset 192.168.10.1 timeout 60
ipset -exist add myset 192.168.10.1 timeout 600

comment 此参数对所有类型集有效,添加注释,注释用单引号或双引号括起来,注释内容中不能包含相同的引号,因为使用\无法转义
ipset create myset hash:ip comment
ipset add myset 192.168.10.1 comment "a b"
ipset add myset 192.168.10.1 comment "a b\"c" # 错误注释

counters, packets, bytes 此参数对所有类型集有效,指定数据包和字节数计数器
ipset create myset hash:ip counters
ipset add myset 192.168.10.1 packets 42 bytes 1024

hashsize 此参数对所有哈希类型集的create命令有效,定义了集合的初始哈希大小,默认是1024
ipset create myset hash:ip hashsize 4096

maxelem 此参数对所有哈希类型集的create命令有效,定义了可以存储在集合中的最大元素数,默认为65536
ipset create myset hash:ip maxelem 2048

family { inet | inet6 }
此参数对除了hash:mac之外的所有哈希类型集的create命令有效,定义了要存储在集合中的IP地址的协议族,默认值为inet,即IPv4
对于inet,可以通过在条目的IP地址部分指定IPv4地址的范围或网络来添加或删除多个条目
ipset create myset hash:ip family inet6

nomatch 可以存储网络类型数据的哈希集类型(即hash:*net*)在添加条目时支持可选的nomatch选项,无需在create时指定nomatch选项
当匹配集合中的元素时,标记为nomatch的条目会被跳过,可用于排除匹配网段中的指定IP或子网
ipset create myset hash:net
ipset add myset 192.168.10.0/24
ipset add myset 192.168.10.0/30 nomatch

forceadd 此参数对所有哈希类型集的create命令有效,当使用此选项创建的集合已满时,可以强制追加到集合,但会从集合中随机删除一个条目
ipset create myset hash:ip forceadd

add命令选项依赖create命令选项,即必须在create时添加指定选项,然后才能在add时添加对应的选项,add选项为可选非必须
ipset create myset hash:ip comment
ipset add myset 192.168.10.1
ipset add myset 192.168.10.2 comment "myip"
使用add,del,myset命令时指定的条目参数必须和集合中的条目格式保持一致,如下需要指定IP和端口
ipset create myset hash:ip,port
ipset add myset 192.168.10.1,80
如果指定的主机名中含有连字符-,则必须用中括号表示,如[sftp-server],[ftp-host]
默认指定的端口为TCP,如需指定UDP,则格式为udp:port,如udp:53
如果指定的域名解析后有多个IP地址,则只使用第一个IP地址,注意iptables添加主机名时会添加解析到的所有IP地址

注意

ipset用于批量处理ip,port或net,然后使用iptables调用ipset的集合实现动态管理规则

参考
https://ipset.netfilter.org/ipset.man.html
https://www.cnblogs.com/wn1m/p/10919940.html

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
$ ipset --version
ipset v7.1, protocol version: 7

# 创建IP集合
$ ipset create myset hash:ip
$ ipset list
Name: myset
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 120
References: 0
Number of entries: 0
Members:
# 添加IP列表
$ ipset add myset 192.168.10.1
$ ipset add myset 192.168.10.2
# 添加域名时,只添加解析后的第一个IP地址,如果DNS策略为轮询,则可以多次添加以获取所有IP
$ ipset add myset www.a.com
Warning: www.a.com resolves to multiple addresses: using only the first one returned by the resolver.
# create时未指定comment参数,所以add时也无法添加
$ ipset add myset 192.168.20.10 comment "a"
ipset v7.1: Comment cannot be used: set was created without comment support
# 查看IP集合
$ ipset list myset
Name: myset
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 216
References: 0
Number of entries: 2
Members:
192.168.10.1
10.2.1.30
192.168.10.2

# 创建IP端口集合,添加comment参数
$ ipset create black_list hash:ip,port comment
$ ipset add black_list 192.168.10.1
ipset v7.1: Syntax error: Second element is missing from 192.168.10.1.
$ ipset add black_list 192.168.10.1,80
$ ipset add black_list 192.168.20.2,443
# 测试指定条目是否存在
$ ipset test black_list 192.168.10.1,80
Warning: 192.168.10.1,tcp:80 is in set black_list.
$ ipset test black_list 192.168.10.1,443
192.168.10.1,tcp:443 is NOT in set black_list.
# 指定UDP端口
$ ipset add black_list 192.168.30.1,udp:53 comment "dns"
# 指定端口范围,会自动展开端口范围并依次添加
$ ipset add black_list 192.168.10.5,8080-8090
$ ipset list black_list
Name: black_list
Type: hash:ip,port
Revision: 5
Header: family inet hashsize 1024 maxelem 65536 comment
Size in memory: 3596
References: 0
Number of entries: 14
Members:
192.168.10.5,tcp:8088
192.168.10.5,tcp:8080
192.168.10.5,tcp:8082
192.168.10.5,tcp:8085
192.168.10.5,tcp:8083
192.168.20.2,tcp:443
192.168.10.5,tcp:8086
192.168.10.5,tcp:8081
192.168.10.5,tcp:8084
192.168.10.1,tcp:80
192.168.10.5,tcp:8090
192.168.30.1,udp:53 comment "dns"
192.168.10.5,tcp:8087
192.168.10.5,tcp:8089
# 删除指定端口范围
$ ipset del black_list 192.168.10.5,8087-8090
# -s 自动排序显示
$ ipset -s list black_list
Name: black_list
Type: hash:ip,port
Revision: 5
Header: family inet hashsize 1024 maxelem 65536 comment
Size in memory: 3212
References: 0
Number of entries: 10
Members:
192.168.10.1,tcp:80
192.168.10.5,tcp:8080
192.168.10.5,tcp:8081
192.168.10.5,tcp:8082
192.168.10.5,tcp:8083
192.168.10.5,tcp:8084
192.168.10.5,tcp:8085
192.168.10.5,tcp:8086
192.168.20.2,tcp:443
192.168.30.1,udp:53 comment "dns"
# 显示为save格式,即保存集合配置
$ ipset -s -o save list black_list
$ ipset -s save black_list
create black_list hash:ip,port family inet hashsize 1024 maxelem 65536 comment
add black_list 192.168.10.1,tcp:80
add black_list 192.168.10.5,tcp:8080
add black_list 192.168.10.5,tcp:8081
add black_list 192.168.10.5,tcp:8082
add black_list 192.168.10.5,tcp:8083
add black_list 192.168.10.5,tcp:8084
add black_list 192.168.10.5,tcp:8085
add black_list 192.168.10.5,tcp:8086
add black_list 192.168.20.2,tcp:443
add black_list 192.168.30.1,udp:53 comment "dns"
# 显示所有ipset集合名称
$ ipset -n list
myset
black_list
# 显示所有ipset集合头部信息,即不包括IP列表
$ ipset -t list
Name: myset
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 360
References: 0
Number of entries: 5

Name: black_list
Type: hash:ip,port
Revision: 5
Header: family inet hashsize 1024 maxelem 65536 comment
Size in memory: 3212
References: 0
Number of entries: 10
# 保存指定集合,默认save选项为输出,-f 保存到文件
$ ipset -s save black_list > black_list
$ ipset -s save black_list -f black_list
# 保存所有集合
$ ipset -s save > ipset_all
# 清空指定集合
$ ipset flush myset
$ ipset list myset
# 删除指定集合
$ ipset destroy myset
# 注意如果恢复的文件中存在和当前同名的集合,则同名集合会恢复失败
$ ipset restore -f ipset_all
ipset v7.1: Error in line 7: Set cannot be created: set with the same name already exists
# 删除所有集合,destroy可简写为x
$ ipset x
$ ipset list
$ ipset restore -f ipset_all

# 创建网络集合,添加默认timeout参数,即默认600s将自动删除条目
$ ipset create white_list hash:net timeout 600
# 使用nomatch排除子网
$ ipset add white_list 192.168.10.0/25
$ ipset add white_list 192.168.10.0/28 nomatch
# 指定条目的超时时间,0表示永久有效
$ ipset add white_list 192.168.20.0/24 timeout 60
$ ipset add white_list 192.168.100.0/24 timeout 0
# 通过list可以查看剩余时间
$ ipset list white_list
Name: white_list
Type: hash:net
Revision: 6
Header: family inet hashsize 1024 maxelem 65536 timeout 600
Size in memory: 568
References: 0
Number of entries: 2
Members:
192.168.10.0/28 timeout 595 nomatch
192.168.10.0/25 timeout 571
192.168.20.0/24 timeout 57
192.168.100.0/24 timeout 0
# 使用-exist选项重置或更改超时时间
$ ipset -exist add white_list 192.168.20.0/24 timeout 300

# 创建IPv6集合,只能添加IPv6地址
$ ipset create ipv6_list hash:ip family inet6
$ ipset add ipv6_list 192.168.10.1
ipset v7.1: Syntax error: cannot parse 192.168.10.1: resolving to IPv6 address failed
$ ipset add ipv6_list 1:2:3:4::6
$ ipset list ipv6_list
Name: ipv6_list
Type: hash:ip
Revision: 4
Header: family inet6 hashsize 1024 maxelem 65536
Size in memory: 232
References: 0
Number of entries: 1
Members:
1:2:3:4::6