歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> iptables使用范例詳解

iptables使用范例詳解

日期:2017/2/28 14:34:20   编辑:Linux教程

本文只寫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[-t table ]-N chain 創建一條自定義規則鏈
示例;iptables -N test

說明:[-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) 修改為DROP

iptables -P FORWARD DROP


iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

[root@www ~]# iptables -t filter -L -n -v Chain INPUT (policy ACCEPT 774 packets, 105K bytes) 進站默認允許
pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) 轉發 默認拒絕
pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 65 packets, 5404 bytes) 出站默認允許
pkts bytes target prot opt in out source destination -F: 清空鏈中的規則 規則有編號,在鏈中自上而下,從1開始; -L: list,列出表中的所有規則; -n: 數字格式顯示IP和Port

-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: 目標地址;
ptables [-t table] {-A|-D} chain rule-specification -A: append, 附加一條規則 rule-specification

匹配條件 -j 處理機制


匹配條件:實例演示

注意:iptables是從上往依次匹配的,如果事先打開了某個端口服務,而後又想關閉的話,需要把寫的關閉規則寫到開啟的前面

-s:匹配原地址,可以IP,也可以網絡地址;可以使用!操作符取反, ! 172.16.0.0/16; -s 相當於 --src, 或 --source
d : 匹配目標地址 示例;iptables -t filter -A INPUT -s 172.16.0.0/16 -d 172.16.34.30 -j ACCEPT iptables -t filter -A OUTPUT -s 172.16.34.30 -d 172.16.0.0/16 -j ACCEPT -p : 匹配協議,通常只使用{TCP|UDP|ICMP}三者之一;

iptables -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; 匹配的源端口,也要是是連續的多個端口;
Copyright © Linux教程網 All Rights Reserved