How to Flush DNS Cache in OS X Yosemite with discoveryutil
Some Mac users may encounter situations where they need to flush DNS cache in OS X for a name server to resolve properly, or for some DNS address change to become noticed by their individual computer. This is particularly relevant for system administrators, network admins, and web developers, but there are certainly situations where other users may need to dump and reset DNS caches too, like if a user has modified the /etc/hosts file and they need the changes to take effect without rebooting the Mac.
Longtime Mac users will know that resetting DNS cache has changed in nearly every version of Mac OS X, and OS X Yosemite is no different, likely due to the discoveryd replacing mDNSResponder. Flushing DNS cache in Yosemite remains a terminal command, and it actually allows you to clear either Multicast DNS or Unicast DNS, or both. You’ll probably want to clear both for good measure if you’re trying to reset all DNS caches on the Mac.
Clearing DNS Caches in OS X Yosemite (10.10)
You’ll need to use the Terminal to reset cache, you can find the Terminal app in /Applications/Utilities/ or launch it with Spotlight. To completely clear all DNS caches in the latest version of OS X, target both MDNS (Multicast DNS) and UDNS (Unicast DNS) with two different commands.
Clear MDNS Cache
sudo discoveryutil mdnsflushcache
Hit return and enter the admin password when requested.
Clear UDNS Cache
sudo discoveryutil udnsflushcaches
Again, hit return and enter the admin password when requested. Note that caches is plural with the latter command, a subtle but important syntax difference.
Flush and Reset All DNS Caches in OS X Yosemite
You can also string the above two commands together if you want, the following will even verbally announce when the caches have been cleared:
sudo discoveryutil mdnsflushcache;sudo discoveryutil udnsflushcaches;say flushed
Indeed MDNS and UDNS caches are different, but I’ve found that for functional DNS cache to actually clear in OS X Yosemite both commands are necessary. It’s entirely possible that you only need to clear one or the other for your own needs.
For those wondering, OS X Yosemite has done away with mDNSResponder, so there’s no need to kill that mDNSResponder process to refresh DNS caches like in prior versions of Mac OS X.
Checking DNS Cache Details in OS X Yosemite
If you are changing or are going to change DNS, and you want to see some details about what is currently cached, you can use the following commands:
Get UDNS cache statistics:
sudo discoveryutil udnscachestats
You can also retrieve specifics about multicast DNS cache with the following:
sudo discoveryutil mdnscachestats
Both of these will offer details as to the number of DNS entries are cached, offering a report that should look like this:
UDNS Cache Stats: Cached 962 of 1750
If you run those commands before and after running the flushcache variations, you’ll find they should be reset to 0 entries cache, like so:
MDNS Cache Stats: lo0: Cached 0 of 2000
How do you know if it changed?
After you’ve flushed cache, if you want to determine if a name server or IP has actually changed you can use the ‘dig’ command with the URL like so:
dig osxdaily.com
dig is similar to nslookup, except that it has better output and a few more details included, included query time, the set DNS server used to access the domain, and a timestamp, all of which can be valuable when troubleshooting name server issues. By the way, if the query time for this shows up as sluggish, you should use a tool like namebench to find a faster DNS server for you, often Google DNS or OpenDNS.
Know of another DNS cache trick for the latest versions of OS X? Let us know in the comments.
Glad to know how to dump DNS in Yosemite and Mac OS 10.10 but i”m sure it’ll be different in OS X 10.11 and OS X 10.12 now.
Oddly enough, the old one still exists! But, doesn’t seem to do anything, right? I do this and no cache flush?
sudo dscacheutil -flushcache
So, I guess make an alias for the huge double-dipper MDNS and UDNS mega DNS dump that cache:
alias resetdns=’sudo discoveryutil mdnsflushcache;sudo discoveryutil udnsflushcaches;sudo dscacheutil -flushcache;say flushed’
Yea, that’s right, I put them all in there for good measure. Flush that cache, baby! I need this sucker to work.
Why does Apple keep changing the DNS reset tool? Keep us on our toes?
It’s good to note the venerable Unix host, dig, and nslookup commands use different resolver mechanisms than most of OS X. When troubleshooting I usually compare with ping which gives the same results as the gui apps.
I use VPNs to customer sites daily, and often need to debug the VPN sites local DNS not being used.
When I find the DNS resolver works by testing with the host command, I cross check the same host with ping. If the results are not alike, it’s time to flush the DNS cache.
On a side note, the equivalent procedure with DNS flushing used to work for all hosts in a remote vpn site in previous releases, but now I find I need to flush the cache more than once to get multiple hosts working. I sure hope Appe will settle on the architecture to use for resolver and put more effort in stabilizing it 😄
THANKS Paul — your flush cache tutorial was the ultimate solution for the mysterious renaming malady that befell many Yosemite users after update. Previously happy host / computer names like ‘iMac-27′ suddenly and persistently became ‘iMac-27 (2)’ regardless of terminal encantations.
Your flush cache suggestions have allowed my machines to run happy with their proper names for over 24 hours.
EURIKA — and THANKS.
I made the alias I like that idea.
Thank you for this post very “to the point”. It is extremely helpful to myself who has really no knowledge of these things past os 9.. I checkout long ago. Thanks for making a simple copy paste situation. Appreciated!