50

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.

3 Answers 3

63

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/

2
  • 3
    For reference: Another useful command is ‘scutil --dns’. That will show the system’s current DNS configuration. (On macOS, /etc/resolv.conf will not tell the whole story.) Dec 15, 2022 at 15:36
  • I praise you for this question and answer! I was pulling my hair in desperation trying to find out what is going on.
    – not2savvy
    Apr 8 at 18:28
16

In addition to the great dig (pun intended) of the dscacheutil usage that's not covered by the man documentation, I'd like to add that at the core of the macOs name resolution system sits the mDNSresponder service, and it seems to be gathering all different sources of the lookup information under the common interface.

The tool for interacting with the mDNS subsystem is dns-sd, and as such, there is another way of querying for the DNS records that will honor all available resolvers:

~ » dns-sd -G v4v6 google.com 
DATE: ---Wed 04 Nov 2020---
19:05:47.081  ...STARTING...
Timestamp     A/R    Flags if Hostname                               Address                                      TTL
19:05:47.175  Add        2  0 google.com.                            2A00:1450:4001:081D:0000:0000:0000:200E%<0>  377
19:05:47.255  Add        2  0 google.com.                            172.217.22.110                               377

or

~ » dns-sd -q google.com   
DATE: ---Wed 04 Nov 2020---
20:27:50.834  ...STARTING...
Timestamp     A/R    Flags if Name                          Type  Class   Rdata
20:27:50.925  Add        2  0 google.com.                   Addr   IN     172.217.22.110

or even

~ » dns-sd -q google.com TXT
DATE: ---Wed 04 Nov 2020---
20:30:45.908  ...STARTING...
Timestamp     A/R    Flags if Name                          Type  Class   Rdata
20:30:48.831  Add        3  0 google.com.                   TXT    IN     36 bytes: 23 76 3D 73 70 66 31 20 69 6E 63 6C 75 64 65 3A 5F 73 70 66 2E 67 6F 6F 67 6C 65 2E 63 6F 6D 20 7E 61 6C 6C
20:30:48.831  Add        3  0 google.com.                   TXT    IN     46 bytes: 2D 64 6F 63 75 73 69 67 6E 3D 30 35 39 35 38 34 38 38 2D 34 37 35 32 2D 34 65 66 32 2D 39 35 65 62 2D 61 61 37 62 61 38 61 33 62 64 30 65
20:30:48.831  Add        3  0 google.com.                   TXT    IN     46 bytes: 2D 64 6F 63 75 73 69 67 6E 3D 31 62 30 61 36 37 35 34 2D 34 39 62 31 2D 34 64 62 35 2D 38 35 34 30 2D 64 32 63 31 32 36 36 34 62 32 38 39
20:30:48.831  Add        3  0 google.com.                   TXT    IN     60 bytes: 3B 66 61 63 65 62 6F 6F 6B 2D 64 6F 6D 61 69 6E 2D 76 65 72 69 66 69 63 61 74 69 6F 6E 3D 32 32 72 6D 35 35 31 63 75 34 6B 30 61 62 30 62 78 73 77 35 33 36 74 6C 64 73 34 68 39 35
20:30:48.831  Add        2  0 google.com.                   TXT    IN     65 bytes: 40 67 6C 6F 62 61 6C 73 69 67 6E 2D 73 6D 69 6D 65 2D 64 76 3D 43 44 59 58 2B 58 46 48 55 77 32 77 6D 6C 36 2F 47 62 38 2B 35 39 42 73 48 33 31 4B 7A 55 72 36 63 31 6C 32 42 50 76 71 4B 58 38 3D

Truth to be told, this is not the best command for scripting as it operates in an interactive continuous monitoring mode that would notify of any changes on the given address until one interrupts it. Although it allows for some fun stuff like watching a VPN-only path to disappear and then reappear when you reconnect:

~ » dns-sd -G v4v6 vpn.only.address.com    
DATE: ---Wed 04 Nov 2020---
19:10:01.672  ...STARTING...
Timestamp     A/R    Flags if Hostname                               Address                                      TTL
19:10:01.673  Add 40000002  0 vpn.only.address.com.                      10.100.100.100                                333
19:10:01.759  Add        2  0 vpn.only.address.com.                      0000:0000:0000:0000:0000:0000:0000:0000%<0>  323   No Such Record
19:10:07.703  Rmv        0  0 vpn.only.address.com.                      10.100.100.100                                0
19:10:10.783  Add        2  0 vpn.only.address.com.                      0.0.0.0                                      4502   No Such Record
19:10:10.905  Add        2  0 vpn.only.address.com.                      0000:0000:0000:0000:0000:0000:0000:0000%<0>  4502   No Such Record
19:10:32.669  Add        3  0 vpn.only.address.com.                      10.100.100.100                                298
19:10:32.670  Add        2  0 vpn.only.address.com.                      0000:0000:0000:0000:0000:0000:0000:0000%<0>  228   No Such Record
2
-4

Use BIND's Domain Internet Grapher tool. DiG.

dig www.domain.com a

dig www.domain.com mx

dig www.domain.com ns

can also be used to query specific name servers

dig www.domain.com a @ns1.domain.com

Pretty simple native tool that you'll find on pretty much every unix machine, including Mac osx.

1
  • 4
    The question mentions dig - and that native apps might work differently. The question says " I'm interested in using the native macOS DNS resolver from a script." - does dig do this?
    – Journeyman Geek
    Jan 9, 2023 at 2:08

You must log in to answer this question.

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