20

what is the command to modify metric of an existing route entry in linux? I am able to change gateway of an existing entry using "ip route change" command as below but not able to change metrics. Is there any other command for that?

route –n
40.2.2.0        30.1.3.2        255.255.255.0   eth2

ip route change 40.2.2.0/24 via 30.1.2.2

route -n
40.2.2.0        30.1.2.2        255.255.255.0   eth1
2

4 Answers 4

25

(Combining various comments into an answer)

Currently, it is not possible to modify a route's metric. As a 2005 message on LKML states:

[...] The metric/priority cannot be changed because we do not have separate fields for the fields to match and the new values so if you specify a metric the entry simply won't be found and the request fails with ENOENT because NLM_F_CREATE is not specified. This is a limitation of the current protocol and it might be a good idea to to change this, however it's non trivial [...]

This seems to apply both to ip route change and ip route replace - the former results in an error for me, while the latter creates an additional route as advertised (its man page states that replace will replace or create a route). This is consistent with the kernel responding with ENOENT, and ip route replace following up with a route creation request.


So, the solution is to delete the existing route and add a new one. e.g.

ip route del 40.2.2.0/24 via 30.1.2.2
ip route add 40.2.2.0/24 via 30.1.2.2 metric 1234
3
  • yes, got bitten by this problem this past few months... as Brian Cantrill keeps saying about Linux: It's an evolving thing with NIH (not invented here) syndrome...
    – Hvisage
    Mar 12, 2019 at 8:48
  • @Hvisage can you elaborate? Is there any tool which allows to change the metric without having to remove it?
    – Braiam
    Apr 23, 2019 at 19:43
  • @Braiam That is a negative, as in: NO such tool exists. as per the answer. If you refer to my Bryan Cantrill quotes, it was somewhere I believe in youtube.com/watch?v=wTVfAMRj-7E
    – Hvisage
    Apr 24, 2019 at 20:28
15

As noted in a comment to the question, quoting a message on the linux-net mailing list: "The metric/priority cannot be changed [...] This is a limitation of the current protocol [...]."
The only way is to delete the route and add a new one.

This is done using the route command, example:

sudo route add -net default gw 10.10.0.1 netmask 0.0.0.0 dev wlan0 metric 1

Debian manpage for the route command

3
  • 5
    The question wasn't how to add a route with a given metric; OP wanted to change the metric on an existing route (without deleting and re-adding the route). Apr 6, 2018 at 18:55
  • 4
    Obviously you haven't read the comment in the question which links to the email showing that the metric can not be changed: The metric/priority cannot be changed [...] This is a limitation of the current protocol[...]. The only way is to delete the route and add a new one.
    – Jan
    Apr 10, 2018 at 8:23
  • 8
    I know the metric can't be changed. Your answer still doesn't answer the question -- maybe point out, in the answer, that the metric can only be changed by deleting and re-adding the route? Apr 18, 2018 at 8:34
15

ifmetric will allow you to change your route metric on the fly, given the interface the routes you want to change are going through.

Usage

ifmetric <iface> [<metric>]

For example, use ifmetric tun0 12 to change the metric to 12 for all routes going through tun0.

1
  • Thanks for the tipp, and thanks to Lennart Poettering for making ifmetric. Nov 17, 2020 at 20:55
3

It can be done by nmcli as mentioned below:-

nmcli connection modify ACTUAL_CONNECTION_NAME ipv4.route-metric 100
nmcli connection up ACTUAL_CONNECTION_NAME

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.