Sometimes, I need to see the actual IPv4 address of web sites I open in Safari.

Now, there are two terminal commands, host and dig, which I could use for that.

But these commands are not always telling my the actual IP address that the Mac apps, such as Safari, use. Examples for when they show a different IP address:

  • I've added the address to the Mac's /etc/hosts file.
  • The IP address has recently changed and the Mac apps still use the previous one it has cached.

So, is there a tool that tells me the IP address that's currently known to OS X instead of the address the external name server reports?

  • Maybe I'm not understanding your question, but how is an App (Safari or otherwise) going to see an IP address that's different from what's in an external DNS? What's leading you to conclude that the IP address is different?
    – Allan
    Apr 5 '18 at 18:01
  • It just is. Try adding " www.apple.com" to your hosts file, restart (or kill the dns resolver) and then issue cmd "host www.apple.com" - it'll give you Apple's actual address, but opening it in Safari will fail. This is also documented in the help for these commands. Apr 5 '18 at 19:01
  • 1
    At the very end of the man page for host - The host command does not use the host name and address resolution or the DNS query routing mechanisms used by other processes running on Mac OS X. The results of name or address queries printed by host may differ from those found by other processes that use the Mac OS X native name and address resolution mechanisms. The results of DNS queries may also differ from queries that use the Mac OS X DNS routing library.
    – Allan
    Apr 5 '18 at 19:06
  • I think you're confusing host resolution with DNS querry tools. Safari will start with /etc/hosts and then go to DNS, while utilities like dig and host will directly query a DNS server.
    – Allan
    Apr 5 '18 at 19:10
  • Install and configure dnsmasq with (a) forwarding server(s) and use as DNS server in your system prefs. This won't work for apps like Google Chrome which may use their own DNS resolution. If you want to include Bonjour/DNS-SD it's probably easier to use bind(9). In both cases dig/host will work (better).
    – klanomath
    Apr 5 '18 at 19:43

It appears I made wrong assumptions about DNS caching. However, I was right on the /etc/hosts file, in that the host command ignores it, while apps running on the Mac do not ignore it by default.

So, I've made a very simple program that uses one of the Mac OS functions to resolve the host name into an IP address. This way, changes to the hosts file will be seen by this program, as they're seen by any other Mac app.

The program, including Xcode project, can be downloaded here.

You must log in to answer this question.

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