How to: Clear or Flush DNS cache on a Mac OS X computer

How to: Clear or Flush DNS cache on a Mac OS X computer

Most operating systems implement a local DNS cache for a number of reasons: lower the load on DNS servers, faster domain name resolution, etc. This is great for a number of reasons, but it also poses a problem from time to time. Most users access sites that are robust and their IP addresses don’t change, but for developers or systems administrators, when working on testing or development environments a change in hosts or ip addresses is not uncommon. So if you are playing with your web hosts and you need your web browser to start talking to a different web host then you need to clear or flush that DNS cache. Also noteworthy is that most operating systems offer a local “hosts” file that serves as a simple name resolution override. So even if the DNS server you are using resolves to a particular IP, you can overwrite this value locally using your hosts file. Because it is a lightweight and fast alternative, it is very simple.

If you come from a Windows background you can clear/flush the DNS cache through one simple command that has been around for ages (at least a solid decade): ipconfig /flushdns. I must confess I was hoping an ifconfig /flushdns on my mac would do the trick… but not. In the case of OS X there seems to be a myriad of options, depending on which version of the operating system you are using. I say this because through time I am always in need to flush my DNS cache and I am always learning a new / different way and previous methods stop working on newer versions.


The Commands

Pre-snow leopard you can run this command line on your terminal (Mac OS X versions 10.3 and 10.4): lookupd -flushcache.

From Mac OS X versions 10.5 and 10.6 you can use the following instruction: dscacheutil -flushcache

For OSX versions 10.7 and 10.8 we have the command (although it seems dscacheutil is still lurking around): sudo killall -HUP mDNSResponder

And now, for OS X version 10.9 we got a new twist again: let’s do what we did back in 10.8 and 10.6 at the same time: dscacheutil -flushcache;sudo killall -HUP mDNSResponder

Finally, For OS X 10.10 Yosemite we’ve got an even more complicated set of commands: sudo discoveryutil mdnsflushcache;sudo discoveryutil udnsflushcaches

So now we have a new command called discoverutil and we must perform an mdns cache flush and a urns cache flush in order to effectively flush all DNS caches.

You might need to run the commands using elevated credentials (as an administrator).


How to check the cache stats

Sometimes you need to check to make sure the cache has been flushed or sometimes you are just curious to see how is the DNS cache performing. The command discoveryutil has a few options to check on those two caches we spoke of before: udnscachestats and mdnscachestats. By using these two we are able to obtain statistics such as these:

sudo discoveryutil udnscachestats

UDNS Cache Stats: Ext Resolver on en0

UDNS Cache Stats: Resolver domain [default], type Unicast, ifIndex 4, port 0, timeout 30, A yes, AAAA no, serviceID 0, scope None, order 0

server 192.168.0.1

UDNS Cache Stats: Cached 241 of 5000, table size 256, hash coverage 0.40, max miss depth 5, avg miss depth 0.67, reclaims 0

Cached records after aging 241

UDNS Cache Stats: Ext Resolver on en0

UDNS Cache Stats: Resolver domain [default], type Unicast, ifIndex 4, port 0, timeout 30, A yes, AAAA no, serviceID 0, scope InterfaceID, order 0

server 192.168.0.1

UDNS Cache Stats: Cached 0 of 5000, table size 256, hash coverage 0.00, max miss depth 0, avg miss depth 0.00, reclaims 0

Cached records after aging 0

sudo discoveryutil mdnscachestats

MDNS Cache Stats: lo0: Cached 6 of 7500, table size 16, hash coverage 0.19, max miss depth 2, avg miss depth 0.25, reclaims 0

Cached records after aging 6

MDNS Cache Stats: awdl0: Cached 3 of 7500, table size 128, hash coverage 0.02, max miss depth 1, avg miss depth 0.02, reclaims 0

Cached records after aging 3

MDNS Cache Stats: en0: Cached 22 of 7500, table size 128, hash coverage 0.09, max miss depth 3, avg miss depth 0.12, reclaims 0

Cached records after aging 22

Those statistics will show you the number of cached domain names. When you flush or clear the DNS cache you’ll see those values drop to 0 out of x. That’s a pretty good indication the clearing of the cache worked.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: