I am running ubuntu xenial 16.04
We are using openvpn to connect to a virtual private cloud. That cloud has it's own DNS server (as does our local route – home or office).
When I connect to the VPN all of the IPs in that network are available but I can't reach any by host name. The reason is simple: the resolv.conf file still shows my local office nameserver. If I manually overwrite the resolv.conf to have the correct name server all is good.
So, how can I get it to automatically reconfigure resolv.conf upon connecting to the VPN?
Can I hook in to a system event and execute a script?
Best Answer
The OpenVPN package has a script for this in
/etc/openvpn/update-resolv-conf
. You need to configure it with:This will fetch the DNS server addresses from the
dhcp-option DNS
options passed by the OpenVPN peer/server and configureresolvconf
accordingly. It handlesdhcp-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 intosystemd-revolved
instead ofresolvconf
.On Debian, this script is in the
openvpn-systemd-resolved
.