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         UG    100    0        0 eth0         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         UG    50     0        0 wlan0         UG    100    0        0 eth0

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

  • Great answer. Thx (also for the route -n). I had to reboot after installing ifmetric for the command to change the metric – Thorsten Niehues Feb 15 '18 at 20:40
  • How to make this change permanent (I want to not use accidentally my iPhones hotspot) – Thorsten Niehues Feb 15 '18 at 20:41
  • 1
    @ThorstenNiehues: A quick solution coming to my mind to make it permanent is to add the ifmetric command to your crontab, by doing crontab -e and then add the line at the end: @reboot sudo ifmetric wlan0 50. – shivams Feb 15 '18 at 20:56
  • It works. But i can't access local machines after this (via browser e.g.). Any idea? Thanks. – tomasb Feb 23 '18 at 13:16
  • @tomasb: interesting doubt. That is expected, however, as changing the interface preference will bring you into the local network of the preferred interface. One straightforward way to access local machines would be to access them using a virtual machine. However, better solutions must exist. Let me think and reply. – shivams Feb 23 '18 at 15:17

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.

  • 2
    Harry - Thanks for the response - The network I am interested in eth1 is shown in the "ifconfig", but not in "/etc/network/interfaces". What are shown in "ifconfig" and what in "/etc/network/interfaces"? – Praveen Sripati Sep 9 '11 at 14:50
  • 1
    Adding eth0 to "/etc/network/interfaces" is more risky. You could try ifconfig as root to change the metric for the interface (check that it stays there after the boot). – harrymc Sep 9 '11 at 15:11
  • When I ran 'sudo ifconfig eth1 metric 4' I got the following error 'SIOCSIFMETRIC: Operation not supported'. goo.gl/UhXBJ says that 'Additionally, not all systems make use of the metric argument. ..... When configuring a Linux system, you add an explicit route command for each interface." Looks like there is no straight forward approach in Ubuntu like from a UI for a novice user. – Praveen Sripati Sep 9 '11 at 17:06
  • 2
    It looks like Linux decides itself on the fastest adapter after a quick speed test on all adapters. Metric is no longer supported on many distributions. You can still try and dictate things via "/etc/network/interfaces", but I don't know how successful you will be. See also this question. – harrymc Sep 9 '11 at 19:06
  • 3
    The simplest solution, which everyone does, is just to turn off the interface you don't want to use, or to limit it to "Use this connection only for resources on its network" (if you also have your printer or whatever on that same router). – harrymc Sep 10 '11 at 12:14
  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 U     0      0        0 eth0   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.


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

  • In the man page for route "Metric The 'distance' to the target (usually counted in hops). It is not used by recent kernels, but may be needed by routing daemons.". I am not sure if on a regular Ubuntu Desktop the Metric parameter will have any effect. – Praveen Sripati Sep 14 '11 at 10:18
  • 1
    Can you please give me the specific route command to do this? – Praveen Sripati Sep 14 '11 at 11:47
  • 10
    @PraveenSripati - I don't believe you gave a 100 of your hard earned rep for such an answer - technically correct, but does not help you or me. :-( I am looking for a specific command as well. – Lord Loh. Nov 15 '13 at 17:58
  • 1
    Really what an answer?? also netstat -nr does not show the metric. View answer from @shivams – Thorsten Niehues Feb 15 '18 at 20:43

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

  • You're right, my NCD software does this nicely; the example there works as-is. I should note however that the pnames list is in fact just an argument to the multidepend() statement on the next line - this is the one which implements the priorities. The mechanism behind this is that when eth1 comes up when eth0 is already up, it executed multiprovide("NET-eth1");, because NET-eth1 is in front of NET-eth0, multidepend() will go down and come back up immediately, but exposing variables from the eth1 process instead of eth0. – Ambroz Bizjak Sep 11 '11 at 13:47
  • I looked at it but couldn't figure it out. This shouldn't be this hard. – SDsolar Jun 27 '17 at 9:11

This is all because of route metrics. You want to delete the default route with lowest metric and then reinstate the old route with higher metric. Please follow commands below.

Lets say your routing table looks like this:

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface         UG    100    0        0 ens38         UG    101    0        0 ens33 U     100    0        0 ens38 UH    100    0        0 ens38 UGH   100    0        0 ens38     U     1000   0        0 ens38   U     100    0        0 ens33

Now delete the default gateway

# route delete default gateway

Now reinstate the older default gateway (note that metric in this case is higher 102 than current default route 101)

# route add default gw metric 102 dev ens38                                                              
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface         UG    101    0        0 ens33         UG    102    0        0 ens38 U     100    0        0 ens38 UH    100    0        0 ens38 UGH   100    0        0 ens38     U     1000   0        0 ens38   U     100    0        0 ens33
  • This works. Note for any other novices (I had to read this a couple of times to follow what was happening): step 1 above removes the entry for Gateway=, which is the UG with the lowest Metric , and step 2 puts it back with a higher Metric so it is still available as backup in case the is unavailable. – WillC Dec 31 '17 at 2:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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