Skip to content

Instantly share code, notes, and snippets.

@xros
Created February 9, 2025 15:15
Show Gist options
  • Save xros/da65b1e721f5f4a783d729c300f5a778 to your computer and use it in GitHub Desktop.
Save xros/da65b1e721f5f4a783d729c300f5a778 to your computer and use it in GitHub Desktop.
Linux (Ubuntu) set network interface priority using nmcli

2 ethernet interfaces: enp6s18, enp6s19

Need to use enp6s19 as default and use enp6s18 as backup

Principle: set different Metric on different interfaces. The lower Metric is, the higher the priority is.

Need tool nmcli

1. Check route

Run command route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         xiaoqiang       0.0.0.0         UG    101    0        0 enp6s19
default         192.168.1.1     0.0.0.0         UG    105    0        0 enp6s18
10.31.0.0       0.0.0.0         255.255.0.0     U     101    0        0 enp6s19
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 enp6s18
192.168.1.0     0.0.0.0         255.255.255.0   U     105    0        0 enp6s18

2. Check connection

Run command nmcli connection show

NAME                                      UUID                                  TYPE      DEVICE
use_bridge_pve_to_xiaomi7000              84fcc077-a4ae-4f06-8692-f110c82bc4db  ethernet  enp6s19
Wired connection 1                        3806e4d5-4376-3073-a68d-6e8e376a3f55  ethernet  enp6s18

3. Set priorities using nmcli

Run command nmcli connection modify "Wired connection 1" ipv4.route-metric 105

Run command nmcli connection modify "use_bridge_pve_to_xiaomi7000 " ipv4.route-metric 101

Note Metric 101 is lower than 105, so the interface whoes name is "use_bridge_pve_to_xiaomi7000" will be prioritized.

In this case traffic go through enp6s19. If enp6s19 fails, traffic will go through enp6s18.

4. Restart interfaces to make it work

Run command

nmcli connection down "Wired connection 1"
nmcli connection up "Wired connection 1"
nmcli connection down "use_bridge_pve_to_xiaomi7000"
nmcli connection up "use_bridge_pve_to_xiaomi7000"

5. Check the priorities

Run command route

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment