80

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.

9 Answers9

90

I am surprised no one has mentioned the simplest command to do this: ifmetric. It can be installed using sudo apt 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.

Tim
  • 2,377
shivams
  • 1,804
20

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
  • 498,455
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.

5

Year 2022: Run nm-connection-editor

Ever since the (network) settings were redesigned in GNOME, some functionality cannot be accessed anymore from the new settings interface. To use the old one, which exposes more options, run nm-connection-editor.

Setting connection priority in nm-connection-editor

Credits to: https://askubuntu.com/a/1061981

tkazik
  • 151
4

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
  • 395
4

[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
  • 41
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

0

Here, when both are connected, and the primary one does not have internet, is not using the second connection.

It is possible to use ifmetric but must be auto started.

If wi-fi internet is failing, to set the wi-fi connection without looking for internet, in Ubuntu: Settings > Wi-Fi > Under "Visible Networks" use the wheel at the connection > IPv4 > Under "Routes" check "Use this connection only for resources on its network". To test it is right: route -n and wi-fi interface (Iface, here is wlp3s0) will have one less rule (the Destination 0.0.0.0 was not added).

Sam
  • 3
0

This network connection priotity, as said in most of the other answers, is set via the metric.

You can see this via routes command; here's mine for example:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    100    0        0 enxdeb70e78396c
default         tomoya          0.0.0.0         UG    101    0        0 enx2225cf7cf844
default         _gateway        0.0.0.0         UG    600    0        0 wlp0s20f3

Here i have three network connections enxdeb70e78396c is my cable connection, enx2225cf7cf844 is my tethered phone and wlp0s20f3 is my wifi connection

so in my case it will first use the cable connection.

on Ubuntu you can change the metric using network-manager. But to do this you'll need the connection name, this can be found using the nmcli tool as follows:

$ nmcli connection show
NAME                UUID                                  TYPE       DEVICE          
Wired connection 1  d1e5e9af-06b5-3d49-a773-e80748773ed8  ethernet   enxdeb70e78396c 
myWifi              f6dad8c9-e1ca-4018-a22b-890c0fbe257a  wifi       wlp0s20f3       
Wired connection 2  0c4c49b8-4881-307f-ac0d-10e9f3f74de1  ethernet   enx2225cf7cf844 

as you can see my cable connection is called Wired connection 1

to use my tethered phone instead i'll have to increase the metric of my cable connection to be higher than my tethered phone. This can be done with the following command:

nmcli connection modify "Wired connection 1" ipv4.route-metric 102

to apply the change i have to bring the connection down and back up as follows:

nmcli connection down "Wired connection 1"
nmcli connection up "Wired connection 1"

now if i check my routes again i get

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         tomoya          0.0.0.0         UG    101    0        0 enx2225cf7cf844
default         _gateway        0.0.0.0         UG    102    0        0 enx4467522ea5c9
default         _gateway        0.0.0.0         UG    600    0        0 wlp0s20f3

so now my all my traffic go through my tethered phone

Fuseteam
  • 103