歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> iptables 運行邏輯及-I -A 參數解析

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

日期:2017/2/28 14:54:28   编辑:Linux教程
剛開始接觸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 是由上而下的進行規則匹配,放行規則需在禁行規則之前才能生效。
Copyright © Linux教程網 All Rights Reserved