OpenVPN: Update Client DNS Servers on Mac OS X Using the Command Line.

There's a bit of a debate on how best to update your DNS resolver on Mac OS X when connecting to an OpenVPN Server. For whatever reason, even if use DHCP on the VPN server, OS X won't use the assigned DNS server(s). It's been recommended to use scutil, but the scripts are crazy long and I've read the resolver order sometimes gets reset anyway.

The usual route of using /etc/resolv.conf does not work on OS X but specifying DNS servers in your Network Preferences does. If you use public network servers like 8.8.8.8 or 4.2.2.2, you're already set. Move along :) But if you rely on DHCP assigned DNS servers, the the script below will do the trick. This script specifies or clears (sets back to DHCP default) the DNS servers on each of the adapters listed in networksetup.

#!/bin/bash

Set bash delimeter to be line break

IFS=$'\n'

VPN DNS Server

vpndns='172.20.0.1'

Get adapter list

adapters=`networksetup -listallnetworkservices |grep -v denotes`

for adapter in $adapters do echo updating dns for $adapter dnssvr=(`networksetup -getdnsservers $adapter`)

    if \[ $dnssvr != $vpndns \]; then
            # set dns server to the vpn dns server
            networksetup -setdnsservers $adapter $vpndns
            else
            # revert back to DHCP assigned DNS Servers
            networksetup -setdnsservers $adapter empty
    fi

done

Again, if you already set your DNS servers, your OpenVPN connection will use those. This script is effective for people who use DHCP assigned DNS servers by default and would like to tunnel their DNS requests when connecting to an OpenVPN server.