经验分享:Linux 双网卡SNAT和DNAT映射本地端口出外网
A 机器
网卡1 公网IP
网卡2 内网
B 机器
网卡1 内网
如果想把公网端口P1 映射为内网B机器端口P2
iptables -t nat -A PREROUTING -d [A公网地址] -p tcp -m tcp --dport [公网端口] -j DNAT --to-destination [B内网IP]:[B内网端口]
iptables -t nat -A POSTROUTING -d [B内网IP] -p tcp -m tcp --dport [B内网端口] -j SNAT --to-source [A内网地址]
iptables -A FORWARD -o [A内网网卡] -d [B内网IP] -p tcp --dport [B内网端口] -j ACCEPT
iptables -A FORWARD -i [A内网网卡] -s [B内网IP] -p tcp –sport [B内网端口] -j ACCEPT
*nat
:PREROUTING ACCEPT [16232:957099]
:INPUT ACCEPT [16232:957099]
:OUTPUT ACCEPT [33:2307]
:POSTROUTING ACCEPT [33:2307]
-A PREROUTING -d 172.168.6.195/32 -p tcp -m tcp --dport 10000 -j DNAT --to-destination 172.168.6.202:5222
-A POSTROUTING -d 172.168.6.202/32 -p tcp -m tcp --dport 5222 -j SNAT --to-source 172.168.6.195
COMMIT
# Completed on Thu Jun 2 10:17:46 2016
# Generated by iptables-save v1.4.12 on Thu Jun 2 10:17:46 2016
*filter
:INPUT ACCEPT [1048988:102011563]
:FORWARD ACCEPT [7312:1019956]
:OUTPUT ACCEPT [744535:106322618]
-A FORWARD -d 172.168.6.202/32 -p tcp -m tcp --dport 5222 -j ACCEPT
COMMIT