A machine with debian 10. It has 3 interfaces with different ip. One of them is needed in rare cases, it has a static route to the management network (the rest of the routing works via ospf through another interface).
The problem is that if you pull the cable out of the network card, the interface is still up and the route remains. Can't this be automated? And I could not find anything on this question, maybe I am doing something wrong?
In addition, if I use auto or allow-hotplug, the interface is enabled after reboot even without a link, so I have to manually raise it if necessary.
High level tools like NetworkManager usually remove address and route on "linkdown" links (ie: with the NO-CARRIER flag displayed on the interface). So if the interface is managed by such tool, one can expect for its route to appear and disappear when plugged and unplugged.
I understand using NetworkManager might be too intrusive with a dynamic routing protocol used at the same time and so might not be the best idea.
But actually the kernel routing stack already provides such feature, without having to modify any address or route: ignore_routes_with_linkdown.
As a few other entries, this one which appeared in kernel 4.2 (so available in Debian 10's kernel 4.19) and was shown then in kernelnewbies.org when it appeared, has been documented only since kernel 5.11:
So if the interface is called
mgmt0
and the route it provides should be used only when a carrier is detected on it, one should simply do:or put this in
/etc/sysctl.conf
.Now when this interface still kept administratively up is disconnected, the route entry displayed back by
ip route
would show the flagsdead linkdown
instead of the single flaglinkdown
to tell userland that indeed this route is currently ignored during a FIB lookup.Note: for an interface to detect a link, it has to be powered up. So it must stay administratively up. If it was brought down when the carrier is lost, it couldn't be brought back up automatically later on carrier detect, since there would be no such detection anymore.
Complete example:
Setting the addresses installs the automatic kernel LAN routes.
vethtest1
whose peer interface wasn't brought up will have a NO-CARRIER status and its routes will get alinkdown
flag.A route to 192.0.2.100 will use
vethtest1
since it got a narrower route (despite having thelinkdown
flag).Then with the new setting:
comes the new behavior:
Now if the interface gets carrier (simulated here by bringing up its peer interface):
route is restored through it: