First of all the path to your hosts file on OS X is:
/private/etc/hosts
The "hosts" file is in the "etc" folder. Quick command:
-
sudo nano /private/etc/hosts
If you need more help and don't understand the format of the hosts file or need additional help then please continue, if you don't need the help than you, probably, have already left and are not reading this any longer.
For everyone else, adding Hostname to IP address mapping to your hosts file in OS X is essential for doing any development for yourself or for clients.
Doing this allows you to shorten your paths when accessing your website in development on the development server. This is assuming that you are setting up Virtual Host's on your development server. I use Apache as my development server and will cover setting up Apache Virtual Hosts at a later time. Until then there are many good examples.
An example of how this will work, you could access:
http://192.168.1.2/clients/client_name/html
Or, you could access:
http://client_name
While accessing the same content in both of the examples. The above example assumes that your development server is located on your LAN with an IP address of 192.168.1.2. Additionally, my example assumes that you have a Virtual Host called "client_name" setup up in Apache.
Let's Get Started
You can accomplish this a few ways, but I will be covering the method of adding it via a command line utility with Apple's Terminal or iTerm. If you are not comfortable with using the command line, even though the steps are very easy, you could use the GUI.
Here are the steps to take:
- Open your command line utility.
- Backup your original "hosts" file. Type "sudo cp /private/etc/hosts /private/etc/hosts.bak"
- Type "sudo nano /private/etc/hosts"
- Type your password to confirm that you want to edit the "hosts" file.
- Edit your "hosts" file with your new entries
- Press "ctrl-x" to save the changes
- Press "y" to confirm you would like to save the changes
- Press "return" to save the file as the same file name
- Test your new "hosts" file.
- Remove your backed up "hosts.bak" file.
Backup Original "hosts" File
-
sudo cp /private/etc/hosts /private/etc/hosts.bak
Open the File for Editing
Type the command and the terminal utility will prompt you for your password to continue editing the file.
-
sudo nano /private/etc/hosts
Editing the "hosts" file
Your "hosts" file will look something like:
-
###
-
# Host Database
-
#
-
# localhost is used to configure the loopback interface
-
# when the system is booting. Do not change this entry.
-
127.0.0.1 localhost
As you can see by the sample "hosts" file the IP address goes first then you press "tab" and type the Hostname. In our example situation we want to go to:
http://client_name
And have your browser internally go to:
http://192.168.1.2/clients/client_name/html
To do this we will edit our "hosts" file to look like:
-
###
-
# Host Database
-
#
-
# localhost is used to configure the loopback interface
-
# when the system is booting. Do not change this entry.
-
192.168.1.2 localhost
-
192.168.1.2 client_name
Our new "hosts" file will do two things for us.
- Redirect your browser to your development server's web root if you point your browser to http://localhost.
- Redirect your browser to your development server in the correct folder (http://192.168.1.2/clients/client_name/html) if you point your browser to http://client_name
Save the "hosts" File
Now all we need to do is save our "hosts" file with the following steps.
- Press "ctrl-x" to save the changes
- Press "y" to confirm you would like to save the changes
- Press "return" to save the file as the same file name
Testing and Cleanup
Now all we need to do is to point our browser to http://client_name and your browser will pull up your clients website. If it does not then the "hosts" file is in the wrong format.
If everything worked correctly all we need to do is delete the "hosts.bak" file and we are all done. You can do this by typing the following into your command line utility:
-
sudo rm /private/etc/hosts.bak
Type in your password and the file is gone.
Apache Development Linux Mac OS X« iTerm Background and Text Color Modifications Rails 2 Blog in 15 Minutes »

Thanks for the great tip! I’ve just got a Macbook Pro after being a Windows user for far too long and had to edit the hosts for a VPN connection I have but didn’t know where to start. You’re explanation was really clear and simple – thanks again!
I got to tell you, that I haven’t been able to do so, I just can unlock the restrictions over /etc/hosts file… I just need to add a line. Is a brand new macbook pro… if you can show me the ligth in this issue i’ll be very happy
Daniel,
What are you having problems with? Did you type sudo nano /private/etc/hosts? Make sure to type sudo.
Regards,
Justin