I'm able change my network routing metrics with ifmetric, for example ifmetric enp0s3 1.

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface         UG    1      0        0 enp0s3         UG    100    0        0 enp0s8

When I reboot though, the metric for enp0s3 reverts to 101. How can I make this change permanent or have it set automatically at boot time?


6 Answers 6


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.

  • This does not really answer the question, because it does not do it at boot time, automatically. Apr 1, 2020 at 7:02
  • 3
    This is permanent and IMHO answer the question. Be aware, still, that this solution is attached to a connection, not an interface. If you happen to use several connections (typical case: roaming device connecting to various places) on the same interface (the machine's wifi or Ethernet), the operation has to be done for the each of the connections that need metric adjustment. This may or may not be what you want. I feel this is good because you can e.g. say "this specific Wifi costs me money by the byte so put a higher metric", "this Ethernet connection is high-performance, use it in priority". Apr 14, 2020 at 12:46
  • Unfortunately, it doesn't change the metric of the default gateway immediately, I posted a question here : unix.stackexchange.com/questions/625126/…
    – Aminos
    Dec 18, 2020 at 15:43
  • This is THE answer for systems running NetworkManager
    – gatopeich
    Feb 25, 2021 at 13:01
  • Related blog post titled "Changing NetworkManager route metrics": blog.felipe.lessa.nom.br/?p=129. Mar 8, 2021 at 7:45

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:


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


if [ "$IF" = "enp0s3" ]
        case "$STATUS" in
                ip route del default dev $IF
                ip route add default via $DHCP4_ROUTERS dev $IF metric $MY_METRIC

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.


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.

  • 1
    That's assuming that we're using Network Manager in Debian, which is still not necessarily the case (As in my case, for instance; I dropped it to avoid a systemd-as-init dependency that was causing boot failures at random). Other than that this is still a good answer for most (but definitely not all) Debian users. It works fine on my other system which still uses Network Manager.
    – Wyatt Ward
    Mar 31, 2020 at 20:24
  • 1
    This answer would benefit from instructions on how to kill the dhclient, flush the IP address of the interface and restart the network manager.
    – FvD
    Jun 18, 2020 at 3:19
  • 2
    @FvD: done, please read my edit. Jun 18, 2020 at 5:14

For Ubuntu

ip route replace default via metric 1

For me, it doesn't require any more steps ever since I type that in and it is still metic 1 until now.

For Centos

The proper way is that you add the above entry to the "/etc/sysconfig/network-scripts/route-eth(x)" except for the ip route replace so add only default via metric 1

  • This doesn't seem to persist, I replaced a route added via IPv6 router advertisement, but the original entry keeps getting recreated with the original (wrong) metric
    – Ferrybig
    Nov 16, 2021 at 14:08
  • @Ferrybig that could be your some other 3rd software keep changing it. find it and fix it. Linux systems alone don't do themselves.
    – Seandex
    Jan 13, 2022 at 15:54
  • it was first party software that came with a default installation of Ubuntu. Network manager on Ubuntu is broken for IPv6 and requires the uninstall of the connection check package to respect the interface metrics
    – Ferrybig
    Jan 13, 2022 at 17:02
  • 1
    This solution worked best for me when having more than 1 active network interface (e.g. two GSM modems)
    – George Rey
    Aug 17, 2022 at 20:58

The easiest and the right way to do this is by editing /etc/network/interfaces.
(If this file does not exist, you can create it.)

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.
  • /etc/network/interfaces does not exist on my machine, so this answer is somewhat confusing.
    – FvD
    Jun 18, 2020 at 3:35
  • This is nearly correct for Raspbian. Instead of editing /etc/network/interfaces directly, add a file to /etc/network/interfaces.d/ with the changes. Example: sudo vim /etc/network/interfaces.d/wlan1, add allow-hotplug wlan1 \ iface wlan1 inet dhcp \ metric 200 Nov 17, 2020 at 21:25
  • @FvD, sorry for noticing late. Hopefully you got the solution - You can simply creat the file. Updated the answer for anyone new looking for this. Mar 18, 2022 at 11:53
  • The addition of "metric" has no effect for me on debian 10. Nov 4, 2022 at 0:23

You should be able to make it permanent in /etc/dhcpd.conf where you can set an interface metric like this.

interface enp0s3;
metric 1;
  • Um, I don't think so. What is the os? Also try and see if this exists /etc/conf.d/dhcpcd
    – ProdIssue
    Feb 14, 2017 at 17:58
  • tried creating this file with your settings - it did not work. Ubuntu 16.10. Thanks though.
    – marathon
    Feb 15, 2017 at 4:24
  • didn't work with me either Jul 4, 2018 at 17:52
# vim /etc/dhcpcd.conf

interface enp0s3
metric 1

Without semicolon works for me.

Without semicolon

Without semicolon

Tested on

  • Raspberry Pi 4 Model B
  • Raspberry Pi OS Bullseye 32bit
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 14 at 7:32
  • This works when you use dhcpcd which needs to be running as a dhcp client of OS.
    – undefined
    Jul 17 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .