Connection metric management

Here is how with some simple steps I changed the metric of an internet connection in order to give priority to a usb dongle so its set as the default interface for internet traffic in my personal laptop. The laptop’s integrated wifi interface does not support 5 Ghz connections so every time I boot up the system I had to manually disconnect the integrated interface and reconnect it so its metric changes to a higher value:

First one needs to see the internet connections managed by the NetworkManager service in order to get the name of the internet connection we need to modify:

nmcli connection show

The list shows the 2 wifi connections I currently use. One is for General internet traffic and the other one to manage an openwrt device:

NAME                    UUID                                  TYPE      DEVICE
Connection_1            0711f8ae-049e-4e4f-8800-3cffc70b458f  wifi      wlp3s0
Connection_2            b2c7835e-69ac-4520-b401-f7120a456d65  wifi      wlx3xvre3db545  

To verify the current metric for both interfaces we use ip route:

default via 192.168.1.1 dev wlp3s0 proto dhcp src 192.168.1.111 metric 600
default via 192.168.5.1 dev wlx3xvre3db545 proto dhcp src 192.168.5.198 metric 601  

As we can see the internet traffic has a default route through the slower wifi interface. We can change this by changing the metric:

nmcli connection modify "Connection_2" ipv4.route-metric 100

We restart the service:

sudo systemctl restart NetworkManager

Once the service is back up we verify the metric again:

default via 192.168.5.1 dev wlx3xvre3db545 proto dhcp src 192.168.3.198 metric 100  
default via 192.168.1.1 dev wlp3s0 proto dhcp src 192.168.1.111 metric 602

This change will be persistent after a reboot.