本文只寫iptables的使用格式方法;
iptables的使用格式;
iptables -L -n -v --line-numbers 顯示規則序列號,如果需要刪除規則的話,只需刪除編號即可
iptables -t filter -L -n 顯示當前默認規則鏈[root@www ~]# iptables -t filter -L -n Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination示例;iptables -N test
iptables[-t table ]-N chain 創建一條自定義規則鏈
說明:[-t table ] 是指表的意思,這個在創建新的規則的時候可以不用指,如果指了就表示創建的新規則鏈只能在 table這張表裡的某些規則被引用或跳轉
iptables [-t table ] -X 刪除自定義的規則鏈 示例;iptables -X test iptables [ -t table ] -E 舊自定義鏈名 新定定義鏈名 示例;iptables -E test newtest iptables [ -t table ] -P chain target 為鏈指定默認策略,指定默認規則 示例;把系統當前默認鏈 Chain FORWARD (policy ACCEPT) 修改為DROPiptables -P FORWARD DROP
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
-v: 以詳細格式顯示
[root@www ~]#iptables -L -n -a --line-numbers
pkts bytes target prot opt in out source destination pkts: packets, 被本規則所匹配到的報文的個數; bytes: 被本規則所匹配到的所有報文的大小之和,會執行單位換算; target: 目標,即處理機制; prot: 協議,一般為{TCP|UDP|ICMP}; opt: 可選項 in: 數據包的流入接口; out: 數據包的流出接口; source: 源地址; destination: 目標地址;匹配條件 -j 處理機制
匹配條件:實例演示
注意:iptables是從上往依次匹配的,如果事先打開了某個端口服務,而後又想關閉的話,需要把寫的關閉規則寫到開啟的前面
-s:匹配原地址,可以IP,也可以網絡地址;可以使用!操作符取反, ! 172.16.0.0/16; -s 相當於 --src, 或 --sourceiptables -A INPUT -i eth1 -d 172.16.34.30 -p icmp -j REJECT
-i :數據報文流入的接口;通常只用於INPUT、FORWARD和PREROUTING
-o:流出的接口;通常只用於OUTPUT、FORWARD和POSTROUTING
iptables -A INPUT -i eth1 -d 172.16.100.7 -j ACCEPT iptables -A OUTPUT -o eth1 -s 172.16.34.30 -j ACCEPT 擴展匹配;隱含擴展;使用-p{tcp|udp|icmp}指定某特定協議後,自動能夠對協議進行的擴展; -p tcp --dport ;匹配的目標端口,可以是連續的多個端口; --sport; 匹配的源端口,也要是是連續的多個端口;