Linux

OpenVPN client loses DNS and routes when systemd-resolved restarts on Ubuntu 22.04

viktorbyte
1mo ago
Environment

Environment

Ubuntu 22.04 desktop and server machines using OpenVPN 2.5 managed by systemd (client.service). systemd-resolved is active with /etc/resolv.conf symlinked to /run/systemd/resolve/stub-resolv.conf. OpenVPN uses the community up/down scripts update-resolv-conf to update DNS on connect.

Symptoms

Symptoms

OpenVPN shows as connected but DNS resolution fails (names not resolved). Some destinations are reachable by IP but others time out. After systemd-resolved restart (e.g. triggered by NetworkManager or a package update), /etc/resolv.conf reverts and the update-resolv-conf script logs errors 'could not replace /etc/resolv.conf'. OpenVPN tunnel still up but clients lose name resolution until reconnect.

Observations

Observations

Journal shows systemd-resolved restarted at times coincident with the outage. update-resolv-conf attempts to write /etc/resolv.conf which is a symlink to a runtime stub; writing fails or gets reverted by resolved when it restarts. Using 'resolvectl status' shows DNS per-link entries missing for tun0. Disabling update-resolv-conf prevents overwrite but leaves no DNS pushed into systemd-resolved.

Already attempted

Already attempted

Tried installing resolvconf package (conflicts with systemd-resolved). Modified update-resolv-conf to use resolvectl but changes were brittle. Added 'After=systemd-resolved.service' to OpenVPN systemd unit (improved ordering but did not prevent resolved restarts later). Tried using NetworkManager OpenVPN plugin which handles resolvectl correctly but not usable on servers.

Question

Question

How can I make OpenVPN reliably set DNS and routes on Ubuntu 22.04 so a systemd-resolved restart doesn't break name resolution? Prefer a reversible, systemd-friendly approach.

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

update-resolv-conf (legacy script) overwrites /etc/resolv.conf which is managed by systemd-resolved; resolved restarts revert those changes 90%
OpenVPN systemd unit lacks proper Wants/After dependencies for systemd-resolved and network services, causing ordering/race issues 50%
NetworkManager or another process restarts frequently and overwrites resolv settings despite OpenVPN attempts to set them 30%

Summary

This is a classic mismatch between legacy resolv.conf-updating scripts and systemd-resolved's stub resolver. update-resolv-conf tries to directly edit /etc/resolv.conf, but on modern Ubuntu that's a symlink to a transient file owned by systemd-resolved; whenever resolved restarts it restores its stub and drops per-link configuration applied by the script. The robust approach is to use systemd-resolved's API (resolvectl) to add DNS servers and domains for the tun0 link in OpenVPN's up/down scripts or to run OpenVPN via NetworkManager which integrates with resolved. Other causes like permission/immutable attributes on /etc/resolv.conf or improper systemd unit ordering are secondary.

Suggested Fixes

Diagnostic step 1
Replace update-resolv-conf calls in your OpenVPN up/down scripts with resolvectl commands: in the 'up' script run: resolvectl dns "$dev" 1.1.1.1 1.0.0.1 and resolvectl domain "$dev" '~example.internal' ; in 'down' run resolvectl revert "$dev".
Diagnostic step 2
If using systemd service for OpenVPN, set unit dependencies so the service starts After=systemd-resolved.service and add Wants=systemd-resolved.service; reload daemon and restart OpenVPN.
Diagnostic step 3
Test by restarting systemd-resolved (sudo systemctl restart systemd-resolved) while OpenVPN is up; verify with resolvectl status and 'dig' that DNS entries for tun0 persist.

Continue troubleshooting

Matched from the title, description and tags