Ipset (简体中文)

Related articles

翻译状态: 本文是英文页面 Ipset翻译,最后翻译时间:2016-06-14,点击这里可以查看翻译后英文页面的改动。

ipset是 Linux 防火墙iptables的一个伴随工具。 除了其他众多功能,它允许你建立规则来轻松愉快地屏蔽一组IP地址。

安装

官方源安装 ipset

配置

屏蔽一组地址

先创建一个新的网络地址的“集合”。下面的命令创建了一个新的叫做“myset”的“net”网络地址的“hash”集合。 Start by creating a new "set" of network addresses. This creates a new "hash" set of "net" network addresses named "myset".

# ipset create myset hash:net

把你希望屏蔽的IP地址添加到集合中。 Add any IP address that you'd like to block to the set.

# ipset add myset 14.144.0.0/12
# ipset add myset 27.8.0.0/13
# ipset add myset 58.16.0.0/15

最后,配置iptables来屏蔽这个集合中的所有地址。这个命令将会向“INPUT”链顶端添加一个规则来从ipset中“-m”匹配名为“myset”的集合,当匹配到的包是一个“src”包时,“DROP”屏蔽掉它。 Finally, configure iptables to block any address in that set. This command will add a rule to the top of the "INPUT" chain to "-m" match the set named "myset" from ipset (--match-set) when it's a "src" packet and "DROP", or block, it.

# iptables -I INPUT -m set --match-set myset src -j DROP

使ipset持久化

你创建的ipse存在于内存中,重启后将会消失。要使ipset持久化,你要这样做:

首先把ipset保存到/etc/ipset.conf:

# ipset save > /etc/ipset.conf

然后启用 ipset.service,这个服务与用于恢复iptables rulesiptables.service相似。

使用PeerGuardian和其它列表屏蔽

maeyanie.com所作的pg2ipset-gitAURipset-update.sh 脚本配合可以用cron来自动更新多个屏蔽列表。 当前实现了按默认国家屏蔽,tor退出节点屏蔽,和来自Bluetack的pg2列表屏蔽。

其他命令

查看集合。

# ipset list

删除名为“myset”的集合。

# ipset destroy myset

删除所有集合。

# ipset destroy

更多信息请参考ipset的man手册页。

优化

iprangeAUR工具可以通过合并相邻范围或消除重复范围来帮助减少ipset.conf中的项目。 在表的大小很大时,这有助于改善路由/防火墙的性能。这个工具也可以把一个主机名的列表转换成IP列表。