linux防火墻iptables規(guī)則的查看、添加、刪除和修改方法總結(jié)
1、查看
iptables -nvL --line-number
-L 查看當前表的所有規(guī)則,默認查看的是filter表,如果要查看NAT表,可以加上-t NAT參數(shù)
-n 不對ip地址進行反查,加上這個參數(shù)顯示速度會快很多
-v 輸出詳細信息,包含通過該規(guī)則的數(shù)據(jù)包數(shù)量,總字節(jié)數(shù)及相應(yīng)的網(wǎng)絡(luò)接口
–-line-number 顯示規(guī)則的序列號,這個參數(shù)在刪除或修改規(guī)則時會用到
2、添加
添加規(guī)則有兩個參數(shù):-A和-I。其中-A是添加到規(guī)則的末尾;-I可以插入到指定位置,沒有指定位置的話默認插入到規(guī)則的首部。
當前規(guī)則:
[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
添加一條規(guī)則到尾部:
[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP
再插入一條規(guī)則到第三行,將行數(shù)直接寫到規(guī)則鏈的后面:
[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP
查看:
[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.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參數(shù)
刪除之前添加的規(guī)則(iptables -A INPUT -s 192.168.1.5 -j DROP):
[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP
有時候要刪除的規(guī)則太長,刪除時要寫一大串,既浪費時間又容易寫錯,這時我們可以先使用–line-number找出該條規(guī)則的行號,再通過行號刪除規(guī)則。
[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 -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.3 0.0.0.0/0
刪除第二行規(guī)則
[root@test ~]# iptables -D INPUT 2
4、修改
修改使用-R參數(shù)
先看下當前規(guī)則:
[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.5 0.0.0.0/0
將第三條規(guī)則改為ACCEPT:
[root@test ~]# iptables -R INPUT 3 -j ACCEPT
再查看下:
[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 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
第三條規(guī)則的target已改為ACCEPT。
5、永久生效
service iptables save
service iptables restart
以上就是本文介紹的對linux防火墻iptables規(guī)則進行查看、添加、刪除和修改的操作
相關(guān)文章
Apache Request-URI Too Large錯誤解決方法
這篇文章主要介紹了Apache Request-URI Too Large錯誤解決方法,需要的朋友可以參考下2014-05-05Linux內(nèi)核設(shè)備驅(qū)動之高級字符設(shè)備驅(qū)動筆記整理
今天小編就為大家分享一篇關(guān)于Linux內(nèi)核設(shè)備驅(qū)動之高級字符設(shè)備驅(qū)動筆記整理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12詳解Ubuntu16.04下Hadoop 2.7.3的安裝與配置
本篇文章主要介紹了詳解Ubuntu16.04下Hadoop 2.7.3的安裝與配置,具有一定的參考價值,有興趣的可以了解一下。2017-01-01linux系統(tǒng)下定時執(zhí)行php腳本的方法
網(wǎng)站運營過程中,經(jīng)常會遇到需要定時執(zhí)行php腳本的情況,下面這篇文章主要介紹了linux系統(tǒng)下定時執(zhí)行php腳本的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01淺談Apache?Commons?Pool2池化技術(shù)
Apache?Commons?Pool2為開發(fā)者提供了一套豐富的API和靈活的配置選項,以實現(xiàn)對象的池化管理,本文就詳細的來介紹一下Apache?Commons?Pool2池化技術(shù),感興趣的可以了解一下2024-08-08Linux服務(wù)器安全需要注意到幾點事項小結(jié)
Linux服務(wù)器安全是個很重要的問題,這里簡單的總結(jié)下linux服務(wù)器的設(shè)置技巧。2010-08-08