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   eth2

ip route change via

route -n   eth1

This is done using the route command, example:

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

Debian manpage for the route command

  • 4
    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). – András Korn Apr 6 '18 at 18:55
  • 2
    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 '18 at 8:23
  • 6
    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? – András Korn Apr 18 '18 at 8:34

(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 via
ip route add via metric 1234
  • 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 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 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 at 20:28

ifmetric will allow you to change your route metric on the fly.

  • 3
    Hi! It would be best to give a working example of how to use the command to get the results that the question asks for. – dhag Jun 5 at 16:19
  • Yes it really would. Thanks. – AeroClassics Jun 5 at 18:02

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.