How to automatically fetch new name server on VPN connect

The OpenVPN package has a script for this in /etc/openvpn/update-resolv-conf. You need to configure it with:

script-security 2
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf

This will fetch the DNS server addresses from the dhcp-option DNS options passed by the OpenVPN peer/server and configure resolvconf accordingly. It handles dhcp-option DOMAIN as well.

It is not perfect however, because this will prepend those name servers to the list of existing name servers instead of overwriting the list of name servers. If you are using openresolv the -x can be used to overwrite the DNS configuration instead of preprending to it.


If you're using systemd-resolved, you can use the /etc/openvpn/update-systemd-resolved which hooks into systemd-revolved instead of resolvconf.

script-security 2 
up /etc/openvpn/update-systemd-resolved
down /etc/openvpn/update-systemd-resolved
down-pre

On Debian, this script is in the openvpn-systemd-resolved.

Tags:

Dns

Ubuntu

Vpn