Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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=./bind.so 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): kindlund.wordpress.com/2007/11/19/… –  medigeek Jan 28 '12 at 23:52

5 Answers 5

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: http://tldp.org/HOWTO/IPCHAINS-HOWTO.html

share|improve this answer

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=./mylib.so ls

and by that you change what ls does.

for your problem i would try http://www.ryde.net/code/bind.c.txt, which you can use like:

% BIND_ADDR="ip_of_ethX" LD_PRELOAD=./bind.so twinkle

here is how you build it:

% wget http://www.ryde.net/code/bind.c.txt -O bind.c
% gcc -nostartfiles -fpic -shared bind.c -o bind.so -ldl -D_GNU_SOURCE

a longer howto is http://daniel-lange.com/archives/53-Binding-applications-to-a-specific-IP.html

similar hacks and tools:

share|improve this answer
5  
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
2  
LD_PRELOAD is ignored if your effective UID is not the same as your real UID. –  matthias krull Mar 24 '11 at 17:59

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
2  
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

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 ;) )

IN MORE DETAILS:

#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 192.168.0.10/24 up
#set loopback (may be needed by process run in this namespace)
ip netns exec myNamespace ifconfig lo 127.0.0.1/8 up
#set route in namespace
ip netns exec myNamespace route add default gw 192.168.0.1
#force firefox to run inside namespace (using eth0 as outgoing interface and the route)
ip netns exec 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
1  
Please try to add more details to your answer. –  Renju Chandran chingath May 6 at 16:47
    
Details added... –  snakerdlk Jun 16 at 16:40
    
don't do this on the remote server if eth0 is the public network interface.. –  ygrek Jul 21 at 7:46

Your Answer

 
discard

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.