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, and then switching back to mDNSResponder yet again. Regardless, flushing DNS cache in Yosemite remains a terminal command, but it differs slightly depending on the exact release you are using of the OS, 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.
Clear DNS Cache in OS X Yosemite 10.10.4 & OS X 10.10.5
From OS X 10.10.4 moving onward to 10.10.5, including 10.11, Apple has ditched discoveryd and has replaced it (or rather, reverted back to) mDNSResponder. Thus, to clear DNS caches in OS X Yosemite 10.10.4, and 10.11 El Capitan, and presumably onward, the command string is as follows:
sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder;say cache flushed
That command flushes all DNS caches for OS X 10.10.4+.
Longtime Mac users may recall that command string is basically what worked in the release prior to Yosemite. With that said, earlier versions of OS X Yosemite before 10.10.4 will use a different command string discussed below.
Clearing DNS Caches in OS X Yosemite (10.10, 10.10.1, 10.10.2, 10.10.3)
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 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.