- Enable ip forward.
$ sudo echo 1 >/proc/sys/net/ipv4/ip_forward
- Add forwarding rule.
# this example forwards port 8080 to machine ip 122.164.34.240, port 80
$ sudo iptables -t nat -A PREROUTING -p tcp –dport 8080 -j DNAT –to-destination 122.164.34.240:80
- Add masquerade so iptables rewrite the origin of connections so the final destination knows the real requester.
# this command masquerade all routings
$ sudo iptables -t nat -A POSTROUTING -j MASQUERADE
# thisl will masquerade only a specific routing
$ sudo iptables -t nat -A POSTROUTING -p tcp -d 122.164.34.240 –dport 80 -j MASQUERADE