38

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.

22

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.

  • 1
    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
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.

  • 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
7
+100
  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.

  • 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
  • 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. 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
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

  • 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
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
  • 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 Dec 31 '17 at 2:28
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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