11

What command line command do I use to lookup a domain address?

When looking at the man pages for nslookup and dig I can see a section saying that these utilities do not use the Mac OS methods of finding names.

So, what command should I be running?

2 Answers 2

25

nslookup, host, and dig are perfectly good DNS query tools in OS X, but they all query DNS directly rather than going through OS X's internal resolution system. As a result, they don't check the /etc/hosts file, or resolve Bonjour/mDNS names (those ending in .local). The standard (quick 'n dirty) way to test resolution is to use ping, since it'll resolve the address you give it through the usual system before attempting to ping it. If you want something intended for the purpose, use dscacheutil instead:

$ dscacheutil -q host -a name www.apple.com
name: e3191.c.akamaiedge.net
alias: www.apple.com www.isg-apple.com.akadns.net www.apple.com.edgekey.net 
ip_address: 184.24.141.15

$ ping www.apple.com
PING e3191.c.akamaiedge.net (184.24.141.15): 56 data bytes
64 bytes from 184.24.141.15: icmp_seq=0 ttl=57 time=57.258 ms
^C
--- e3191.c.akamaiedge.net ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 57.258/57.258/57.258/0.000 ms
3
  • Kudos. I've filed a Radar that the manpages should be clearer on this: openradar.me/radar?id=5038477976535040 Dec 4, 2015 at 8:37
  • One issue with using dscacheutil for lookups is that if it cannot resolve a hostname it doesn't exit with a non-zero return code like host does.
    – gurple
    Feb 14, 2017 at 9:50
  • To overcome the lack of useful return codes I've created a most basic shell script to supplement the standard 'host' command. #!/usr/bin/env bash STAT=1 LOOKUPRESULT="$(dscacheutil -q host -a name "$1")" if [ -z "$LOOKUPRESULT" ] ; then exit "$STAT" else echo "$LOOKUPRESULT" STAT=0 exit "$STAT" fi
    – gurple
    Feb 14, 2017 at 12:33
4

Either command works fine. Just because it's not using OS X's methods doesn't mean that it doesn't work. If you want to use "OS X methods" may I recommend Network Utility?

enter image description here

This may still call whois and just display it in the window.

2
  • I guess this doesn't work for .local addresses, does it? May 4, 2017 at 19:38
  • No, this won't work for .local addresses. This uses a public whois server, and .local addresses won't be posted there.
    – Everett
    Feb 9, 2018 at 7:23

You must log in to answer this question.

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