Friday, March 29, 2013

Dev on your Mac? How to edit host file for testing!

OK so, you are working on your Mac and want to test a website in your browser... but the domain is not yet delegated - so you only have IP address for your dev server or new web hosting.

What you need to do is edit your host file, so that your browser checks this first before the domain DNS.

  1. Open a terminal window. (Applications/Utilities/Terminal)
  2. Type this line in:



    sudo nano /private/etc/hosts

  3. You will be requested to type your password in.
  4. The terminal window will then show any current host settings. Now add the line you want. Use arrows keys to position flashing cursor where you want.



    EG: 127.0.0.1  mydomain.com.au
  5. Once done, hit Control+O to save. Then press Enter.
  6. Now exit Terminal - Control+X.

You can also download a "Hosts" application that will install and be available from System Preferences. However this will not work for all OSX versions. Terminal is a better way to go.

For more advanced users, you can use "sudo vim".

To flsuh DNS in OSX 10.5 + use "dscacheutil -flushcache" - for OSX 10.4 and below, use "lookupd -flushcache".

NOTE: Had a few questions on this, if you add domain without "www" - you need to check in browser without the www as well.