When x.x.x.x makes a request on port 80 to y.y.y.y I would like the request to be redirected to z.z.z.z:80. Is it possible to do this by adding rules to iptables on y.y.y.y?

link|improve this question

45% accept rate
feedback

2 Answers

You probably want to use DNAT to rewrite the destination IP address, something like this:

iptables -t nat -A PREROUTING -p tcp -s x.x.x.x -d y.y.y.y --dport 80 -j DNAT --to-destination z.z.z.z:80

link|improve this answer
2  
For this to work you also need to enable IPv4 forwarding. echo 1 > /proc/sys/net/ipv4/ip_forward – Kenny Rasschaert Nov 30 '11 at 13:36
Plus, if x.x.x.x, y.y.y.y, and z.z.z.z are in the same subnet, a -j SNAT rule will also be necessary. – pepoluan Nov 30 '11 at 14:11
I haven't done anything with SNAT yet - maybe I need to, x.x.x.x and y.y.y.y are on the same network? Initially the connection times out and after echo 1 > /proc/sys/net/ipv4/ip_forward it fails more quickly. – James Nov 30 '11 at 15:02
feedback

Final solution as follows:

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -p tcp -s x.x.x.x -d y.y.y.y --dport 80 -j DNAT --to-destination z.z.z.z:80
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

where eth0 is outside interface on y.y.y.y.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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