I want to display the DNS servers that are used by the current network setup on OS X, from the command line.
Asked
Active
Viewed 1.4e+01k times
82
-
Same question: https://superuser.com/questions/258151/how-do-i-check-what-dns-server-im-using-on-mac-os-x – Ricardo May 11 '21 at 23:08
3 Answers
118
There are several ways - here are two:
cat /etc/resolv.conf
-or-
scutil --dns
Scot
- 7,835
- 4
- 39
- 46
-
1Its 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... – Geoff Nixon Sep 10 '16 at 05:46
-
3It'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 09 '17 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 04 '18 at 08:43
-
1I like `scutil --dns | grep nameserver` to _just_ get the DNS servers. – SamAndrew81 Jun 26 '19 at 00: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.
kenorb
- 12,217
- 17
- 81
- 140
-
9This is the same as `scutil --dns | grep nameserver` correct (just different syntax)? – SamAndrew81 Jun 26 '19 at 00:18
-
1
-
Technically this is [process substitution](https://www.gnu.org/software/bash/manual/bash.html#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. – shawkinaw Jan 26 '23 at 01:58
-1
To get all into a comma separated line:
scutil --dns | sed -n '/nameserver/ { s/^.* : \(.*\)/\1/p; }' | sort -u | paste -s -d',' -
estani
- 139
- 3
-
-
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? – estani Jul 26 '22 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 – mmmmmm Jul 26 '22 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. – estani Jul 26 '22 at 13:00
-
that is my point to enter a regex you need to escape characters. How can you enter in a script just the regex. You can't separate the two you can only deal with the presentation on the screen. Even then it is just odd characters – mmmmmm Jul 26 '22 at 19:27
-
A simpler way to print just the IP addresses is `awk -F' : ' '/nameserver/ {print $2}'` – shawkinaw Jan 26 '23 at 01:54