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 or, 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.


# Set bash delimeter to be line break

# VPN DNS Server

# Get adapter list
adapters=`networksetup -listallnetworkservices |grep -v denotes`

for adapter in $adapters
        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
                # revert back to DHCP assigned DNS Servers
                networksetup -setdnsservers $adapter empty

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.

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in Networking, OS X & iPhone
0 comments on “OpenVPN: Update Client DNS Servers on Mac OS X Using the Command Line.
1 Pings/Trackbacks for "OpenVPN: Update Client DNS Servers on Mac OS X Using the Command Line."
  1. […] out this post on updating client-side DNS servers if you'd like to update all of your adapters instead of the ones most often used (Ethernet and […]

Leave a Reply