OpenVPN and systemd-resolved

How does one use the dns pushed via an openvpn server with systemd-resolved ?

Before I decide to 'upgrade' to systemd-networkd. I could use some variant of openvpn-resolv-conf script to call resolvconf to manage entries in /etc/resolv.conf on successfully establishing a vpn tunnel.

This would allow me to resolve names on the remote end of the vpn tunnel.

Now that systemd-resolved manages /run/systemd/resolved/resolv.conf is it possible to automatically add DNS pushed via a openvpn connection to the list of nameservers used for resolution?


Solution 1:

Use up/down scripts from https://github.com/jonathanio/update-systemd-resolved in your OpenVPN's config file. These use systemd-resolved's DBus interface to update DNS information.

Solution 2:

Edit: as of today, this solution is mostly obsolete. Users of newer systemd versions should refer to the solution given by Piotr Dobrogost.

The trick is to create a temporary network configuration file with DNS settings, and then restart systemd-network so it will apply global DNS settings.

I have written a modified script that does exactly that. You can check it out here: update-systemd-network.sh