75

I want to display the DNS servers that are used by the current network setup on OS X, from the command line.

1

3 Answers 3

110

There are several ways - here are two:

cat /etc/resolv.conf

-or-

scutil --dns
4
  • 1
    Its extremely annoying that networksetup -getdnsservers doesn't work for DHCP-assigned DNS servers. I always forget about scutil. The 'sc' stands for System Configuration? It sure doesn't configure much of the system... Sep 10, 2016 at 5:46
  • 2
    It's also good to note that dig or nslookup don't necessarily give a realistic picture of how the macOS applications resolve domain names from the local system, especially when multiple (domain-specific) DNSes have been configured, such as when using a VPN client for multiple concurrent connections. Instead of nslookup or dig, use dscacheutil -q host -a name somehostname.com to test DNS resolution. It takes into account all configured DNS servers as well as their priority order.
    – Ville
    Aug 9, 2017 at 21:08
  • 6
    cat /etc/resolv.conf doesn't seem like a "reliable" solution anymore. This is the notice I get in macOS High Sierra when using it: (sorry for the formatting - comments don't support simple line breaks) # macOS Notice # # This file is not consulted for DNS hostname resolution, address # resolution, or the DNS query routing mechanism used by most # processes on this system. # # To view the DNS configuration used by this system, use: # scutil --dns
    – PatrikN
    Apr 4, 2018 at 8:43
  • 1
    I like scutil --dns | grep nameserver to just get the DNS servers. Jun 26, 2019 at 0:16
5

The following shell command can be useful to list the current DNS entries:

grep nameserver <(scutil --dns)

To filter it out for the script, you can pipe the output into awk '{print $3}' or grep -o "[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+" command.

2
  • 7
    This is the same as scutil --dns | grep nameserver correct (just different syntax)? Jun 26, 2019 at 0:18
  • 1
    @SamAndrew81 correct same
    – slm
    May 6, 2021 at 16:47
-1

This is what I get on my system for the first command in the accepted answer. I think this is what @PatrikN was posting in his comment. So you definately want to prefer the second command in the accepted answer scutil --dns

11:40:03 ~ $ cat /etc/resolv.conf
#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
...actual settings redacted...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .