Iptables

如何顯示iptables轉發規則?

  • December 24, 2021

我在 EC2 實例上執行了以下命令,將傳入埠 80 流量轉發到埠 8080:

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

它沒有輸出任何東西,但是當我看到轉發確實有效時。

我試圖在命令行中進行驗證,但我不知道如何:

$ iptables --list
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 --help,該-A選項需要一個鏈名作為參數,因此在我的情況下,鏈名將是PREROUTING

還根據iptables --help

--list-rules -S [chain [rulenum]]
Print the rules in a chain or all chains

但我得到的是:

$ iptables -S PREROUTING
iptables: No chain/target/match by that name.
iptables -S REDIRECT
iptables: No chain/target/match by that name.

如何實際列印我創建的轉發規則?

PREROUTING鏈位於 NAT 表 ( iptables -t nat) 中,因此您需要列出它才能看到它

iptables -t nat -nvL

您可以將其概括為所有四個表

for table in filter mangle nat raw
do
   echo
   echo "Rules for table $table"
   echo
   iptables -t "$table" --line-numbers -nvL
done

引用自:https://unix.stackexchange.com/questions/683775