高效管理IP集合利器ipset
🚀为什么需要ipset
在 Linux 防火墙管理中,当需要处理大量 IP 地址时,传统的 iptables 会面临性能瓶颈。例如,封禁 10,000 个 IP 需要创建 10,000 条 iptables 规则,导致:
- ❌ 规则匹配效率低下
- ❌ 内存占用巨大
- ❌ 管理维护困难
ipset 应运而生,它将多个 IP/端口/MAC 地址存储在高效的集合结构中,通过单条 iptables 规则即可匹配整个集合,性能提升 10-100 倍!
🚀ipset核心优势
1. 性能对比
| 场景 |
iptables |
ipset |
提升倍数 |
| 10,000个IP封禁 |
10,000条规则 |
1条规则 + 1个集合 |
100倍 |
| 内存占用 |
300MB+ |
5-10MB |
30-60倍 |
| 规则匹配 |
O(n) 线性扫描 |
O(1) 哈希查找 |
指数级 |
2. 适用场景
DDoS防护:快速封禁攻击源 IP
地理封锁:批量管理国家 IP 段
安全组:动态 IP 白名单/黑名单
负载均衡:后端服务器集合管理
网络审计:流量监控 IP 集合
🚀基础安装与配置
1. 安装ipset
1 2 3 4 5 6 7 8
| sudo apt update && sudo apt install ipset
sudo yum install ipset
ipset --version
|
2. 内核模块加载
1 2 3 4 5 6
| lsmod | grep ip_set
sudo modprobe ip_set_hash_net sudo modprobe ip_set_hash_ip
|
🚀常用实战案例
1. 案例1:IP黑名单管理
1 2 3 4 5 6 7 8 9 10 11 12
| ipset create blacklist hash:ip timeout 1800
ipset add blacklist 203.0.113.5 ipset add blacklist 198.51.100.10 timeout 3600
iptables -I INPUT -m set --match-set blacklist src -j DROP
ipset list blacklist
|
2. 案例2:国家IP段封锁
1 2 3 4 5 6 7 8 9
| ipset create cn_ip hash:net maxelem 10000
wget -O cn.zone http://www.ipdeny.com/ipblocks/data/countries/cn.zone while read line; do ipset add cn_ip $line; done < cn.zone
iptables -A INPUT -p tcp --dport 80 -m set --match-set cn_ip src -j DROP
|
3. 案例3:动态端口白名单
1 2 3 4 5 6 7 8 9 10
| ipset create whitelist hash:ip,port
ipset add whitelist 192.168.1.100,tcp:22 ipset add whitelist 192.168.1.200,tcp:80,443
iptables -A INPUT -m set --match-set whitelist src,dst -j ACCEPT iptables -A INPUT -j DROP
|
4. 案例4:DDoS防护自动封禁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #!/bin/bash
IPSET_NAME="ddos_protect" THRESHOLD=100
ipset create $IPSET_NAME hash:ip timeout 600
tcpdump -nni eth0 | awk '{print $3}' | cut -d. -f1-4 | \ sort | uniq -c | while read count ip; do if [ $count -gt $THRESHOLD ]; then echo "封禁IP: $ip (连接数: $count)" ipset add $IPSET_NAME $ip 2>/dev/null fi done
|
🚀与iptables高级集成
1. 多重条件匹配
1 2 3 4 5 6 7
| ipset create src_ips hash:ip ipset create dst_ports hash:port
iptables -A FORWARD -m set --match-set src_ips src \ -m set --match-set dst_ports dst \ -j LOG --log-prefix "MATCHED: "
|
2. 连接状态+ipset组合
1 2 3 4
| iptables -A INPUT -m conntrack --ctstate NEW \ -m set --match-set blacklist src \ -j DROP
|
🚀持久化与备份
1. 保存ipset配置
1 2 3 4 5
| ipset save > /etc/ipset.rules
ipset save blacklist > /etc/ipset-blacklist.rules
|
2. 恢复ipset配置
1 2 3 4 5 6
| ipset restore < /etc/ipset.rules
ipset destroy blacklist 2>/dev/null ipset restore -f /etc/ipset-blacklist.rules
|
🚀性能优化技巧
1. 合理设置哈希表大小
1 2
| ipset create optimized hash:ip hashsize 4096 maxelem 100000
|
2. 使用网络位图优化小范围IP
1 2
| ipset create lan_ips bitmap:ip range 192.168.1.0/24
|
3. 超时机制避免集合膨胀
1 2
| ipset create temp_ips hash:ip timeout 600 family inet6
|
🚀核心管理命令
1. 集合生命周期管理
| 命令 |
功能 |
示例 |
create |
创建新集合 |
ipset create blacklist hash:ip |
destroy |
删除集合 |
ipset destroy blacklist |
flush |
清空集合元素 |
ipset flush blacklist |
rename |
重命名集合 |
ipset rename blacklist blocklist |
2. 元素操作命令
| 命令 |
功能 |
示例 |
add |
添加元素 |
ipset add blacklist 192.168.1.100 |
del |
删除元素 |
ipset del blacklist 192.168.1.100 |
test |
检查元素存在 |
ipset test blacklist 192.168.1.100 |
3. 信息查询命令
| 命令 |
功能 |
示例 |
list |
显示集合内容 |
ipset list 或 ipset list blacklist |
save |
保存到文件 |
ipset save > ipset.rules |
restore |
从文件恢复 |
ipset restore < ipset.rules |
🚀常用参数总结
1. 集合创建参数
1 2 3 4 5 6 7 8 9
| ipset create <集合名> <类型> [选项]
hashsize 1024 maxelem 65536 timeout 300 counters family inet6
|
2. 集合类型快速参考
| 类型 |
语法 |
存储格式 |
典型用途 |
hash:ip |
hash:ip |
单个IP地址 |
IP黑名单 |
hash:net |
hash:net |
IP网段 |
国家封锁 |
hash:ip,port |
hash:ip,port |
IP+端口组合 |
服务访问控制 |
hash:net,port |
hash:net,port |
网段+端口 |
部门策略 |
bitmap:ip |
bitmap:ip range x.x.x.x/y |
IP范围位图 |
局域网管理 |
list:set |
list:set |
集合的集合 |
复杂策略组合 |
3. 元素添加参数
1 2 3 4 5 6
| ipset add blacklist 192.168.1.100 timeout 3600
ipset add blacklist 192.168.1.100 comment "恶意扫描IP"
ipset add blacklist 192.168.1.100 skbmark 0x1
|
4. 高级匹配参数
1 2 3 4 5 6
| ipset add service_ports 80,443,8000-8080
ipset add network_blocks 192.168.0.0/16
ipset add mac_list 00:11:22:33:44:55
|
上一篇 « 一定要了解的iptables实用技巧