如何將 POSTROUTING / SNAT 與 firewalld 一起使用?
我嘗試在我的 CentOS-7-Router 上設置帶有 firewalld 的 SNAT,如此處所述,加上Karl Rupps 的解釋,但我最終像Eric一樣。我還閱讀了一些其他文件,但我無法讓它工作,因此我的客戶端 IP 被翻譯成另一個源 IP。
兩個都
firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -p tcp -o enp1s0 -d 192.168.15.105 -j SNAT --to-source 192.168.25.121
或者
firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -p tcp -s 192.168.15.105/32 -j SNAT --to-source 192.168.25.121
給出了“成功”。我做一個
firewall-cmd --reload
之後。但是如果我嘗試檢查帶有iptables -t nat -nvL POSTROUTING
規則的表並沒有列出。但是,如果我再次應用上述規則之一,firewalld 會案例如警告我
Warning: ALREADY_ENABLED: rule '['-p', 'tcp', '-o', 'enp1s0', '-d', '192.168.15.105', '-j', 'SNAT', '--to-source', '192.168.25.121']' already is in 'ipv4:nat:POSTROUTING'
- 但源 IP 192.168.15.105 沒有 SNAT 功能被偽裝成 192.168.45.121 正在工作。也許有人可以解釋我做錯了什麼?
經過幾個小時的掙扎,我仍然堅持 DNAT/SNAT。我現在只使用 iptables:
1.)
iptables -t nat -A PREROUTING -p tcp --dport 1433 -i enp1s0 -d 192.168.25.121 -j DNAT --to-destination 192.168.15.105
和
2.)
iptables -t nat -A POSTROUTING -p tcp --sport 1433 -o enp1s0 -s 192.168.15.105/32 -j SNAT --to-source 192.168.25.121
所以
iptables -t nat -nvL PREROUTING
顯示:pkts bytes target prot opt in out source destination 129 12089 PREROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0 129 12089 PREROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 129 12089 PREROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 DNAT tcp -- enp1s0 * 0.0.0.0/0 192.168.25.121 tcp dpt:1433 to:192.168.15.105
和
iptables -t nat -nvL POSTROUTING
顯示:pkts bytes target prot opt in out source destination 97 7442 POSTROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0 97 7442 POSTROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 97 7442 POSTROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 SNAT tcp -- * enp1s0 192.168.15.105 0.0.0.0/0 tcp spt:1433 to:192.168.25.121
一切順利,這裡有一些更好的解釋:
PREROUTING (resp. POSTROUTING) 不是在 (resp. after) ip_forwarding 從內部到外部介面之前完成嗎?
#!/bin/bash # Assuming that your Linux box has two NICs; eth0 attached to WAN and eth1 attached to LAN # eth0 = outside # eth1 = inside # [LAN]----> eth1[GATEWAY]eth0 ---->WAN # Run the following commands on LINUX box that will act as a firewall or NAT gateway firewall-cmd --query-interface=eth0 firewall-cmd --query-interface=eth1 firewall-cmd --get-active-zone firewall-cmd --add-interface=eth0 --zone=external firewall-cmd --add-interface=eth1 --zone=internal firewall-cmd --zone=external --add-masquerade --permanent firewall-cmd --reload firewall-cmd --zone=external --query-masquerade # ip_forward is activated automatically if masquerading is enabled. # To verify: cat /proc/sys/net/ipv4/ip_forward # set masquerading to internal zone firewall-cmd --zone=internal --add-masquerade --permanent firewall-cmd --reload firewall-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -o eth0 -j MASQUERADE firewall-cmd --direct --add-rule ipv4 filter FORWARD 0 -i eth0 -o eth1 -j ACCEPT firewall-cmd --direct --add-rule ipv4 filter FORWARD 0 -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT firewall-cmd --reload