How can I make changes to the network routing metric permanently

If you are using NetworkManager, the proper way to change the metric for the default route is to modify the connection associated with interface enp0s3 in this way:

nmcli connection modify <connection-name> ipv4.route-metric 1

and then re-activate the connection:

nmcli connection up <connection-name>

You can find the value for <connection-name> in the output of nmcli connection.


The correct way to do this, in Debian and derivatives, is to write a file in /etc/NetworkManager/dispatcher.d (call it whatever you like), with the following content:

#!/bin/sh

# Change the metric of the default route only on interface enp0s3

IF=$1
STATUS=$2
MY_METRIC=1

if [ "$IF" = "enp0s3" ]
then
        case "$STATUS" in
                up) 
                ip route del default dev $IF
                ip route add default via $DHCP4_ROUTERS dev $IF metric $MY_METRIC
                ;;
                *)
                ;;
        esac
fi

This way, your customization will not be overwritten upon each update. In order to check this, stop the Network Manager, kill the dhclient and flush the IP address of the interface, then restart network manager.

You can find documentation here.

EDIT:

as per FvD request:

systemctl stop network-manager
pkill dhclient
ip addr flush dev eth0   
systemctl start network-manager

if the interface in question is eth0, otherwise change accordingly.


The easiest and right way to do this is by editing /etc/network/interfaces.

Here is a simple example of /etc/network/interfaces:

auto lo eth0
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp
    metric 700

Restart networking using service networking restart for the changes to take place.

Ref: Section 5.7.2. The ifmetric package of Debian Manual

The following sets the eth0 interface to be preferred over the wlan0 interface.

Install the ifmetric package.

Add an option line with "metric 0" just below the "iface eth0 inet dhcp" line in "/etc/network/interfaces".

Add an option line with "metric 1" just below the "iface wlan0 inet dhcp" line in "/etc/network/interfaces".

Extra Note:

  • auto <interface1> <interface2> starts interfaces on start of the system.
  • allow-hotplug <interface> starts the interface on hotplug event.