Wednesday 14 March 2018

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


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



Answer



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.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...