使用自定义链可以对规则进行分类,将特定的类放在一个链中,比如将HTTP服务的放在一个链中,SSH服务的放在另一个链中

自定义链默认是不可使用的,只有被默认链引用才会生效

创建自定义链:

root@Archer_BE230:~# iptables -t filter -N IN_WEB
root@Archer_BE230:~# ipctables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
 3240  775K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
 2842  769K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
  301 21075 input_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0
  301 21075 input      all  --  *      *       0.0.0.0/0            0.0.0.0/0
    1    32 input_expolicy  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
  476 24752 forwarding_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0
  476 24752 forward    all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
 3550  794K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
 2842  769K ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0
   88  7212 output_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0
   88  7212 output     all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain IN_WEB (0 references)
 pkts bytes target     prot opt in     out     source               destination

root@Archer_BE230:~# iptables -t filter -I IN_WEB -s 192.168.0.2 -j REJECT
root@Archer_BE230:~# iptables -t filter --line -nvL IN_WEB
Chain IN_WEB (0 references)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       192.168.0.2          0.0.0.0/0            reject-with icmp-port-unreachable

-N选项用于创建自定义链。

引用自定义链:

root@Archer_BE230:~# iptables -I INPUT -p tcp --dport 80 -j IN_WEB
root@Archer_BE230:~# iptables -t filter --line -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 IN_WEB     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
2     6593 1729K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
3     6010 1708K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4      358 24543 input_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0
5      358 24543 input      all  --  *      *       0.0.0.0/0            0.0.0.0/0
6        1    32 input_expolicy  all  --  *      *       0.0.0.0/0            0.0.0.0/0

这里的-j IN_WEB表示访问80端口的TCP报文将由自定义链IN_WEB中的规则处理。与创建规则时的-j选项指定动作不同。

重命名自定义链:

root@Archer_BE230:~# iptables -E IN_WEB WEB
root@Archer_BE230:~# iptables -t filter --line -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1       30  1560 WEB        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
2    10360 2861K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
3     9603 2828K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4      394 27041 input_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0
5      394 27041 input      all  --  *      *       0.0.0.0/0            0.0.0.0/0
6        1    32 input_expolicy  all  --  *      *       0.0.0.0/0            0.0.0.0/0
root@Archer_BE230:~#
root@Archer_BE230:~# iptables -t filter --line -nvL WEB
Chain WEB (1 references)
num   pkts bytes target     prot opt in     out     source               destination
1       30  1560 REJECT     all  --  *      *       192.168.0.2          0.0.0.0/0            reject-with icmp-port-unreachable

-E选项重命名自定义链之后,自定义链及其引用都会被重命名。

删除自定义链:

满足条件:

  1. 无引用
  2. 无规则
root@Archer_BE230:~# iptables X WEB
iptables: Too many links.
root@Archer_BE230:~# iptables -D INPUT 1
root@Archer_BE230:~#
root@Archer_BE230:~# iptables -t filter --line -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1    10937 3009K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2    10133 2975K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
3      401 27483 input_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0
4      401 27483 input      all  --  *      *       0.0.0.0/0            0.0.0.0/0
5        1    32 input_expolicy  all  --  *      *       0.0.0.0/0            0.0.0.0/0
root@Archer_BE230:~#
root@Archer_BE230:~# iptables -X WEB
iptables: Directory not empty.
root@Archer_BE230:~#
root@Archer_BE230:~# iptables -t filter -F WEB
root@Archer_BE230:~# iptables -t filter --line -nvL WEB
Chain WEB (0 references)
num   pkts bytes target     prot opt in     out     source               destination
root@Archer_BE230:~#
root@Archer_BE230:~# iptables -X WEB
root@Archer_BE230:~#

参考文件

iptables详解(10):iptables自定义链 - wanstack - 博客园