Iptables工具的使用

                                                    Iptables工具的使用

                                                                                  —iptables指令參數

iptables命令在使用上有些複雜,但是如果一定規律發現還是沒那麼難懂的。

iptables指令可以劃分爲兩個部分,一個是“iptables指令參數”,另一個是“規則語法”。這裏先分析“iptables指令參數”。

(一)、iptables命令參數

                                                                

對於上圖進行解釋:

iptablesiptsbles命令

-tTABLE爲選擇Netfilter內部命令結構默認爲FilterTables。目前TABLE有四個選項filternatmangleraw四種。

Filternetfilter內部最重要的機制,其任務爲執行封包的過濾動作,也就是防火牆的功能。

NatNAT(NetworkAddress Translation)也是防火牆上衣個不可或缺的重要機制,其功能用通俗的方式來說,就是IP分享器,只不過其所能執行的功能比訪問的IP分享器功能強大了許多。

Mangle我們可以藉助mangle機制修改行經防火牆內的封包內容。

Raw負責加快封包穿越防火牆機制的速度,藉此提高防火牆的性能。

Filter的操作方式:

-L列出Table內容

-F清除Table內容

-A加入新的規則

-P設定DefaultPilicy

-l插入新的規則

-R取代規則

-D刪除規則

(二)、FilterTable的操作

舉例1[root@guo~]#iptables -t filter -L

    filterTables的所有內容列出來 。

[root@guo~]#iptables -t filter -L

ChainINPUT (policy ACCEPT) ------------ INPUTChain的所有內容。目前INPUTChain的DefaltChain狀態爲ACCEPT
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps

ChainFORWARD (policy ACCEPT) ----------- FORWARDChain的所有內容。目前FORWORDChain的DefaltChain狀態爲ACCEPT
target prot opt source destination ACCEPT all -- anywhere 192.168.122.0/24 stateRELATED,ESTABLISHED
ACCEPT all -- 192.168.122.0/24 anywhere
ACCEPT all -- anywhere anywhere REJECT all -- anywhere anywhere reject-withicmp-port-unreachable
REJECT all -- anywhere anywhere reject-withicmp-port-unreachable
REJECT all -- anywhere anywhere reject-withicmp-host-prohibited

ChainOUTPUT (policy ACCEPT) --------------- OUTPUT Chain的所有內容,此時爲空,表示目前沒有規則。目前OUTPUTChain 的DefaltChain狀態爲ACCEPT
target prot opt source destination

由上面可知:FilterTable包含三種ChainINPUTChainOUPUTChainFORWORDChain

舉例2[root@guo~]# iptables -t filter -L INPUT

     filterTable中的INPUTChain內容列出來

