Force DNS on Mac OS X

Update: There's a neat little Mac App called Gasmask that can do this for you with a GUI. Or install from Brew via: brew cask install gas-mask.

When you go to any website on the Internet, your computer makes a request from a DNS server to what out what IP address the website is living on. This is IP address then cached locally on your computer, which means you can actually overwrite this value. This is especially handy when building a new website and you need to preview the new website before it goes live.

Start by opening up Terminal, and running the command sudo nano /etc/hosts.

You will need to enter your local computer password.

So, if your new website lives on the server 1.2.3.4, you can add the following line to the bottom of your hosts file:

1.2.3.4 www.domainname.com.au
1.2.3.4 domainname.com.au

Pro tip: if you want to compare your old website to your new website, you can be clever and setup your new website on www and leave your old website on non-www (or vice versa). This also depends on if your CMS will let you do this and not try to redirect one to the other.

To quit, press Ctrl + x then y.

Now to flush your local DNS enter dscacheutil -flushcache

And now try opening your website in your browser of choice.

To revert back to what the rest of the world is seeing, just go back into your hosts file as above, and comment out the line with a # then save and quit again.