this is the current network that I have:

    gateway 62.x.x.x
    gateway ?


I basically want this:

Make specific NAT rules from the internet to specific internal servers depending on the port:

Connections incoming to port 80 must be redirected to

Connections incoming to port 3306 must be redirected to

and so on...

I also need one NAT rule to allow the servers in the subnet 192.168.1.x to browse the internet. I need to route the requests on eth0 to eth1 to be able to exit to internet.

Can I do this on the UBUNTU machine with iptables?


1 Answer 1


For the fist requirement you will need to use DNAT or destination natting it's used like this,

iptables -t nat -A PREROUTING -p tcp --dport TARGETPORT -j DNAT --to TARGET-IP:TARGETPORT


iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to

For Internet browsing you will need Source Natting:

iptables -t nat -A POSTROUTING -s -o eth0 -j SNAT --to-source GATEWAY-IP

Or you can use masquarding instead of Source Natting like this:

iptables -t nat -A POSTROUTING -o eth0 -s -j MASQUERADE

Also don't forget to open the needed ports on the servers.

  • It doesn't seem to work. The strange thing is that when i do a tracepath from CentOS, it says: 1: 2: ... why it doesn't go to ? To be sure that there is no firewall block I also did: iptables -A INPUT -p tcp -j ACCEPT iptables -A INPUT -p udp -j ACCEPT iptables-save But the CentOS instance cannot ping :( Nov 6, 2013 at 11:52
  • that is normal with tracepath is the centos interface, on ubuntu what is the default gateway ? use this route -n , normaly in linux the interface with highest number becomes the default gateway so in your case the default gateway will be eth1-gateway which is not what you want. if this the issue you need to do this : route delete -net gw CURRENT-GATEWAY route add -net gw PUBLIC-GATEWAY_IP
    – MohyedeenN
    Nov 6, 2013 at 12:14
  • Seems that i forget to tell, you also need to enable routing on the ubuntu machine, you do it like this: sysctl -w net.ipv4.ip_forward=1 or echo 1 > /proc/sys/net/ipv4/ip_forward
    – MohyedeenN
    Nov 6, 2013 at 12:18
  • Hello, I just noticed a problem: when I try to connect with ssh from the Ubuntu machine to the CentOS, it takes ages (more than 10s). This is the tracepath from Ubuntu to CentOS: root@vm-ubuntu:~# tracepath 1: vm-ubuntu.local 0.074ms pmtu 1500 1: 0.118ms !H 1: 0.111ms !H Resume: pmtu 1500 What could be the problem?! It seems like he is trying to reach by the wrong ethernet port.. Ping is fast: rtt min/avg/max/mdev = 0.111/0.115/0.121/0.004 ms Nov 6, 2013 at 23:38
  • umm, i face this issue on some machines, the good news is that it appears one machine and the others not, even though all on the same vlan and the same switch, not sure what is the cause, if you figured it out please let me know.
    – MohyedeenN
    Nov 7, 2013 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.