Skip to content

Instantly share code, notes, and snippets.

@luisprox
Last active May 4, 2022 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisprox/d6c242585126e524c7a5e42ff11cb9ad to your computer and use it in GitHub Desktop.
Save luisprox/d6c242585126e524c7a5e42ff11cb9ad to your computer and use it in GitHub Desktop.
[Linux Port Forwarding] How to forward port request to another machine on the network #Linux #Debian #iptables
  1. Enable ip forward.
$ sudo echo 1 >/proc/sys/net/ipv4/ip_forward
  1. 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
  1. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment