Missing Policy Routing Rules on Linux

If you have set up some policy routing rules on Linux (eg using the ip rule add command) on a Linux distribution with systemd (which is most of them), you will probably find at some point that your policy routing rules have mysteriously disappeared.

This is because systemd’s networkd service expects that it — and only it — will be used to manage all routes and policy routing rules. Whenever it starts up or shuts down a network interface, or whenever the networkd service itself is restarted, networkd by default will delete all the policy routing rules that aren’t defined in its own network configuration files (as well as delete any routes that are not defined in its own config files, or aren’t implicitly required by the network interfaces themselves).

Fortunately, you can stop this misbehavior. To do so, edit the /etc/systemd/networkd.conf file, and switch the ManageForeignRoutingPolicyRules and ManageForeignRoutes settings from yes (the default) to no:

# /etc/systemd/networkd.conf
[Network]
ManageForeignRoutingPolicyRules=no
ManageForeignRoutes=no
...

After making this change, restart networkd (eg sudo systemctl restart systemd-networkd) — and this time your policy routing rules shouldn’t disappear!