Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have two different network interfaces, connected to 2 networks. One is an eth0 and the other one a wlan0. How can I tell a software to use only a specific interface?

Basically I want Firefox to use eth0 because it is the university lan network and I have to go to intranet sites, the other one is a wifi network open to the internet and I want to bind it to Chrome.

I'm working and I need to use intranet. So eth0 is my choice but eth0 is an intranet without internet access (obviously). Since I want internet access I'm connected to wlan0 (university wifi for students).

The problem is if I have both connected sometimes the browser looks for www.stackoverflow.com using eth0. So I wanted to assign a browser to use only a specific interface.

share|improve this question
    
Isn't routing a better solution for your problems?. I mean, that connections to ubuntu.stackexchange.com should use the same interface from any program. –  Javier Rivera Sep 29 '10 at 8:58

3 Answers 3

up vote 16 down vote accepted

You cannot bind client software to specific network interfaces, but you can tell the kernel that you only want to use one network interface for some IP addresses and the other one for everything else. This is called "routing", and can be configured using the commands /sbin/route and /sbin/ip.

If I read your question correctly, you want to connect to intranet IP addresses using interface eth0 and to the Internet using interface wlan0.

If you run the command ip route list, you should see an output like the following (numbers will be different, and also you can have more lines in it):

$ ip route list
10.60.44.0/25 dev eth0  proto kernel  scope link  src 10.60.44.39  metric 1 
192.168.80.0/21 dev wlan0  proto kernel  scope link  src 192.168.84.122  metric 2 
[...]
default via 10.60.44.1 dev eth0  proto static 

The first two lines tell you about the networks connected to interfaces eth0 and wlan0: network traffic directed to computers on those networks will be directly sent to them through the corresponding interface.

The last line tells you what the "default route" is: if your computer wants to talk to a computer on a network it is not attached to (e.g., the stackoverflow.com server), it will route traffic via eth0, realying through host 10.60.44.1 (called the "default gateway").

So, to route Internet traffic thorugh wlan0 you should ensure that the last line in the ip route list output reads something like:

default via A.B.C.D dev wlan0 proto static

where A.B.C.D is the IP address of the gateway on the wireless LAN. If the output does not contain "dev wlan0", you can change it with the command:

sudo ip route change to default dev wlan0 via A.B.C.D

You can find out the correct A.B.C.D for wlan0 in two ways:

  1. Look into directory /var/lib/dhcp3/: you should find some dhclient-...-wlan0.lease files. Open the most recent one and search for a line with the string option router in it: the rest of the line tells you the IP address A.B.C.D.

  2. Ask your local network administrators. (Probably the best thing to do, anyway.)

With this configuration, you should be able to:

  • browse the Internet through wlan0
  • browse your Intranet through eth0, provided it is on a single network.

If your intranet spans multiple networks, then you will need to add routes for them - and this is definitely something that requires you to interact with the local network admins. :-)

share|improve this answer
    
Just out of curiosity: what if I want to bound dns (to include subdomains) rather than IP addresses? –  dierre Sep 29 '10 at 16:15
    
@dierre In short: you can't, routing is based on IP addresses. The long story begins by telling that routing is a network layer 3 thing, so it won't even know about DNS names, whose resolution happens further up the networking protocol stack... –  Riccardo Murri Sep 29 '10 at 18:40
    
yeah yeah, I didn't mean with routing. I mean in general. Can it be done? Binding DNS to network interfaces? –  dierre Sep 29 '10 at 19:28
    
@dierre What exactly do you want to do? Making a DNS server answer only on a specific network interface? Or having a DNS client (i.e., DNS resolution) use only a selected interface? –  Riccardo Murri Sep 29 '10 at 20:18
1  
@Riccardo Murri: ubuntu.stackexchange.com/questions/4988/… et voilà –  dierre Sep 30 '10 at 19:48

"ip netns" creates network namespaces. You can then create virtual interfaces (ip link add... veth) and associate them to the namespaces.

The namespaces can be configured to use different routes for example (thus using different interfaces)

Then you can run commands in that namespace, that will use the created namespace.. "ip netns exec NAME cmd..."

Source: http://manpages.ubuntu.com/manpages/saucy/en/man8/ip-netns.8.html

share|improve this answer

I know I know, late as usual.

Wow, I love it (Murri's answer), clear as a bell, but let's go further...

So dierre wants to bind an application to an interface. Murri says "not really", but you can change the route.. !and that's it!

To bind an app to an interface one would bind an app launch event to a shell script that alters the route to use the desired interface, eg: wlan0, thus 'binding the interface to the app'.

We can go further, and we will since we're POWER USERS!, yup, we can actually look to determine which applications we use together at which times and which interface we would look to use at those times. In short we can determine which interface we use for which activity: web developement, word processing, graphic editing, movie watching etc... and pre-emptively resolve application interface conflicts.

When we launch our context the route can be changed to use the interface we want. When we switch to another context that uses another interface, presto, the app or here context launch scripts will change the route to use that interface.

Wow, dierre and Riccardo have lent us to a path where we now have auto activity context based interface switching with application interface conflict resolution.

Neat.

Of course some scripts have to be written but that's a simple matter for (shell script guru's name here).

share|improve this answer

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.