87

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

128

There are several ways - here are two:

cat /etc/resolv.conf 

-or-

scutil --dns 
5
  • 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... Commented Sep 10, 2016 at 5:46
  • 6
    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. Commented Aug 9, 2017 at 21:08
  • 7
    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 Commented Apr 4, 2018 at 8:43
  • 1
    I like scutil --dns | grep nameserver to just get the DNS servers. Commented Jun 26, 2019 at 0:16
  • /etc/resolv.conf is no more used, but scutil --dns gives to-the-point info. Commented Nov 5, 2023 at 7:09
6

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.

4
  • 10
    This is the same as scutil --dns | grep nameserver correct (just different syntax)? Commented Jun 26, 2019 at 0:18
  • 1
    @SamAndrew81 correct same Commented May 6, 2021 at 16:47
  • Technically this is process substitution, where the <(...) creates a FIFO that can (often) be used in place of a file name. In this case, grep can either read from stdin or a file, so either technique works, but they are not synonyms. Commented Jan 26, 2023 at 1:58
  • Also scutil --dns | creates a pipe, so at least w.r.t the result is the same, and I find it more intuitive to use than <(scutil --dns) because the data flow is "from left to right". Commented Nov 5, 2023 at 7:17
0

To get all into a comma separated line:

scutil --dns | sed -n '/nameserver/ { s/^.* : \(.*\)/\1/p; }' | sort -u | paste -s -d',' - 
10
  • grep is much simpler why use the complex regexp? Commented Jul 25, 2022 at 10:50
  • Which complex regex? I would differ that dots and starts are a complex regex... in any case this returns the IPs separated by commas, grep cannot extract those values, it just select lines. Or am I missing something? Commented Jul 26, 2022 at 10:16
  • Any regex including \ is complex to me and I suspect most programmers. ANyway it is more complex in this case than grep. The OP only wants to display the IPs so why go more complex Commented Jul 26, 2022 at 10:42
  • ok. '\' is an escape sequence, not part of the regex, but part of sed. The title of my answer already states what this does, which is what I needed (and anyone doing anything with the IP afterwards within the same shell). I'm sorry you don't like that I shared. Commented Jul 26, 2022 at 13:00
  • 1
    @shawkinaw I misread the quotes and saw dvo answer, which is omitting them. Very nice! Commented Nov 17, 2023 at 21:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.