本文說明如何用 iptables 為內部網路建立 NAT 上網,並將外部連接埠映射到內部 Web 伺服器。
伺服器需有兩張網卡,一張對外,一張對內,並開啟 ip_forward。
在 nat 表的 PREROUTING 鏈加入 DNAT 規則,將外部 80 連接埠導向內部 Web 主機。此規則需放在透明代理規則之前。
在 POSTROUTING 使用 SNAT,讓內部網段透過公網 IP 出去。filter 表則可加入基本防護,例如阻擋 ping 與不必要連接埠。
iptables NAT 是否支援連接埠多工
端口映射只寫 DNAT 不夠,還要記得 SNAT
iptables NAT 表實戰:SNAT、DNAT 與埠映射完整配置
iptables 端口映射實戰:從 netfilter 流程到 DNAT 轉發
Linux 用 iptables 做 NAT 上網與連接埠映射的指令整理
Linux 使用 iptables 設定連接埠映射
iptables NAT 與端口映射完全實作(轉載)
NAT 連接埠映射:讓外網存取內網服務的實作思路