Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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).]

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
8.8.8.8 is a valid one, though. Google's Public DNS. 8.8.4.4 is the secondary. :-) –  Forgotten Semicolon Dec 21 '09 at 19:03
    
I know, I use it :) –  John T Dec 21 '09 at 19:11
    
Nice, I'll be checking this out shortly. +1 for now. –  Yar Dec 21 '09 at 19:16
    
John T, I want these servers to take priority over whatever servers are currently configured. Will it do that? –  Yar Dec 21 '09 at 19:17
    
It should. You can use scutil --dns to confirm. –  John T Dec 21 '09 at 19:21
show 1 more comment

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))

share|improve this answer
    
Wow, 84144 is the problem I'm having. Still processing all of this.... thank you muchly! –  Yar Dec 21 '09 at 21:52
    
Thanks for clear and understandable answer :) –  nXqd May 31 '12 at 7:24
    
+1 works beautifully, thank you. –  jackJoe Nov 29 '12 at 9:14
1  
One note to anyone wanting to remove the DNS, just write "empty" (without the quotes) instead of the DNS: sudo networksetup -setdnsservers <networkservice> empty –  jackJoe Nov 29 '12 at 9:36
add comment

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

share|improve this answer
3  
As of older versions of OS X (10.2 and prior), that is the way to go. Not with newer releases though. –  John T Dec 21 '09 at 19:06
    
Thanks for that. Unfortunately, I'm in 10.6.2. –  Yar Dec 21 '09 at 19:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.