Thursday, September 20, 2012

IP Forwarding in Linux

How to forward connection from one interface to another under linux

 

 Basicly you need to figure an external IP address on the "outside" interface and add iptables rule:

 

#Flush all iptable chains and start afresh
sudo iptables -F

 

 iptables -t nat -A PREROUTING -p tcp -d X.X.X.X --dport 8080 -j DNAT --to Y.Y.Y.Y:8080

 

 

X.X.X.X is the external address while Y.Y.Y.Y is the internal one running webserver. In that scenario you also have to make sure you are allowing the traffic in the forward chain:
iptables -A FORWARD -p tcp -d Y.Y.Y.Y --dport 8080 -j ACCEPT
Your box has to have forwarding enabled for this:
sysctl net.ipv4.ip_forward=1 
or
echo 1 > /proc/sys/net/ipv4/ip_forward