1、创建自定义链
#示例:在filter表中创建IN_WEB自定义链
iptables -t filter -N IN_WEB
2、引用自定义链
#示例:在INPUT链中引用刚才创建的自定义链
iptables -t filter -I INPUT -p tcp --dport 80 -j IN_WEB
3、重命名自定义链
#示例:将IN_WEB自定义链重命名为WEB
iptables -E IN_WEB WEB
4、删除定义链
删除自定义链需要满足两个条件
1、自定义链没有被引用
2、自定义链中没有任何规则
#示例:删除引用计数为0并且不包含任何规则的WEB链
iptables -X WEB
5、实践
自定义链并不能直接使用,而是需要被默认链引用才能够使用
在filter创建一个自定义链,链名为IPsecVPN
iptables -t filter -N IPsecVPN
自定义链创建完成后,查看filter表中的链,自定义链已经被创建,而且可以看到,这条自定义链的引用计数为0 (0 references),也就是说,这条自定义链还没有被任何默认链所引用,所以,即使IPsecVPN
配置了规则,也不会生效,我们现在不用在意它,继续聊我们的自定义链。
# iptables -t filter -nvL
Chain IPsecVPn (0 references)
pkts bytes target prot opt in out source destination
向自定义链中添加规则,对自定义链的操作与对默认链的操作并没有什么不同,一切按照操作默认链的方法操作自定义链即可
iptables -t filter -I IPsecVPN -s 10.0.0.0/8 -p udp --dport 500 -j ACCEPT
iptables -t filter -I IPsecVPN -s 10.0.0.0/8 -p udp --dport 4500 -j ACCEPT
此时自定义链中生成了相关规则,但这时由于自定义链还未被引用,所以规则未生效。
Chain IPsecVPn (0 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT udp -- * * 10.0.0.0/8 0.0.0.0/0 udp dpt:4500
0 0 ACCEPT udp -- * * 10.0.0.0/8 0.0.0.0/0 udp dpt:500
在INPUT链中引用自定义链,udp目的端口为500,4500的流量,交由自定义链IPsecVPN
处理。
iptables -I INPUT -p udp --dport 500 -j IPsecVPN
iptables -I INPUT -p udp --dport 4500 -j IPsecVPN
查看filter表中的IPsecVPN链情况,此时为2 references
,表示被引用了两次。
Chain IPsecVPn (2 references)
pkts bytes target prot opt in out source destination
8 232 ACCEPT udp -- * * 10.0.0.0/8 0.0.0.0/0 udp dpt:4500
0 0 ACCEPT udp -- * * 10.0.0.0/8 0.0.0.0/0 udp dpt:500