How to Change DNS from Command Line of Mac OS X

Jun 2, 2015 - 12 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.

.

Related articles:

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

12 Comments

» Comments RSS Feed

  1. WizIT says:

    Following Empty Outputs, but computer is working fine with Internet connections, why?

    networksetup -getdnsservers Ethernet
    There aren’t any DNS Servers set on Ethernet.

    networksetup -getdnsservers Wi-Fi
    Wi-Fi is not a recognized network service.
    ** Error: The parameters were not valid.

  2. pamela says:

    I’ve followed the steps but my DNS won’t work when I tried opening blocked sites :( What should I do? I’ve tried manually changing the DNS in Network Preferences as well and failed as well. My DNS stopped working after I changed my mac’s SSD. Any ideas how to fix this?

    I’m on El capitan 10.11.4

  3. 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

  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. Bert Visscher says:

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

  6. 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

  7. Gilgamesh says:

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

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site