ipset 教程

 

 

ipset介紹 
ipset是iptables的擴展,它容許你建立 匹配整個地址集合的規則。而不像普通的iptables鏈只能單IP匹配, ip集合存儲在帶索引的數據結構中,這種結構即時集合比較大也能夠進行高效的查找,除了一些經常使用的狀況,好比阻止一些危險主機訪問本機,從而減小系統資源佔用或網絡擁塞,IPsets也具有一些新防火牆設計方法,並簡化了配置.官網:http://ipset.netfilter.org/網絡

1.ipset安裝數據結構

yum安裝: yum install ipset 
2.建立一個ipsettcp

ipset create xxx hash:net (也能夠是hash:ip ,這指的是單個ip,xxx是ipset名稱) .net

或者 ipset create xxx hash:ip設計

hash:net 和hash:ip的區別是net能夠時ip段,ip卻只能是固定的iprest

好比192.168.0.0/24 這個時ip段,能夠用net存儲。索引

192.168.1.1 這種用ip存儲,hash ip 存儲不了ip段。ip

通常最好時用net存儲,大量的ip段,最好用net存儲。資源


ipset默承認以存儲65536個元素,使用maxelem指定數量get

ipset create blacklist hash:net maxelem 1000000 #黑名單 
ipset create whitelist hash:net maxelem 1000000 #白名單 
查看已建立的ipset

ipset list 
3.加入一個名單ip

ipset add blacklist 10.60.10.xx 
4.去除名單ip

ipset del blacklist 10.60.10.xx 
5.建立防火牆規則,與此同時,allset這個IP集裏的ip都沒法訪問80端口(如:CC攻擊可用)

iptables -I INPUT -m set –match-set blacklist src -p tcp -j DROP 
iptables -I INPUT -m set –match-set whitelist src -p tcp -j DROP 
service iptables save 
iptables -I INPUT -m set –match-set setname src -p tcp –destination-port 80 -j DROP 
6.將ipset規則保存到文件

ipset save blacklist -f blacklist.txt 
ipset save whitelist -f whitelist.txt 
7.刪除ipset

ipset destroy blacklist 
ipset destroy whitelist 
8.導入ipset規則

ipset restore -f blacklist.txt ipset restore -f whitelist.txt ipset的一個優點是集合能夠動態的修改,即便ipset的iptables規則目前已經啓動,新加的入ipset的ip也生效

相關文章
相關標籤/搜索