Forward port to local ip + port

12,886

What you're looking for is called NAT.

First we want to enable portforwarding:

sysctl net.ipv4.ip_forward=1

Now we should add a rule that forwards all incoming traffic on 8080:

iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.33.99:80

Then all that is left, is iptables to masquerade

iptables -t nat -A POSTROUTING -j MASQUERADE

What is masquerade?

Share:
12,886

Related videos on Youtube

www.data-blogger.com
Author by

www.data-blogger.com

Updated on September 18, 2022

Comments

  • www.data-blogger.com
    www.data-blogger.com over 1 year

    On my Ubuntu 14.04 I have a service running on a local IP (192.168.33.99) and port 80. In my network, my Ubuntu computer has IP 192.168.2.3. Now I want to redirect all traffic on 192.168.2.3:8080 to 192.168.33.99:80 (note that this is a local IP on the Ubuntu machine). How can I do that?

  • www.data-blogger.com
    www.data-blogger.com about 8 years
    Thank you! I now get the following message after trying to reach 192.168.2.3:8080 -> ERR_CONNECTION_REFUSED. How can I resolve this?
  • rowan
    rowan about 8 years
    @Kevin - are you able to reach 192.168.33.99:80? Also please post output of iptables -nvL
  • www.data-blogger.com
    www.data-blogger.com about 8 years
    Yes @FAT32, but I am not able to reach the 192.168.2.3:8080
  • rowan
    rowan about 8 years
    @Kevin - Please post output of iptables -nvL
  • www.data-blogger.com
    www.data-blogger.com about 8 years
    Ah, it is working (I can reach it through the network) @FAT32, but not on my local machine, thank you!
  • rowan
    rowan about 8 years
    @Kevin - haha, that makes sense! Cheers =) (I was actually stressing out that I made a mistake, been a while since I did this haha)
  • toing_toing
    toing_toing almost 5 years
    this wonderful answer would only be more complete if you could add how to edit or change a route :)