標籤:2.0-iptables詳解
iptables 串連netfilter的工具
3張表
filter 過濾進出資料包 預設3個鏈input output forward
nat 網路位址轉譯 預設3個鏈prerouting postrouting output
mangle 給資料做標記,根據標記繼續操作 預設5個鏈input output forward prerouting postrouting
常用的表和鏈有filter的input和output
iptables -nvL 查看filter表的規則,不指定-t預設為filter
iptables -t nat -nvL 查看nat表的規則
iptables -t mangle -nvL 查看mangle表的規則
iptables -Z 規則匹配計數器清零
iptables -F 清空filter表規則
service iptables save 儲存iptables規則到設定檔/etc/sysconfig/iptables
iptables-save > 1.txt 規則另存到指定的檔案
iptables-restore < 1.txt 通過備份檔案,恢複規則
iptables -t filter -I INPUT -p tcp --dport 80 -s 1.1.1.1 -j DROP
-I插入規則到指定序號,不指定,預設最開始
iptables -t filter -I INPUT 3 -p tcp --dport 80 -s 1.1.1.1 -j DROP
-A插入規則到最末尾 iptables -t filter -A INPUT -p tcp --dport 80 -s 1.1.1.1 -j DROP
-D刪除指定的規則 iptables -t filter -D INPUT -p tcp --dport 80 -s 1.1.1.1 -j DROP
-j有3種動作:DROP直接丟棄 REJECT檢查後拒絕 ACCEPT接受
注意:如果針對連接埠操作,必須指定協議類型tcp/udp,否則報錯。
iptables -I INPUT -p tcp -s 1.1.1.1 -j ACCEPT 不指定連接埠,預設為所有連接埠。
iptables -P INPUT DROP 設定filter表INPUT鏈的預設策略為拒絕(總開關)
如果iptables規則太多,可用規則序號來刪除:
iptables -nvL --line-number 顯示filter表的規則序號
iptables -D INPUT 3 刪除filter表中序號是3的規則
2.0-iptables詳解