1. 程式人生 > 實用技巧 >linux系統中防火牆管理工具iptables

linux系統中防火牆管理工具iptables

1、iptables是什麼

防火牆:內網與公網之間、主機之間的保護屏障,依據防火牆策略對流量進行過濾。

iptables是防火牆策略管理工具,用於設定防火牆策略。

防火牆會從按照從上至下的順序來讀取防火牆策略,如果匹配,則執行匹配規則定義的行為,如果沒有匹配當前策略,則繼續匹配下一條,如果都沒有匹配,則執行預設行為。

防火牆的策略一般有允許和拒絕兩種。當防火牆的預設策略是拒絕時,就要設定允許的規則,否則誰都進不來;如果防火牆策略是允許時,就要設定拒絕動作,否則誰都能進來。

一般來說,從內網向外網傳送的流量一般都是可控和良性的,因此我們使用最多的就是INPUT進入流量規則設定

2、iptables的使用

iptables是依據命令列進行防火牆策略設定的工具。常用引數如下:

3、如何檢視系統當前的防火牆策略規則鏈

[root@PC1linuxprobe test1]# iptables -L | wc -l
104
[root@PC1linuxprobe test1]# iptables -L | head -n 5  ## 檢視前5行,進入流量INPUT預設是ACCEPT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  
-- anywhere anywhere INPUT_direct all --
anywhere anywhere [root@PC1linuxprobe test1]#

4、清空當前的防火牆策略規則鏈

[root@PC1linuxprobe test1]# iptables -F  ## 清空當前的防火牆策略規則鏈
[root@PC1linuxprobe test1]# iptables -L | wc -l  ## 列出當前的防火牆策略規則鏈,由104行減少為71行
71
[root@PC1linuxprobe test1]# iptables 
-L | head -n 5 ## 檢視前5行,可見只剩下標題 Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination