Yesterday I was requested to redirect the traffic to the 80 TCP port of an host to the TCP 8080 port of a second host just for some hours. Thanks to iptables it was been very easy.
First it’s better to enable port forwarding:
# echo 1 >/proc/sys/net/ipv4/ip_forward
Then here comes some iptables commands and rules:
# iptables -F
# iptables -X
# iptables -t nat -F
# iptables -t nat -X
# iptables -t mangle -F
# iptables -t mangle -X
# iptables -P INPUT ACCEPT
# iptables -P FORWARD ACCEPT
# iptables -P OUTPUT ACCEPT
# iptables -t nat -A PREROUTINGÂ -p tcp -m tcp -d HOST1 –dport PORT1 -j DNAT –to-destination HOST2:PORT2
# iptables -t nat -A POSTROUTING -p tcp -d HOST2 –dport PORT2 -j MASQUERADE
If You like, just download this simple shell script.
To display the nat rule:
# iptables -t nat -n -L
many thanks to cyberciti.biz and Chris Siebenmann‘s wiki.