Flush the DNS Cache on Yosemite (using a nice quick alias)

Problem: I’ve updated some Name Server records on a domain and I want to check they’ve propagated using Dig. When I dig mydomain.com ns I keep getting the old results.

Solution: The local DNS cache on the machine needs to be cleared. As far as the OS is concerned, it’s already looked up that domain name and doesn’t need to do a fresh lookup until the domain record’s TTL (time to live) has expired.

On OS X Yosemite there are two name resolution caches that need emptying, Multicast DNS and Unicast DNS. They can be cleared / emptied / flushed using two commands in Terminal:

sudo discoveryutil mdnsflushcache
sudo discoveryutil udnsflushcaches

A Nice Quick Alias: But really… who’s going to remember that? So I’ve added an alias to make it easier. Aliases are added (amongst other places) to your user’s .bash_profile file. To edit your bash profile file, open it with the following command in terminal:

nano ~/.bash_profile

Then once nano (the text editor) is open, add the following line to the file:

alias flushdns='sudo sh -c "discoveryutil mdnsflushcache; discoveryutil udnsflushcaches"'

To save the file, press Control (ctrl) + x and follow the prompts.
Once this is saved, load the changes you’ve made into your current terminal session with the command:

source ~/.bash_profile

Now when you type flushdns in terminal, your caches will be cleared! woo!

1. The quickest way to open Terminal (or anything actually) is to use Spotlight. Press the Command (cmd) + Space keys, and type ‘Terminal’ in the resulting input, then press Return / Enter.
2. If your bash profile doesn’t exist, it might be empty when you type the nano command above. This is ok, when you Control (ctrl) + x after adding the line, the new file will be created.
3. When opening the bash profile file, I used ~/ before the file name. This is to ensure that it opens the profile file of the user you’re currently logged in as.

Please let me know if this post was useful with a click!
Nope, not helpful...Yep, more useful than not! (No Ratings Yet)

Leave a Reply

Your email address will not be published. Required fields are marked *