Get DNS Server IP Addresses from the Command Line in Mac OS X
You can quickly retrieve active DNS server IP addresses on any Mac by using the networksetup utility. This is done from the command line, so launch the Terminal app and type one of the following command strings, dependent on the version of OS X that is running on the Mac.
Retrieving DNS details from Terminal in new versions of OS X including in OS X Yosemite, Mac OS X 10.7 Lion, OS X 10.8 Mountain Lion, 10.9 Mavericks, and later, is done with the following networksetup syntax:
networksetup -getdnsservers Wi-Fi
This presumes you’re using wi-fi, which most of us do these days. Replace Wi-Fi with ethernet or your interface of choice if otherwise.
Getting DNS info from the command line in prior versions of OS X, like Mac OS X 10.6 Snow Leopard, 10.5, and before, use the following syntax instead:
networksetup -getdnsservers airport
Note that I’m specifying “Wi-Fi” or ‘airport’ in these example strings because I primarily use a wireless connection, but you could also specify ethernet and bluetooth to get the DNS details for those interfaces. Just replace the latter interface text with the interface you are looking to identify DNS IP information for, typically it’s the same for every interface on the computer.
Assuming there are several DNS servers set within Mac OS X’s networking preferences, you will see a report of each DNS server in the order of their priority, looking something like this:
8.8.8.8
208.67.220.220
208.67.222.222
10.0.0.1
For those wondering, the top most DNS IP in that sample list is Google’s Public DNS, the next two are from OpenDNS, with the last being a local router. If you need to, you can use a free utility like namebench to find a fast DNS server.
Additionally, you can retrieve DNS information by using the ‘nslookup’ command on a server, this will report back the remote servers DNS details, as well as your own primary DNS to resolve the other server:
nslookup google.com
This will report back something like the following, with the first “Server” and “Address” bit showing the DNS IP used by the local machine:
$ nslookup google.com
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: google.com
Address: 74.125.239.135
Finally, another option is to look at /etc/resolv.conf, but because that file is automatically generated, it is not always considered accurate if DNS has recently changed and has not yet been flushed, do note that flushing DNS is a bit different in new versions of OS X, as Apple has changed how DNS functions on several occasions.
… since it show also DNS servers for virtual interfaces, which are used for VPN.
to your trouble, it seems it is simply an nslookup bug. At least in my case, nslookup seems to follow only nameservers which you can see in network settings in GUI.
I’m also not able to resolve host by nslookup by default, but when I try to use any application (for example ping), it resolves names c
I found a better way to get the DNS IP’s only issue is trying to find out how to change the order of the DNS’s server.
scutil –dns
this will give IP’s Only in order of priorty:
scutil –dns | grep nameserver | sed “s/ nameserver\[0\] \: //g
My issue is when I connect to VPN and try to do a nslookup of a host on the VPN connection get following error:
nslookup host.vpndomain.com
Server: 192.168.1.254
Address: 192.168.1.254#53
** server can’t find host.vpndomain.com.gateway.2wire.net: REFUSED
as can be seen it’s resolving to the 1st DNS server and gives up.
If go in to System Preferences –> Network –> Advance and enter the DNS server for vpndomain.com resolves with no issues.
Correct, scutil –dns is much better tool sin
I never thought about using networksetup for this but you can also get the same result by simply looking at nslookup:
$ nslookup
> server
Default server: 8.8.8.8
Address: 8.8.8.8#53
Default server: 4.2.2.1
Address: 4.2.2.1#53
Command in OS X Lion is:
networksetup -getdnsservers Wi-Fi
Because Airport = Wi-Fi just renamed
Nice tip. Google’s DNS is surprisingly slow in benchmarks, but I would rather use Google than OpenDNS.