Linux下iptables可以用来做端口转发
示例:
将本机的8080端口转发至其他主机,主机IP:192.168.1.12,目标主机IP和端口:192.168.1.13:8088,规则如下;
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8080 -j DNAT --to-destination 192.168.1.13:8088
iptables -t nat -A POSTROUTING -p tcp -m tcp --dport 8088 -j SNAT --to-source 192.168.1.12
echo 1 > /proc/sys/net/ipv4/ip_forward #开启iptables forward转发功能。
tcp范围端口映射:
iptables -t nat -A PREROUTING -p tcp --dport 2000:2500 -j DNAT --to 192.168.66.2:2000-2500
远程转发:
iptables -A FORWARD -p tcp --dport 80 -j ACCEPT
iptables -A FORWARD -p tcp --sport 80 -j ACCEPT
#把访问192.168.0.12的80端口转发到192.168.0.13的80端口
iptables -t nat -A PREROUTING -p tcp -i eth0 -d 192.168.0.12 --dport 80 -j DNAT --to-destination 192.168.0.13:80
#配置地址伪装
iptables -t nat -A POSTROUTING