Ubuntu 18.04 DNS set to VPN DNS

To ensure your vpn’s dns server is used, the following script can be put in “/etc/NetworkManager/”. It will need execute permission and the “CONNECTION_ID” changed to the vpn connection name.

#!/bin/sh

# NetworkManager script to set dns to use the work vpn when it is started.

DEVICE=${1}

setdns(){
  link=$(systemd-resolve --status | grep vpn0 | awk -F' ' '{print $2;}')
  busctl call org.freedesktop.resolve1 /org/freedesktop/resolve1 org.freedesktop.resolve1.Manager SetLinkDomains 'ia(sb)' ${link} 1 . true
}

if [ "${CONNECTION_ID}" = "Work" ]
then
  case "$2" in
  "up")
    ;;
  "vpn-up")
    setdns
  ;;
  "down")
  ;;
  "vpn-down")
  ;;
  esac
fi

Leave a Reply

Your email address will not be published. Required fields are marked *