How to Change DNS from Command Line of Mac OS X

Jun 2, 2015 - 10 Comments

Change DNS servers from the 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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

10 Comments

» Comments RSS Feed

  1. Gilgamesh says:

    Wish I knew how to create my own DNS server now. :P

  2. Ivan Storck says:

    when I
    `networksetup -getdnsservers Wi-Fi`
    I get the error message:
    `** Error: The amount of parameters was not correct`
    I am on 10.10.3

  3. Bert Visscher says:

    There’s an S missing in “-setdnservers” in the above screenshot.

  4. chris says:

    What about if I want to change my ethernet – what do I put instead of Wi-Fi ?

    • pants cretin says:

      Open Network control panel and change DNS in system preferences, the command line is not for you if you are not prepared to detect the network interface. Ethernet and wi-fi on the same Mac often.

      • Matt says:

        Such a rude reply. Why bother replying at all? Chris, you can see the names of all the network services by typing `networksetup -listallnetworkservices`.

  5. Paul says:

    I used this to set google dns servers along with my ISP default ones

    & it worked…

    paul$ networksetup -getdnsservers Wi-Fi
    8.8.8.8
    8.8.4.4
    216.146.35.35
    216.146.36.36

    but after 60 seconds my cursor turns to a pinwheel & then they are automatically reset to the default again

    paul$ networksetup -getdnsservers Wi-Fi
    216.146.35.35
    216.146.36.36

    What could be happening here?? (yosemite 10.10.5)

    • If the configuration is overwritten, is because another process is overwrites the configuration, so you can do the same with a script like:

      while [[ true ]]; do
      networksetup -setsearchdomains Wi-Fi ber.mytoys.de
      networksetup -setdnsservers Wi-Fi 10.2.1.252 10.1.2.51
      sleep 1
      done

      Of course this is a nasty workaround but it could work

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Recent Posts