A 机器
网卡1 外网 外网IP
网卡2 内网 内网IP1
B 机器
网卡1 内网 内网IP2
如果想把公网端口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
iptables-save >/etc/iptables
在rc.local中加入
iptables-restore </etc/iptables
网卡1 外网 外网IP
网卡2 内网 内网IP1
B 机器
网卡1 内网 内网IP2
如果想把公网端口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
iptables-save >/etc/iptables
在rc.local中加入
iptables-restore </etc/iptables
实现重启后自动加载
转载自:http://blog.sina.com.cn/s/blog_53b91ff30100tmte.html