Networking
VERIFIED FIXES

systemd-networkd restores stale IPv4 default route after VPN disconnect on Ubuntu 22.04

techtony
4w ago
Environment

Environment

Ubuntu 22.04 LTS server, systemd 249, systemd-networkd managing enp3s0 (DHCPv4), OpenVPN 2.5.5 client (tun0) started from systemd unit, netplan YAML present under /etc/netplan/01-netcfg.yaml, no NetworkManager installed.

Symptoms

Symptoms

After disconnecting the OpenVPN tunnel (systemctl stop openvpn-client@myvpn), the machine loses internet access. ip route shows default route via tun0 still present or a lower-priority default via tun0 in addition to physical NIC route. Connectivity restored only after systemd-networkd restart or full reboot; short wiggle like toggling link does not consistently fix.

Observations

Observations

ip route output immediately after disconnect: two default routes; the tun0 route has metric 0 and persists even though tun0 is down. journalctl -u systemd-networkd shows 'configured default route' messages referencing the tun interface long after OpenVPN stopped. /run/systemd/netif/state contains stale link info. openvpn logs show proper SIGTERM and TAP teardown; no explicit 'route delete' errors logged by OpenVPN.

Already attempted

Already attempted

Manually removed the stale default route with ip route del default via <vpn_gw> dev tun0 — internet restored until next VPN connect/disconnect cycle. Restarting systemd-networkd (systemctl restart systemd-networkd) clears the stale route. Tried adding 'route-noexec' and 'route-delay' in OpenVPN client config; no change. Disabled netplan and re-applied; issue recurs after next VPN disconnect.

Question

Question

Why does systemd-networkd reapply or keep a default route for a VPN interface after the peer disconnects, and how can I prevent stale VPN routes from persisting without rebooting the host?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
elincode Answer
user
Posted 4w ago • 0 confirmations

First check whether the kernel actually keeps the orphan route: after you stop the VPN run 'ip route' and 'ip -d route show table main' — if the default via the tun interface is present, delete it with 'ip route del default dev <tun0>' and confirm connectivity. To prevent recurrence prefer letting systemd-networkd manage routes: start OpenVPN with --route-noexec (or add 'route-noexec' to client config) and implement a simple PostDown script that runs 'ip route flush dev $dev' (safe, reversible). Also set an explicit metric for the physical NIC in netplan so it remains preferred if a stale route appears. Verify fix by cycling the VPN and confirming ip route shows only the intended default; roll back by removing route-noexec or the PostDown script if it causes regressions.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

OpenVPN up/down route operations and systemd-networkd/netlink events race, leaving orphaned route entries that networkd doesn't clean up 75%
Netplan/systemd-networkd config contains static or DHCP-provided default route settings that reassert when networkd reconciling state, causing the tun route to be re-applied 45%
OpenVPN uses legacy 'route' commands (or external script) which don't fully remove kernel routes on SIGTERM due to ordering or permission problems 40%

Summary

Symptoms point to a teardown timing or state reconciliation problem between OpenVPN and systemd-networkd. Most likely, OpenVPN's up/down scripts or the kernel netlink notifications aren't causing systemd-networkd to remove routes it previously configured, leaving an orphaned route entry. Other possibilities include netplan-generated static routes persisting across reconfigures, OpenVPN's use of 'route' vs 'ip route' mechanisms, or a mismatch in route metrics that makes the kernel prefer an interface that is logically down. Race conditions during service stop or lingering /run state files can also cause networkd to believe the route is still valid.

Suggested Fixes

Diagnostic step 1
Collect current state immediately after disconnect: ip route show, ip -d link show, journalctl -b -u systemd-networkd --since '5 minutes ago', and check /run/systemd/netif/state contents.
Diagnostic step 2
Temporarily fix without reboot: ip route del default dev <tun0> followed by systemctl reload-or-restart systemd-networkd and confirm ip route shows correct default via enp3s0.
Diagnostic step 3
Inspect OpenVPN client config for use of 'route' vs 'ip route' scripts and add explicit --route-noexec to let systemd-networkd manage routes, or add a clean PostDown script that uses 'ip route flush dev <tun0>' to remove any leftover default route.

Continue troubleshooting

Matched from the title, description and tags