Home / Change Mac OS X DNS servers from the command line

Change Mac OS X DNS servers from the command line

I frequently need to change the DNS servers I use on my Mac and it can be quite a convoluted process to switch them around using the GUI settings. This post shows how quickly you can change between DNS server settings from the command line using terminal.

Change DNS using the networksetup command line tool on OS X

Let’s say, for example, you need to switch DNS servers between a local DNS server:


and the Google public DNS servers:


Unfortunately it’s not as simple as simply over-writing the /etc/resolv.conf file with the new DNS settings; the system settings and command line tool shown in this post will update that file, but the settings are stored somewhere else too and apps don’t generally use the resolv.conf information.

To change DNS settings from the command line, open up Terminal and run this to get a list of all the network interfaces:

networksetup listallnetworkservices

This will return a list similar to this:

An asterisk (*) denotes that a network service is disabled.
Bluetooth DUN

Now you can configure DNS for the appropriate network interface. In my case, I want to update DNS for the "Wi-Fi" interface. To change to the DNS server settings to the local DNS server, do this:

sudo networksetup -setdnsservers Wi-Fi

When prompted for a password, it’s the same password as used when you log into your Mac.

Note that if your user is not an admin user then you won’t be able to do this and you will get the error message "[your login name] is not in the sudoers file. This incident will be reported."

To change it to the Google public DNS servers, do this:

sudo networksetup -setdnsservers Wi-Fi

You can put as many (or as few) DNS servers in the list; simply separate each with a space as shown in the second example above.

On your own system, change "Wi-Fi" in the examples above to the actual network interface you want to configure DNS for, and change the IP addresses to the DNS server(s) you wish to use.

Another way?

If you know another (or better) way of doing this, add your views to the comments below.