Black Friday 2025 – NPI EA (cat = Baeldung on Linux)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

In this tutorial, we’ll explore several ways to permanently change the network routing metric value on a Linux machine.

2. Introduction to Network Routing Metric

In networking, the transfer of data from one device to another consists of a series of steps. Furthermore, the data goes through different routers, networks, and networking devices. When data passes through different networks, routers that are part of the networking layer utilize IP addresses to find the best route for the data to reach the destination.

Moreover, the routers store all the routing information in routing tables. As soon as data arrives, the routers run a routing algorithm that calculates each path’s metric value. With the help of the routing metric value, the routing determines an efficient path for the data to travel to the destination.

Furthermore, we use various types of routing metrics in networking. Popular metrics are hop count, bandwidth, latency, and network load.

Often, embedded devices have multiple network interfaces available simultaneously, such as network routers with both wired and wireless interfaces. Such devices should be able to automatically select a preferred network interface. To achieve this on a Linux machine, each interface has a metric value that shows how costly it is to use the interface.

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

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

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

6. 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, our interface has been updated with a new metric value.

7. Using ip route command

Now, regardless of the network interface used by the system, we can utilize the ip route command to change the routing metric value in Linux.

First, let’s view all the existing routes that are already configured on the system:

$ ip route show
default via 10.0.2.2 dev enp0s3 proto dhcp src 10.0.2.15 metric 100 
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100 
169.254.0.0/16 dev enp0s3 scope link metric 1000

The output displays three routes: default, directly connected, and link-local address routes. When the IP address of the data packet doesn’t significantly match the other routes, the router picks the default route.

On the other hand, the directly connected route manages local traffic within the 10.0.2.0/24 network. Finally, the router uses the link-local address route when the IP address is between 169.254.0.0 and 169.254.255.255.

Moreover, the network routing metric values of the three routes are 100, 100 and 1000. A route with a lower metric value indicates higher priority. Therefore, the default and directly connected routes are prioritized more than the link-local address route.

Furthermore, to change the metric values associated with the existing routes on the system, we can utilize the replace option within the ip route command. Let’s see the syntax of the command:

$ sudo ip route replace [destination] via [gateway] dev [interface] metric [value]

Therefore, in this command, we need to provide four inputs:

  • destination indicates the host or target device IP address
  • gateway denotes the gateway IP address through which the data packet travels to the destination
  • interface represents the network interface
  • value denotes the network routing metric value

Now, let’s use the command by changing the route metric value of the default route:

$ sudo ip route replace default via 10.0.2.2 dev enp0s3 proto dhcp src 10.0.2.15 metric 200

Here, we changed the metric value of the default route from 100 to 200, decreasing its priority.

Finally, we can verify the changes we made using the ip route show command:

$ ip route show
default via 10.0.2.2 dev enp0s3 proto dhcp src 10.0.2.15 metric 200 
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100 
169.254.0.0/16 dev enp0s3 scope link metric 1000 

As we can see, we’ve changed the routing metric value for the default route.

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

Finally, we discussed the ip route command, which can be used to change the metric value regardless of the network interface used in the system.