Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Having multiple IP addresses, is it possible to make an arbitrary network application use only specific IP address when establishing connections to the outside world?

System-wide, routes can be used. Some applications are configurable for this. But is there a generic way?

Say, a browser should always use an IP bound to 'eth0', and a torrent-client should use an IP bound to 'eth1'.

Technically, this should explicitly pass a specific IP address to bind(2) call when a client socket is created.

share|improve this question
An app that makes connections to the outside world is a client app. And bind() is specific to server sockets. – Anonymous Dec 14 '09 at 21:08
You,re wrong: bind() is ok for client sockets, but is not obligatory: it's used when when an application desires to use a specific IP address or port for outbound connections. I'm 100% sure it works so :) – kolypto Dec 15 '09 at 0:10
up vote 1 down vote accepted

You could take advantage of the LD_PRELOAD environment variable and "function interposition" to modify the behavior of your network programs. See this article for an example, and Google function interposition for more information.

You could, for example, override the "socket" system call so that every socket() call was followed by an appropriate bind() operation.

NB: This technique can be lots of fun to play with (for example, I once wrote something that would let me "open" URLs for editing with arbitrary text editors), but can be tricky to get right.

share|improve this answer
Wow, great! That's exactly what I wanted, thank you :) – kolypto Dec 15 '09 at 17:53

My only suggestion would be to utilize iptables to redirect the traffic from one interface to another.

Here's a link to someone else who did something similar:

Essentially, the command was this:

iptables -t nat -A PREROUTING -p tcp -d --dport 80 -j REDIRECT --to-ports 8080

To completely steal the content (in case it disappears),

The above rule adds a PREROUTING command to your nat table, stating any TCP packets destined for on port 80 should be redirected to localhost port 8080. So breaking down the above example into template format, you have

 iptables -t nat -A PREROUTING -p PROTO -d DEST_IP --dport DEST_PORT -j REDIRECT --to-ports LOCAL_PORTS
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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