← cd ~/blog

less flushdns-cache-mac-osx.md

FlushDNS cache in Mac OSX

#Bash#Mac OSX archive

# historical archive — written years ago, kept as it was. Some info is dated.

Why on earth would I ever need to ask my Mac to flushdns its cache?
I often work with virtual machines and even more often I find myself testing machines that in the future will become what are known in the jargon as “production servers”.

By the way, a small explanation:

Dev Server – developers playground. test new tools and methodologies
Test Server – Clean environment to QA and test app/code
Production Server – Actual implementation in the enterprise.

Back to us, one of the best ways, at least for me, is to give them a real HOST name, and to do this all I need is a change to the /etc/hosts file.

The advantages of such an operation are easy to guess:

  • you don’t have to modify the DNS of a domain name;
  • there’s not even any need to register the domain name too far in advance;
  • there’s no need to have an active internet connection to run the various tests.

For example, if I wanted to direct the traffic for the domain name www.example.com to one of my virtual machines with IP 10.211.55.33, all I would need to do is this:

  1. edit the /etc/hosts file as root;
  2. add a line with the IP and Host name.

For example, just for a laugh, type the following in the terminal and, when asked, enter your password:

sudo echo "127.0.0.1 www.google.com" >> /etc/hosts

now try doing a search with google.com from your browser: impossible, all the traffic to www.google.com is re-directed to your Mac’s loopback device, effectively making the biggest search engine in the world unreachable.

At this point each of you should understand the power of a very simple tool!

Well, someone would probably still be able to see the google page for various reasons and due to various factors.

Here’s how to get around all this and make it impossible to view the page: you need to force the flushing of the cache ( flushdns ) of Mac OSX’s DNS. To do this and speed things up, just issue the following command in the terminal:

dscacheutil -flushcache;sudo killall -HUP mDNSResponder

The command above is simple to read, there is nothing I need to explain to you.

I often use this command and so I need to simplify the way I call it in my terminal. I take advantage of the power of my BASH aliases. In Linux the aliases are simply placed in the ~/.bashrc file, but in Mac OSX we have to resort to the ~/.bash_profile file

Edit your file, which often doesn’t even exist in your HOME, and add the line below, copy and paste:

alias flushdns='dscacheutil -flushcache;sudo killall -HUP mDNSResponder'

Last step, tell the shell that something has changed; so you can:

  • close and reopen the terminal;
  • add this new resource with the command ” . ~/.bash_profile

Indeed, once our alias has been added, all we have to do is type the following command in a terminal

flushdns

to have what was described earlier executed.

To see all the aliases possibly set in your terminal, all you need to do is call them up with the command

alias

Done!

NOTE: I didn’t go on at length about which terminal editor to use to edit the files above, because if you are reading this I hope that at least this you know how to do 😉 . I personally use “vi” but many use “nano”. The choice is yours!!!