Ubuntu – Select Primary network interface

Sometimes you want to have a default primary interface or network configured.

Most modern Linux distributions, like ubuntu today uses Network-Manager. You can control that from the terminal using either nmcli og nm-tui

To display current connections configured, with priority:

nmcli -f NAME,UUID,AUTOCONNECT,AUTOCONNECT-PRIORITY c

To display the primary route to the internet

route -n

You can set a temporary connection priority, by install ifmetric. (Lower number is higher priority)

apt install ifmetric
ifmetric wlp6s0 50

For a more permanent fix, i have chosen to write the above command, and run it as a service.

/etc/systemd/system/ifmetric.service

[Unit]
Description= Set ifmetric to use WIFI as primary route
After=multi-user.target
[Service]
Type=forking
User=root
ExecStart=/bin/setwifidefault

Then create a script in /bin

/bin/setwifidefault

#!/bin/sh
ifmetric wlp6s0 50

Make the script executable

chmod +x /bin/setwifidefault

Enable the script at startup

systemctl enable ifmetric.service