How To Setup A Local DNS Host File On Mac OS X

A local DNS host file can be useful when developing a website. You can have a build environment and a production environment and simply point the host to the build environment on your machine, and your machine only, while all of the other users are directed to the live site. This allows you to work on the build environment and fully test prior to pushing the changes to the live production site.

Step 1: Open up a Terminal windows (this is in your Applications/Utilites folder by default)

Step 2: Enter the following command to open the local host file. (Note: The sudo command requires an admin password to allow you to open this file as root.)

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

Your hosts file will open in the application TextEdit.

Step 3: Now you can edit the host file. To add a new host entry, on it’s own line enter it in the format:

IP Address Host
1.0.0.127 domain.com
1.0.0.127 sub.domian.com

Step 4: Use the menus to save or press Command + S to save. As soon as you save this file the changes you made will take effect.

To check the changes, go to that host in a browser and see if your build environment opens instead of the live site.

If you are conformable in Terminal you can use vi to edit the file using the following command instead for Step 2.

$ sudo vi /etc/hosts

You can edit the host file within Terminal then save the changes to your host file by pressing SHIFT + Z twice in vi.

Something you may find useful is the command for flushing your local DNS cache in OS X. Enter the following in your Terminal window:

dscacheutil -flushcache

If you are using and older version of OS X below 10.5.2 use the following instead:

lookupd -flushcache

You will not receive any kind of response from the Terminal window after running the command. This is normal.

This article was posted on 20/01/12 in Apple, Code, Snippets

Save a massive 36% on a subscription to .net magazine!

We love .net magazine (known as "Practical Web Design" outside the UK) and have teamed up with Future Publishing to get our readers up to a massive 36% off a years subscription.

comments

add comment

What's the matter cat got your tongue? Be the first to comment...

x
Step 1. : Login details

about you.

Step 2. : Password

your say.

Step 3. : Email details

preferences.


That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.