Add & remove route with low priority in Linux

Posted on

Problem :

For a VPN network, to use non-VPN route as default, the article Wireguard Split Tunnel By Adding Low Metric Route describes how to run a command in PowerShell to achieve this behaviour.

In PowerShell, it is required to run the following command to add a route with a low priority (interface metric)

route add 0.0.0.0 mask 0.0.0.0 0.0.0.0 IF $wgInterface.ifIndex metric 9999; Set-NetIPInterface -InterfaceIndex $wgInterface.ifIndex -InterfaceMetric 9999;

And once finished / VPN is de-activated run:

route delete 0.0.0.0 mask 0.0.0.0 0.0.0.0 if $wgInterface.ifIndex metric 9999; Set-NetIPInterface -InterfaceIndex $wgInterface.ifIndex -InterfaceMetric 9999;

What is the equivalent of these commands in Linux please?

Solution :

To add the route:

sudo ip route add default metric 9999 via 0.0.0.0

To remove it again:

sudo ip route delete default metric 9999 via 0.0.0.0

where default corresponds to your 0.0.0.0 mask 0.0.0.0 while the via 0.0.0.0 corresponds to your third 0.0.0.0 (the next-hop address), so adapt accordingly if you don’t really want to route everything to the all-zeroes address.

See man ip-route for details.

Leave a Reply

Your email address will not be published.