Iptables 实现端口转发

目标

将本机的7088端口转发至其他主机

主机IP:1.1.1.1

目标主机IP和端口:2.2.2.2:1521

方法

echo "1" > /proc/sys/net/ipv4/ip_forward 

vi  /etc/sysctl.conf

将 net.ipv4.ip_forward = 0 改成:

net.ipv4.ip_forward = 1

syscty -p


例1:

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 7088 -j DNAT --to-destination 2.2.2.2:1521

iptables -t nat -A POSTROUTING -p tcp -m tcp --dport 1521 -j SNAT --to-source 1.1.1.1

iptables -t nat -A POSTROUTING -j MASQUERADE

service iptables save

service iptables restart



例2:

192.168.0.7 转发机

113.108.16.119 腾讯企业邮箱服务器IP

邮件客户端配置:

POP3服务器:192.168.0.7 端口:1010

SMTP服务器:192.168.0.7 端口:2525


iptables -t nat -A PREROUTING -d 192.168.0.7 -p tcp -m tcp --dport 1010 -j DNAT --to-destination 113.108.16.119:110
iptables -t nat -A PREROUTING -d 192.168.0.7 -p tcp -m tcp --dport 2525 -j DNAT --to-destination 113.108.16.119:25

iptables -t nat -A POSTROUTING -d 113.108.16.119 -p tcp -m tcp --dport 25 -j SNAT --to-source 192.168.0.7
iptables -t nat -A POSTROUTING -d 113.108.16.119 -p tcp -m tcp --dport 110 -j SNAT --to-source 192.168.0.7

你可能感兴趣的:(iptables)