How do I set the priority of network connections in Ubuntu?

If a Ubuntu 11.04 machine is connected to WiFi and 3G simultaneously, how do I set the priority to let the applications (browser etc.) to use WiFi first? If that's not available, it should use the 3G.

Basically, I would like to set the order in which the network connections are used.

Edit: I am looking for an easier approach which would be useful for those who are just comfortable and not experts in Ubuntu/Linux.


I am surprised no one has mentioned the simplest command to do this: ifmetric. It can be installed using sudo apt-get install ifmetric. This command can be used to change the metric of any interface. The interface with lower metric is preferred for Internet.

To use this, first see the metrics using route command:

$ route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.42.0.1       0.0.0.0         UG    100    0        0 eth0
0.0.0.0         10.42.0.2       0.0.0.0         UG    600    0        0 wlan0

Here, eth0 has lower metric, so it will be preferred over wlan0. If you want to prefer wlan0, then lower its metric:

sudo ifmetric wlan0 50

Now, the routing table would look like:

$ route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.42.0.2       0.0.0.0         UG    50     0        0 wlan0
0.0.0.0         10.42.0.1       0.0.0.0         UG    100    0        0 eth0

Now Linux will be using wlan0 for Internet. The change will be reflected immediately.


Setting the metrics is how you change priorities. The higher metric is more "expensive" to use, so the OS will use the interfaces with the lowest metric if it needs to route traffic. In case the lower metric interface is shutdown it will use the higher metric interface since it is the only interface which can be used to route traffic towards that particular network/destination.

The metrics are specified in the file /etc/network/interfaces, link points to the documentation.

Use any text editor to edit the file, identify the networks, and just change the metric parameter and save. Reboot is the simplest way to reset all the values without getting into the geeky details of restarting the network services.


  1. Prioritising interfaces for general traffic is done by manipulating the routing metrics. Each route has associated parameters such as hop-counts and bandwidths. See the "metric" option in the man-page for route command.

    $ route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref     Use Iface
    0.0.0.0         192.168.1.254   255.255.255.255 U     0      0        0 eth0
    192.168.1.0     0.0.0.0         255.255.255.0   U     256    0        0 eth0
    
  2. Prioritising application access to network resources is often addressed by "traffic shaping" - I'd use a web search-engine to see if Ubuntu or the router can do that.


Footnote.

On MS Windows, but not on Linux, the netstat -nr command outputs the same information as route print. Including the routing metrics.


I haven't really tried it out, but NCD (Network Configuration Daemon - 1) can be used for this purpose. The site claims to make the network configuration easy. The syntax seems to be simple.

#Wait for some network connection. Prefer eth1 by putting it in front of eth0.
list("NET-eth1", "NET-eth0") pnames;

(1) - http://code.google.com/p/badvpn/wiki/NCD