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

45

18

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.

Praveen Sripati

Posted 2011-09-04T07:47:18.187

Reputation: 1 385

Answers

30

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.

shivams

Posted 2011-09-04T07:47:18.187

Reputation: 1 269

1Great answer. Thx (also for the route -n). I had to reboot after installing ifmetric for the command to change the metric – Thorsten Niehues – 2018-02-15T20:40:21.180

How to make this change permanent (I want to not use accidentally my iPhones hotspot) – Thorsten Niehues – 2018-02-15T20:41:52.553

2@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 – 2018-02-15T20:56:18.777

It works. But i can't access local machines after this (via browser e.g.). Any idea? Thanks. – tomasb – 2018-02-23T13:16:39.587

@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 – 2018-02-23T15:17:06.423

I tried this, but I am looking for a solution which lists the networks by SSID, so it's human-readable, and such that the settings are permanent. I found a better answer here

– 6005 – 2019-10-04T17:46:26.130

14

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.

harrymc

Posted 2011-09-04T07:47:18.187

Reputation: 306 093

Here is an interesting link that says ifconfig won't do it in these modern versions, and that the route command is the best way. I am not convinced I know enough to do it: https://stackoverflow.com/questions/10592612/set-network-interface-metric

– SDsolar – 2017-06-27T09:09:33.447

@SDsolar: It says that a route needs to exist for the metrics to be taken into account, which is only logical - no useful priority can assigned without some valid route to assign it to. But it is true that modern OS have started to prefer their own measured metrics above the specified ones, although methods still vary. – harrymc – 2017-06-27T10:03:15.620

Thank you for that pointer, @harrymc. It really is a different way to look at it than Windows does. Windows is interface-centric. Linux is much more like a Cisco router. But this machine has such a simple iptable that I can't figure out what to do. In my Cisco training things were a lot more explicit. Hmmm... – SDsolar – 2017-06-28T07:52:58.563

2Harry - 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 – 2011-09-09T14:50:03.210

1Adding 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 – 2011-09-09T15:11:12.553

When I ran 'sudo ifconfig eth1 metric 4' I got the following error 'SIOCSIFMETRIC: Operation not supported'. http://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 – 2011-09-09T17:06:09.787

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 – 2011-09-09T19:06:04.083

Faster network is nice to have but is costlier :) But, it might be expensive. http://goo.gl/K0O6j is too exhaustive for everyone to use it.

– Praveen Sripati – 2011-09-10T04:57:47.620

3The 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 – 2011-09-10T12:14:00.890

7

  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.

RedGrittyBrick

Posted 2011-09-04T07:47:18.187

Reputation: 70 632

1Really what an answer?? also netstat -nr does not show the metric. View answer from @shivams – Thorsten Niehues – 2018-02-15T20:43:04.210

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 – 2011-09-14T10:18:55.517

1Can you please give me the specific route command to do this? – Praveen Sripati – 2011-09-14T11:47:54.850

11@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. – 2013-11-15T17:58:15.907

3

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
0.0.0.0         10.16.87.254    0.0.0.0         UG    100    0        0 ens38
0.0.0.0         192.168.151.2   0.0.0.0         UG    101    0        0 ens33
10.16.86.64     0.0.0.0         255.255.255.248 U     100    0        0 ens38
10.16.87.254    0.0.0.0         255.255.255.255 UH    100    0        0 ens38
10.16.88.6      10.16.87.254    255.255.255.255 UGH   100    0        0 ens38
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens38
192.168.151.0   0.0.0.0         255.255.255.0   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 10.16.87.254 metric 102 dev ens38                                                              
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.151.2   0.0.0.0         UG    101    0        0 ens33
0.0.0.0         10.16.87.254    0.0.0.0         UG    102    0        0 ens38
10.16.86.64     0.0.0.0         255.255.255.248 U     100    0        0 ens38
10.16.87.254    0.0.0.0         255.255.255.255 UH    100    0        0 ens38
10.16.88.6      10.16.87.254    255.255.255.255 UGH   100    0        0 ens38
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens38
192.168.151.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

PnotNP

Posted 2011-09-04T07:47:18.187

Reputation: 263

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=10.16.87.254, 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 192.168.151.2 is unavailable. – WillC – 2017-12-31T02:28:10.193

3

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

Praveen Sripati

Posted 2011-09-04T07:47:18.187

Reputation: 1 385

I looked at it but couldn't figure it out. This shouldn't be this hard. – SDsolar – 2017-06-27T09:11:59.613

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 – 2011-09-11T13:47:50.227

0

[Update] As of Ubuntu 18.04 LTS (server), netplan is the default wrapper for network management. Configuring Netplan is done through a YAML file, by default /etc/netplan/01-netcfg.yaml (more details here).

Routing metric is defined by the "metric" option, which expects a positive integer (100 is the default value generally). Here's the example from the reference page:

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      addresses:
      - 10.0.0.10/24
      - 11.0.0.11/24
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
      routes:
      - to: 0.0.0.0/0
        via: 10.0.0.1
        metric: 100
      - to: 0.0.0.0/0
        via: 11.0.0.1
        metric: 100

The route with the lowest metric (path length) becomes the "preferred" gateway. (Use: sudo netplan try to enable changes)

Note that in a roaming environment (multiple connections, going on and off), you might want to set the optional (boolean) parameter to true (default is false):

network:
  version: 2
  ethernets:
    enred:
      dhcp4: yes
      dhcp4-overrides:
        route-metric: 100
    engreen:
      dhcp4: yes
      dhcp4-overrides:
        route-metric: 200
      # this is plugged into a test network that is often
      # down - don't wait for it to come up during boot.
      optional: true

Notice the slightly different syntax for route metric in the case of DHCP connections.

You can also use NetworkManager as a renderer, which I suppose (haven't tested myself yet) would let you see/edit that part of the configuration also via GUI tools.

renderer (scalar)

Use the given networking backend for this definition. Currently supported are networkd and NetworkManager. This property can be specified globally in networks:, for a device type (in e. g. ethernets:) or for a particular device definition. Default is networkd.

(The very last 'big' example on the reference page shows such a hybrid use of both renderers).

See also this question (askubuntu).

ΦDev

Posted 2011-09-04T07:47:18.187

Reputation: 1