Problem with DNS with OpenVPN on Ubuntu 20.04

Solution 1:

It seems, that the main problem is with systemd-resolve as described here: https://github.com/systemd/systemd/issues/6076
Really great article is here, which I took as a starting point: https://www.gabriel.urdhr.fr/2020/03/17/systemd-revolved-dns-configuration-for-vpn/

A small workaround that worked for me is to run this after every connection to VPN. Basically setting DNS manually

sudo resolvectl dns tun0 10.0.9.2 # Replace with IP of your DNS server
# All internal services are like git.int.mycompany.com or ldap.int.mycompany.com
# You can try to set up "~mycompany.com", worked for me as well
sudo resolvectl domain tun0 "~int.mycompany.com" 

How to automate it

With NetworkManager:
If you use Network Manager (pictures of Manager available here), you can automate this with scripts in /etc/NetworkManager/dispatcher.d/

Create custom script, name it 02-ifupdown set chmod +x to it and paste

#!/bin/sh

EXPECTED_VPN_NAME="MyCompany VPN" # Put your VPN name here
VPN_CONN_NAME=`nmcli --get name,type con show --active | grep vpn | sed 's/\:.*//'`

if [ "$2" = "vpn-up" ] && [ "$EXPECTED_VPN_NAME" = "$VPN_CONN_NAME" ]; then
        resolvectl dns tun0 10.0.9.2 # Replace with IP of your DNS server
        resolvectl domain tun0 "~int.mycompany.com"
fi


With CLI:
Create your custom script, set chmod +x to it and paste into config:

script-security 2
up /path/to/my/script