[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps
ACCEPT tcp -- anywhere anywhere tcp dpt:bootps 
舉例3[root@guo~]# iptables -t filter -F

filterTable中的所有內容清除

[root@guo~]# iptables -t filter -F
[root@guo~]# iptables -t filter -L

ChainINPUT (policy ACCEPT)
target prot opt source destination

ChainFORWARD (policy ACCEPT)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination 
舉例4[root@guo~]# iptables -t filter -A INPUT -p icmp -j ACCEPT

把規則加入到FilterTabieINPUTChain

解釋:-AINOUT將規則將如到INOUTChain

規則:-picmp -j ACCEPT爲本次將如到INPUTChain的規則,匹配icmp協議

封包,-jACCEPT將符合條件的封包以特定的方式處理,這裏爲ACCEPT

[root@guo~]# iptables -t filter -A INPUT -p icmp -j ACCEPT
[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere 
舉例5[root@guo~]# iptables -t filter -P FORWARD DROP

-PFORWARD DROP:本次所要設定的FORWARDChainPolicy

[root@guo~]# iptables -t filter -P FORWARD DROP
[root@guo~]# iptables -t filter -L FORWARD

ChainFORWARD (policy DROP) ------------此時FPRWARDChain的DefaultPolicy已經設置爲DROP。注意iptables中的-F不會影響到DefaultPolicy的值,若要改
DefaultPolicy的狀態,一定要是用-P的參數來設定。
target prot opt source destination 
舉例6[root@guo~]# iptables -tfilter -I INPUT 2 -p tcp -j ACCEPT

   INPUTChain內插入新規則INPUT2表示插在第二行

[root@guo~]# iptables -tfilter -I INPUT 2 -p tcp -j ACCEPT
[root@guo~]# iptables -tfilter -L

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere

ChainFORWARD (policy DROP)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination

[root@guo~]# iptables -t filter -L INPUT –line-number ---------在-L參數後方加–line-number的參數,這樣會在規則的前方加上行號

ChainINPUT (policy ACCEPT)
num target prot opt source destination

1 ACCEPT udp -- anywhere anywhere udpdpt:domain
2 ACCEPT tcp -- anywhere anywhere tcpdpt:domain
3 ACCEPT udp -- anywhere anywhere udpdpt:bootps
4 ACCEPT tcp -- anywhere anywhere tcpdpt:bootps
5 ACCEPT all -- anywhere anywhere stateRELATED,ESTABLISHED 
舉例7[root@guo~]# iptables -t filter -R INPUT 3 -p tcp -j ACCEPT

取代INPUTChain已經存在的規則,3表示取代第桑三條規則,-ptcp -j ACCEPT

以本規則取代原有的規則。

[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:domain

[root@guo~]# iptables -t filter -R INPUT 3 -p tcp -j ACCEPT
[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere --------- 已被取代
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
舉例8[root@guo~]# iptables -t filter -D INPUT 2

刪除INPUTChain內已經存在的規則

-D INPUT刪除INPUTChain已經存在的規則

2刪除原有的第二條規則

[root@guo~]# iptables -t filter -L INPUT
ChainINPUT (policy ACCEPT)

target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT udp -- anywhere anywhere udp dpt:domain -----刪除
ACCEPT tcp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:domain

[root@guo~]# iptables -t filter -D INPUT 2 ---- 刪除
[root@guo~]# iptables -t filter -L INPUT ----- 刪除成功

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
(三)、NATTable的操作

舉例1[root@guo~]# iptables -t nat -L

查看NATTable的內容

[root@guo~]# iptables -t nat -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainPOSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE tcp -- 192.168.122.0/24 !192.168.122.0/24 masq ports:1024-65535
MASQUERADE udp -- 192.168.122.0/24 !192.168.122.0/24 masq ports:1024-65535
MASQUERADE all -- 192.168.122.0/24 !192.168.122.0/24

ChainOUTPUT (policy ACCEPT)
target prot opt source destination 
由上面可知NATTable也有三個Chain,分別爲PREROUTINGChainPOSTROUTINGChainOUTPUT

舉例2root@guo~]# iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -jSNAT --to 222.24.21.168

將規則--oeth0 -s 192.168.0.0/24 -j SNAT --to 222.24.21.168加入到POSTROUTING Chain中。

[root@guo~]# iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -j SNAT--to 222.24.21.168
[root@guo~]# iptables -t nat -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainPOSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 192.168.0.0/24 anywhere to:222.24.21.168 --證明已經成功

ChainOUTPUT (policy ACCEPT)
target prot opt source destination 
(四)、MangleTable的操作

[舉例1root@guo~]# iptables -t mangle -L

MangleTable的內容列出

root@guo~]# iptables -t mangle -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainINPUT (policy ACCEPT)
target prot opt source destination

ChainFORWARD (policy ACCEPT)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination

ChainPOSTROUTING (policy ACCEPT)
target prot opt source destination
CHECKSUM udp -- anywhere anywhere udp dpt:bootpcCHECKSUM fill 
有上面可以看出:MangleTable包含五種ChainPREROUTINGChainINPUTChainFORWARDChainOUTPUTChainPOSTROUTINGChain

舉例2[root@guo~]# iptables -t mangle -A INPUT -p icmp -j ACCEPT

將規則-p icmp -j ACCEPT將如到INPUTChain

[root@guo~]# iptables -t mangle -A INPUT -p icmp -j ACCEPT
[root@guo~]# iptables -t mangle -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere --- 查看已經添加成功
(五)、RAWTable的操作

舉例1[root@guo~]# iptables -t raw -L

列出RAWTables內容

[root@guo~]# iptables -t raw -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination
由上面可以看出RAWTables包含兩種ChainPREROUTINGChainOUTPUTChain

舉例2[root@guo~]# iptables -t raw -A OUTPUT -p tcp -j ACCEPT

將規則-p tcp -jACCEPT加入到OUTPUTChain中去

[root@guo~]# iptables -t raw -A OUTPUT -p tcp -j ACCEPT
[root@guo~]# iptables -t raw -L OUTPUT

ChainOUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere --查看已存在
(六)、由上面可以總結出Netfilter的結構圖

                                         

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