iptables 運行邏輯及-I -A 參數解析

轉自http://wangwei007.blog.51cto.com/68019/1095786

 

    剛開始接觸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 是由上而下的進行規則匹配,放行規則需在禁行規則之前才能生效。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章