Macos – How to query macOS DNS resolver from Terminal

dnsmacmacosnetworking

I know how to query DNS using tools like dig, host, etc. But their macOS man pages state:

The results of DNS queries may also differ from queries that use the macOS DNS routing library.

Given that, how do I query DNS in terminal and get the same results as native macOS apps?

When testing DNS changes I sometimes see different results between the unix tools and native mac applications. Here is an example of what I've encountered:

$ dig +short example.com
192.168.0.100

dig shows that the DNS query returns a local address handled by our own DNS server. Meanwhile using the macOS 'Network Utility' lookup returns a cached result:

Network Utility lookup showing cached IP address

using the macOS network reachability tool, I get a cached result as well (for those not familiar with the output the IP address is revealed in the last line):

$ scutil -W -r example.com
 0: direct
   <SCNetworkReachability 0x7f8a39605ab0 [0x7fffa3c088f0]> {name = example.com}
Reachable

 1: start
   <SCNetworkReachability 0x7f8a39606000 [0x7fffa3c088f0]> {name = example.com}

 2: on runloop
   <SCNetworkReachability 0x7f8a39606000 [0x7fffa3c088f0]> {name = example.com (DNS query active), flags = 0x00000002, if_index = 13}
Reachable


*** 13:08:23.373

 3: callback w/flags=0x00000002 (info="by name")
    <SCNetworkReachability 0x7f8a39606000 [0x7fffa3c088f0]> {name = example.com (complete, 93.184.216.34, 2606:2800:220:1:248:1893:25c8:1946), flags = 0x00000002, if_index = 13}
Reachable

Is there a macOS command line utility to resolve addresses using the same heuristic that native mac apps use? Note: I am not interested in clearing the DNS cache, I'm interested in using the native macOS DNS resolver from a script. I've looked at man pages for mDNSResponder, dns-sd, scutil, networksetup, and dscacheutil none of which seem to query dns.

Best Answer

It seems taking the effort to formulate the question I understood the problem better to perform more precise google searches and found the answer.

On macOS querying DNS is achieved by:

$ dscacheutil -q host -a name example.com
name: example.com
ipv6_address: 2606:2800:220:1:248:1893:25c8:1946

name: example.com
ip_address: 93.184.216.34

Like most macOS shell commands it is not quite as nice as the unix equivalent, but it gives me consistent results with native mac apps.

I found this from https://random.ac/cess/2018/04/12/macos-dig-vs-dscacheutil-while-using-split-dns-with-viscosity-vpn-client/

Related Question