iptables 运行逻辑及-I -A 参数解析


刚开始接触Iptables 就对-I  和 -A 参数很疑惑,-I 插入一条或多条规则 ,-A 是追加一条或多条规则。
 
都是加一条规则,究竟这两个有什么不同呢?
 
实验:
 
拿了两台机器,一台发PING包,一台被PING。
 
两台机器使用 iptables -nvL INPUT 查看,iptables 是空的
 
然后在被PING的机器加入 iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP
 
再用 iptables -nvL INPUT 查看如下:
 
Chain INPUT (policy ACCEPT 592 packets, 55783 bytes)
 
 pkts bytes target    prot opt in    out    source              destination
 
    8  672 DROP      icmp --  *      *      0.0.0.0/0            0.0.0.0/0          icmp type 8
 
此时发PING包的机器显示的PING包停住了。
 
此时在被PING的机器再加入 iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j ACCEPT
 
再用 iptables -nvL INPUT 查看如下:
 
Chain INPUT (policy ACCEPT 678 packets, 62701 bytes)
 
 pkts bytes target    prot opt in    out    source              destination
 
  21  1764 DROP      icmp --  *      *      0.0.0.0/0            0.0.0.0/0          icmp type 8
 
    0    0 ACCEPT    icmp --  *      *      0.0.0.0/0            0.0.0.0/0          icmp type 8
 
显示iptables 被追加了一条规则,但发PING包的机器显示的PING包仍停住,证明新加入的规则不能放行PING包
 
在被PING的机器再加入iptables -I INPUT -p icmp --icmp-type 8 -s 0/0 -j ACCEPT
 
再用 iptables -nvL INPUT 查看如下:
 
Chain INPUT (policy ACCEPT 770 packets, 70223 bytes)
 
 pkts bytes target    prot opt in    out    source              destination
 
    2  168 ACCEPT    icmp --  *      *      0.0.0.0/0            0.0.0.0/0          icmp type 8
 
  31  2604 DROP      icmp --  *      *      0.0.0.0/0            0.0.0.0/0          icmp type 8
 
    0    0 ACCEPT    icmp --  *      *      0.0.0.0/0            0.0.0.0/0          icmp type 8
 
显示iptables 新增一条规则,此时发PING包的机器显示的PING包再次跳动,证明新加入的规则能放行PING包
 
而两个规则放行规则的差异只是 -A 和 -I ,-A 追加规则在DROP 规则后,-I增加规则在DROP 规则前。
 
iptables 是由上而下的进行规则匹配,放行规则需在禁行规则之前才能生效。

相关内容