Redirect outbound traffic from one host to another

The task is to redirect locally ports.

So for example, when you go in browser to http://a.a.a.a:80, you would like to be always redirected to http://b.b.b.b:80.

-> http://a.a.a.a:80

------------         --------------
|   Host   |  --->   | a.a.a.a:80 |
------------         --------------

-> http://a.a.a.a:80

------------         --------------
|   Host   |  --->   | b.b.b.b:80 |
------------         --------------

What first comes to one’s mind? Right, to just add a line to /etc/hosts:

127.0.0.1    localhost
a.a.a.a      b.b.b.b

That’s absolutely wrong, because with hosts file you can’t map one IP address to another.

The right way is to alter iptables OUTPUT table:

iptables -t nat -A OUTPUT -p tcp -d a.a.a.a --dport 80 -j DNAT --to-destination b.b.b.b:80

The explanation of this command is here.

Bookmark the permalink.

Leave a Reply

Your email address will not be published.