Set network interface priority in Ubuntu core 20

I am trying to set the network interface priority on a device with Ubuntu Core 20 so that in case multiple network interfaces are available the device first connects via Ethernet , then via WiFi and after that via 4G. On Ubuntu I can use the ifmetric command to change the metric of an interface to accomplish this, but that is not available on Ubuntu-core.

I looked into nmcli, but could not find a command to set the metric for a particular interface. I was only able to set metrics for particular connections. I managed to write a script that selected all the connections available on a particular interface and set metric for them individually, but that setting gets reset after boot and I cannot find a way to trigger a script on boot in Ubuntu core.

I also tried adding the script to /etc/NetworkManager/dispatcher.d, but I got an error that it is a read-only directory.

How can I set network interface priorities on Ubuntu core so that they are persistent even after a reboot?

Hello,

I simply install the network-manager snap, and with that, I can then set the network route metric of a certain interface with: nmcli con mod "Wired connection 1" ipv4.route-metric 100, and so on. I believe this will be applied across reboots/network unplug-replug.

You can check what you have available with nmcli con show, and show the specific interface’s details with nmcli con show "Wired connection 1"

If you are unsure if the control of the network interfaces is changed over from networkd to NetworkManager, have a look here:

I hope this helps? BR

Yes, that command does change the metric, but only for a particular connection and not the entire interface, but I managed to write a bash script using this command that changes priority of all connections on a particular interface, but the settings get reset on reboot.

so make your script a oneshot daemon in a snap :wink: