天天看點

iptables規則的檢視、添加、删除和修改

iptables規則的檢視、添加、删除和修改

這裡隻列出比較常用的參數,詳細的請檢視man iptables

1、檢視

iptables -nvL –line-number

-L 檢視目前表的所有規則,預設檢視的是filter表,如果要檢視NAT表,可以加上-t NAT參數

-n 不對ip位址進行反查,加上這個參數顯示速度會快很多

-v 輸出詳細資訊,包含通過該規則的資料包數量,總位元組數及相應的網絡接口

–line-number 顯示規則的序列号,這個參數在删除或修改規則時會用到

2、添加

添加規則有兩個參數:-A和-I。其中-A是添加到規則的末尾;-I可以插入到指定位置,沒有指定位置的話預設插入到規則的首部。

目前規則:

[root@test ~]# iptables -nL --line-number

Chain INPUT (policy ACCEPT)

num  target     prot opt source               destination

1    DROP       all  --  192.168.1.1          0.0.0.0/0

2    DROP       all  --  192.168.1.2          0.0.0.0/0

3    DROP       all  --  192.168.1.4          0.0.0.0/0

添加一條規則到尾部:

[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

再插入一條規則到第三行,将行數直接寫到規則鍊的後面:

[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

檢視:

3    DROP       all  --  192.168.1.3          0.0.0.0/0

4    DROP       all  --  192.168.1.4          0.0.0.0/0

5    DROP       all  --  192.168.1.5          0.0.0.0/0

可以看到192.168.1.3插入到第三行,而原來的第三行192.168.1.4變成了第四行。

3、删除

删除用-D參數

删除之前添加的規則(iptables -A INPUT -s 192.168.1.5 -j DROP):

[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP

有時候要删除的規則太長,删除時要寫一大串,既浪費時間又容易寫錯,這時我們可以先使用–line-number找出該條規則的行号,再通過行号删除規則。

[root@test ~]# iptables -nv --line-number

iptables v1.4.7: no command specified

Try `iptables -h' or 'iptables --help' for more information.

删除第二行規則

[root@test ~]# iptables -D INPUT 2

4、修改

修改使用-R參數

先看下目前規則:

3    DROP       all  --  192.168.1.5          0.0.0.0/0

将第三條規則改為ACCEPT:

[root@test ~]# iptables -R INPUT 3 -j ACCEPT

再檢視下:

3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

第三條規則的target已改為ACCEPT。

本文轉自 xoyabc 51CTO部落格,原文連結:http://blog.51cto.com/xoyabc/1659162,如需轉載請自行聯系原作者

繼續閱讀