Super User is a question and answer site for computer enthusiasts and power users. 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

I have two network interfaces on a Linux PC, and I need to manually set the interface that a given process will use.

The program (Twinkle softphone) does not have a similar option, so I believe that it must be set externally.

How can I do it?

Edit: I'm not trying to make a server process bind to a specific interface, but rather to make a client program contact a server using a specific interface.

share|improve this question
clients use bind/connect as well, look at the bind.c.txt documentation of how to force ircII (an irc-client programm) to a given ip: 'Example in bash to use your virtual IP as your outgoing sourceaddress for ircII: BIND_ADDR="your-virt-ip" LD_PRELOAD=./ ircII' – akira Feb 3 '11 at 13:34
I found a different approach here, I hope it's helpful (I sure hope the described kernel policy routing is enabled by default nowadays):… – medigeek Jan 28 '12 at 23:52

you can replace code at runtime by the use of LD_PRELOAD (@windows you can use a similar technique called detours, quite fancy). what this does is to inform the dynamic linker to first load all libs into the process you want to run and then add some more ontop of it. you normally use it like this:

% LD_PRELOAD=./ ls

and by that you change what ls does.

for your problem i would try, which you can use like:

% BIND_ADDR="ip_of_ethX" LD_PRELOAD=./ twinkle

here is how you build it:

% wget -O bind.c
% gcc -nostartfiles -fpic -shared bind.c -o -ldl -D_GNU_SOURCE

a longer howto is

similar hacks and tools:

share|improve this answer
Wow, what the heck. +1 – sinni800 Feb 3 '11 at 12:04
Hi, this seems a really nice trick, but it does not work for me. I have two 3G modems that, when connected, open two interfaces (ppp0 and ppp1). If I try to force one of the two IPs, I always end up going out with the same interface (I see it because I have two instances of wireshark, one for each interface). I also removed the debug prints from bind.c and indeed I see that the "overloaded" library is loaded, so I don't know why it doesn't work. – Andrea Spadaccini Feb 9 '11 at 17:18
LD_PRELOAD is ignored if your effective UID is not the same as your real UID. – matthias krull Mar 24 '11 at 17:59
The force_bind project by Catalin M. Boie supports ipv6 – tolos Jan 12 at 5:52

ip netns can do this.

TL;DR: Create network namespaces, associate interfaces to them and then run "ip netns exec NAME cmd..."

Just check if your distro supports ip netns... (Backtrack 5r3 does not, whereas Kali does ;) )


#create netns
ip netns add myNamespace
#link iface to netns
ip link set eth0 netns myNamespace
#set ip address in namespace
ip netns exec myNamespace ifconfig eth0 up
#set loopback (may be needed by process run in this namespace)
ip netns exec myNamespace ifconfig lo up
#set route in namespace
ip netns exec myNamespace route add default gw
#force firefox to run inside namespace (using eth0 as outgoing interface and the route)
ip netns exec myNamespace firefox

Why is this better than binding the ip via LD_PRELOAD? Because LD_PRELOAD does not control the route that the processes uses. It will use the first route.

share|improve this answer
Please try to add more details to your answer. – Renju Chandran chingath May 6 '14 at 16:47
Details added... – snakerdlk Jun 16 '14 at 16:40
don't do this on the remote server if eth0 is the public network interface.. – ygrek Jul 21 '14 at 7:46
Wow, Linux still surprises me with simple solutions to strange problems... – Mark K Cowan Jun 8 '15 at 15:14
the last line should be ip netns exec myNamespace firefox – meuh Jun 20 '15 at 15:01

I don't think it is possible to force a process to use a certain interface.

However, I think you might be able to play with ipchain/iptables and force that a certain port your process is listening at will only get packets coming through a particular interface.

Useful HOWTO:

share|improve this answer
The two higher-voted posts prove otherwise. – Paul Gear Jul 6 at 7:36

Usually if a program has no option for setting listening interface, it's listening on ALL interfaces. (You can verify this with lsof -i).

Creating iptables firewall rules that drop incoming traffic pointed towards its ports on interfaces you don't want it to be visible on is the easiest thing to do.

share|improve this answer

Why would you want a program to use an interface other than the one connected to the server to talk to that server? And if the system isn't using the interface connected to a server to talk to that server, it's a system-level (routing table) issue and has nothing to do with which process happens to want to talk to that server.

Different servers on IP networks have different IP addresses. The kernel should know which interface to use to reach a particular IP address based on the routing table. If you're trying to talk to two different servers that have the same IP address, the system will get confused (because, among other things, it only indexes the connections internally by destination address). You can make that work, but it's a system-level fix involving putting one server in a separate logical network that's only connected to the machine through software NAT.

So if they have different IP addresses, use routes to select the correct interface. If they have the same IP address, you need to use NAT so that they appear to have different IP addresses to the system.

share|improve this answer
Firstly, there may be multiple valid routes between the client & server, but with different characteristics suitable for different traffic types; e.g. UMTS (Cellular Data) may cost money but has greater range than WiFi, but both are slower than a fiber connection. If the upstream providers do source filtering (or NAT), you have no choice but to send out the 'right' interface. Secondly, influencing routing is not the only reason to select a source address. Even when both addresses are on the same interface it can be useful to control which is bound to when initiating connections, as the server – user139322 Jun 11 '12 at 5:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .