Friday, June 5, 2015

How to Change DNS from Command Line of Mac OS X

Advanced Mac users may find it useful to know that DNS servers in OS X can be set from the command line, without having to turn to the System Preferences Network control panel. While the GUI Network control panel is undeniably the easiest approach for most Mac users, the command line method offers benefits for many use cases, particularly for troubleshooting, making temporary DNS changes, and remote management with ssh. 

To change DNS settings from the command line of OS X, you’ll be using the ever-useful ‘networksetup’ command. While networksetup has many advanced and complex uses, setting DNS is actually quite easy.
How to Set DNS Servers from the Command Line of OS X with networksetup

The networksetup command is available in all vaguely modern versions of Mac system software. You’ll use the -setdnsservers flag, point it at the network service, and then include the DNS IP, this may look like the following:

networksetup -setdnsservers (Network Service) (DNS IP)

For example, to set a Mac with wi-fi to Google DNS of 8.8.8.8 the syntax would be:

networksetup -setdnsservers Wi-Fi 8.8.8.8

You can set multiple DNS servers if desired, which will enable fallback in the event the first or second server is unreachable. For example, this would set OpenDNS for the first two DNS servers, and Google DNS as the third fallback:

networksetup -setdnsservers Wi-Fi 208.67.222.222 208.67.220.220 8.8.8.8

This is just an example of DNS servers and not necessarily a universal recommendation. If you’re looking to change yours, it’s worthwhile to find the fastest DNS server through apps like NameBench, which perform benchmark testing to determine which DNS will be fastest for your particular location.

How to Clear All DNS Servers with networksetup


This is not the same as flushing DNS cache, this simply removes any existing custom DNS server settings. This can be helpful if you’re wanting to fall back to DHCP provided DNS from a router, modem, or similar situation:

networksetup -setdnsservers Wi-Fi

As with setting DNS, removing DNS may require you to flush DNS caches after making adjustments, depending on what you’re trying to do.

Checking DNS Settings with networksetup


You can also check existing DNS settings by using the -getdnsservers flag with networksetup, this will report back whatever the current DNS settings are, if any:

networksetup -getdnsservers Wi-Fi
8.8.8.8

Changing and customizing DNS can have a wide variety of purposes, from speeding up lookups, to finding more reliable servers, to changing to alternate providers to perform detailed lookups during propagation, amongst many other situations.

No comments:

Post a Comment