一、ipset概述
ipset與iptable
· iptables是在Linux核心中配置防火牆規則的使用者空間工具。在核心版本更新到2.4以來,iptable一直作為系統中主要的防火牆解決方案。CentOS7将原來的iptable替換為firewall,而firewall提供了對ipset的支援。
· ipset相當于iptable的擴充,它和iptable 處理方式,iptable通過連結清單線性存儲然後周遊來實作比對。而ipset通過通過索引的資料結構設計達到快速比對。這種設計使得當set配置比較龐大的時候,也可以高效地進行比對。
适用範圍
· 存儲多個IP位址或端口号,并在一個swoop中與iptables的集合相比對;
· 在不影響性能的同時,針對IP位址或端口的變化動态更新iptables規則;
· 性能高,用一個iptables規則來表示複雜的IP位址和端口
安裝
在CentOS7更新firewalld到最新版本,即可使用ipset,更新安裝指令:
yum install firewalld
二、ipset使用
利用firewall-cmd對ipset進行操作
ipset的存儲路徑:/etc/firewalld/ipsets
指令集合:
擷取指定ipset資訊
增加ipset
其中option可不填,eg:–option=family=inet6指定該ipset使用IPV6位址。
删除指定ipset
删除後,在ipset目錄下會有一個備份檔案,例如:原來的ipset為test_set.xml,則删除後會生成一個test_set.xml.old.
指定ipset中增加entry
firewall-cmd --permanent --ipset=[ipset_name] --add-entry=[xx.xx.xx.xx]
指定ipset中删除entry
删除entry
–permanent參數直接決定是否永久生效 需要執行firewall-cmd –reload,使其生效
不加–permanent是直接生效(runtime environment),不會儲存在相應的ipset的XML檔案中,重新開機firewalld服務将會失效
IPSet Options
–get-ipset-types Print the supported ipset types
–new-ipset=<ipset> –type=<ipset type> [–option=<key>[=<value>]]..
Add a new ipset [P only]
–new-ipset-from-file=<filename> [–name=<ipset>]
Add a new ipset from file with optional name [P only]
–delete-ipset=<ipset>
Delete an existing ipset [P only]
–load-ipset-defaults=<ipset>
Load ipset default settings [P only]
–info-ipset=<ipset> Print information about an ipset
–path-ipset=<ipset> Print file path of an ipset [P only]
–get-ipsets Print predefined ipsets
–ipset=<ipset> –set-description=<description>
Set new description to ipset [P only]
–ipset=<ipset> –get-description
Print description for ipset [P only]
–ipset=<ipset> –set-short=<description>
Set new short description to ipset [P only]
–ipset=<ipset> –get-short
Print short description for ipset [P only]
–ipset=<ipset> –add-entry=<entry>
Add a new entry to an ipset [P]
–ipset=<ipset> –remove-entry=<entry>
Remove an entry from an ipset [P]
–ipset=<ipset> –query-entry=<entry>
Return whether ipset has an entry [P]
–ipset=<ipset> –get-entries
List entries of an ipset [P]
–ipset=<ipset> –add-entries-from-file=<entry>
Add a new entries to an ipset [P]
–ipset=<ipset> –remove-entries-from-file=<entry>
Remove entries from an ipset [P]
三、ipset類型的差別
1、hash:ip
2、 hash:ip,mark
3、hash:ip,port
4、hash:ip,port,ip
5、hash:ip,port,net
6、 hash:mac
7、hash:net
8、hash:net,iface
9、hash:net,net
10、hash:net,port
11、hash:net,port,net
參考文獻
1、https://www.linuxjournal.com/content/advanced-firewall-configurations-ipset
2、https://firewalld.org/2015/12/ipset-support
3、https://firewalld.org/documentation/man-pages/firewalld.ipset