If I add a route with
ip route add 172.10.1.0/24 via 10.0.0.100 dev eth0
and then execute the same command a second time, it fails because the route is already present.
I don't see that behavior documented anywhere. Is it safe to depend on this behavior, or might different / future versions of Linux allow duplicate routes?
use-case
I'm writing a bash script that configures the routing table. I want to make it safe to run the script multiple times.
I see two options:
Put the
ip route add
command within anif
statement that runsip route list
and uses regex to check if the route is already in place.Just run
ip route add
and ignore if it fails because the route already exists.
The first doesn't seem very robust, as I have to depend on the output format of ip route list
. The second seems to depend on undocumented behavior.
for m in 1 2 3; do ip route add 172.10.1.0/24 via 10.0.0.100 dev eth0 metric $m; done
. A route with a metric != 0 is still a route. You don't have to use ip list | grep regex (yuck), you can check for the existence of a route explicitly: just replace theadd
withlist
in your command.