1. Overview

Often, embedded devices have multiple network interfaces available at the same time, such as network routers where both wired and wireless interfaces are available. Such devices should be able to automatically select a preferred network interface. To achieve this on a Linux machine, each interface has a metric parameter, which shows how costly it is to use the interface.

In this tutorial, we’ll learn how to permanently change metric parameter on a Linux machine. We’ll look at several ways of doing this based on the interface managing tool our device uses.

2. Check the Interface Managing Tool

To change the metric value, it’s essential to know how interfaces are managed.

The two most commonly used managing tools are the Network Manager and the /etc/network/interfaces file.

Let’s check which managing tool is utilized in our system.

Firstly, we can use the nmcli device command, which shows if the Network Manager is active:

$ nmcli device
DEVICE         TYPE      STATE         CONNECTION         
eth0           ethernet  connected     Wired connection 1 
wlan0          wifi      connected     MyWifiSsid
...

As we can see, both wired and wireless interfaces have the connected state, which means that nmcli manages them.

Suppose we don’t see any connected interfaces, or we’re getting an error. In that case, this typically means that the system uses the /etc/network/interfaces file where all our active interfaces are defined. The example of the /etc/network/interfaces file is as follows:

auto eth0 
iface eth0 inet dhcp 

auto wlan0 
iface wlan0 inet dhcp 
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Here, we can see that the eth0 and wlan0 interfaces are defined.

Now that we know which managing tool we have, we can change the interface metric value.

3. Get Metric Value

Let’s get the initial metric value for our network interfaces. For that, we use the ip route command:

$ ip route
default via 10.54.4.1 dev eth0 proto dhcp metric 100 
default via 10.54.14.1 dev wlan0 proto dhcp metric 400 
...

This is the routing table for interfaces eth0 and wlan0. At the end of each line, we can see a metric value.

The system selects the interface with a lower metric number. For example, right now, the interface eth0 has metric 100, while wlan0 has metric 400. So, eth0 has a higher priority over wlan0.

Let’s now look at how we can change the metric value.

4. Systems With nmcli

If our system uses nmcli to manage the network, we can use the nmcli command to change the metric value of the wlan0 interface:

$ sudo nmcli connection modify 'MyWifiSsid' ipv4.route-metric 50

Above, MyWifiSsid is the nmcli name of the wlan0 network, while the number 50 is the new metric value.

Then, we finish up by applying the changes:

$ sudo nmcli connection up 'MyWifiSsid'
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)

The connection with a new metric value is now active.

If we check the routing table, we should be able to see the new metric value for wlan0:

$ ip route
default via 10.54.14.1 dev wlan0 proto dhcp metric 50 
default via 10.54.4.1 dev eth0 proto dhcp metric 100
...

Indeed, the wlan0 interface now has a metric value of 50.

Moreover, if we try to reboot the system, the new metric value will persist.

5. Systems With /etc/network/interfaces File

In case our Linux system uses the /etc/network/interfaces file to manage the interfaces, we can add the metric <number> keyword to the file as in the following example:

auto wlan0
iface wlan0 inet dhcp
    metric 50
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Now, the wlan0 interface should have a metric value of 50.

To apply the changes, we restart the networking service:

$ sudo service networking restart

If we see no errors, then our interface has been updated with a new metric value.

6. Conclusion

In this article, we’ve learned how to permanently change the metric value of a network interface.

Firstly, we looked at the nmcli-managed systems. Secondly, we learned how to do this for the systems managed by the /etc/network/interfaces file.

Comments are closed on this article!