Change DNS Server From Terminal (or script) on Mac OS X

29

19

How can I change my DNS server from the Terminal on Mac OS X? [I need this because my DNS is not working correctly with my VPN. Sometimes it's using the DNS for my main connection, and sometimes it's using the DNS specified for the VPN (which it should).]

Dan Rosenstark

Posted 2009-12-21T18:40:40.920

Reputation: 5 718

Answers

15

You can use scutil interactively from the terminal. Run sudo scutil and run these commands, swapping your DNS servers in where appropriate:

> open
> d.init
> d.add ServerAddresses * 8.8.8.8 9.9.9.9
> set State:/Network/Service/PRIMARY_SERVICE_ID/DNS
> quit

Instead of using 8.8.8.8 and 9.9.9.9 use your DNS servers.

The only problem is this is not persistent across reboots. If you want permanent changes, you'll want ncutil. The reason editing /etc/resolv.conf isn't sufficient in newer versions of OS X is because configd now uses a database to store information of current settings, which other applications read. Certain applications will still read /etc/resolv.conf (host for example), although that is not the case for all applications.

John T

Posted 2009-12-21T18:40:40.920

Reputation: 149 037

2@ssc Run scutil followed by > list to find the key to replace PRIMARY_SERVICE_ID with. – J.Money – 2015-03-31T02:12:02.120

2This does not seem to work on my Mac OSX 10.7.5 MBP, at least not when done literally. Should I replace anything in the set State:/Network/Service/PRIMARY_SERVICE_ID/DNS line or can I use that as it is ? Should the new DNS servers show up in the scutil --dns output after the change ? – ssc – 2013-10-30T13:57:37.963

28.8.8.8 is a valid one, though. Google's Public DNS. 8.8.4.4 is the secondary. :-) – Forgotten Semicolon – 2009-12-21T19:03:36.693

I know, I use it :) – John T – 2009-12-21T19:11:40.607

Nice, I'll be checking this out shortly. +1 for now. – Dan Rosenstark – 2009-12-21T19:16:24.530

John T, I want these servers to take priority over whatever servers are currently configured. Will it do that? – Dan Rosenstark – 2009-12-21T19:17:15.307

1It should. You can use scutil --dns to confirm. – John T – 2009-12-21T19:21:22.977

47

You can use networksetup:

sudo networksetup -setdnsservers <networkservice> DNS1, DNS2, DNS3

eg (having the Airport connection use Google's DNS Servers)

sudo networksetup -setdnsservers AirPort 8.8.8.8 8.8.4.4

You can find out the name of the network service by running networksetup -listallnetworkservices. It'll be 'Wi-Fi' probably.

This is the same as if you were to edit the entires in the Network Preference Pane in System Preferences, so it is persistent across reboots.

You may be running into a DNS issue on Snow Leopard that occurs when the order DNS servers are queried changes (see question 84144))

Chealion

Posted 2009-12-21T18:40:40.920

Reputation: 22 932

6One note to anyone wanting to remove the DNS, just write "empty" (without the quotes) instead of the DNS: sudo networksetup -setdnsservers <networkservice> empty – jackJoe – 2012-11-29T09:36:48.590

Wow, 84144 is the problem I'm having. Still processing all of this.... thank you muchly! – Dan Rosenstark – 2009-12-21T21:52:03.263

14

I don't have enough points to reply to Chealion's post but to add on to it I'd start with listing the interfaces

networksetup -listallnetworkservices

Once you have the interface you'd like to change you can do the below (I'm using the Wi-Fi but you can do any other interface)

sudo networksetup -setdnsservers Wi-Fi empty
sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4
sudo killall -HUP mDNSResponder

The first line of the above will empty out the DNS settings then follow it with the DNS servers we'd like to use and finely clear the DNS cache

To verify the DNS change you can do this before and after or simply after

scutil --dns | grep 'nameserver\[[0-9]*\]'

David Kittell

Posted 2009-12-21T18:40:40.920

Reputation: 141

Anyone that has stumbled across this page. I have a Gist on GitHub now that should help.

https://gist.github.com/dkittell/34fe7b7422323fba546948b7448933a5

– David Kittell – 2019-06-27T18:30:22.293

1

resolv.conf does not work on OSX anymore. There is a notice right at the top of that file as follows:

#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#

Also, networksetup -listallnetworkservices does not list all VPN interfaces.

Here is one way to use scutil to target the right interface: 1. Create a file commands.txt with your commands for the interactive scutil tool. e.g. contents for Pulse secure interface in my case:

get State:/Network/Service/net.pulsesecure.pulse.nc.main/DNS
d.add ServerAddresses * 8.8.8.8 9.9.9.9
set State:/Network/Service/net.pulsesecure.pulse.nc.main/DNS
  1. Run scutil with the commands piped in. (You need sudo for set)
sudo scutil < commands.txt

kikas

Posted 2009-12-21T18:40:40.920

Reputation: 11

0

You should be able to do it by editing /etc/resolv.conf (remember resolv.conf is reset after reboot), hope it helps - http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man5/resolver.5.html

zpon

Posted 2009-12-21T18:40:40.920

Reputation: 832

5As of older versions of OS X (10.2 and prior), that is the way to go. Not with newer releases though. – John T – 2009-12-21T19:06:25.237