A local DNS host file can be very useful when working in a development environment. You can have a development environment and a production environment and simply point the host to the development environment on your machine while all of the other users are directed to the production environment. This allows you to work on the development environment and fully test prior to pushing the changes to the live production environment.
This is a fairly common practice but if you’ve never done it on Mac OS X, then you probably need a hint as to how to do this.
The Hard Way
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 vi /etc/hosts
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
0.0.0.0 example.domain.com
Step 4: Save the changes to your host file by pressing SHIFT + Z twice in vi.
As soon as you save this file the changes you made will take effect. To check the changes, you can ping the host you entered to see if it returns the correct address or just go to that host in a browser and see if your development environment opens instead of the production.




{ 5 comments… read them below or add one }
This is all well and good, but I’d like to be able to add multiple hosts on the same computer. On most UNIX, I would edit the configuration for the local caching DNS and add a wildcard record, e.g.:
*.domain.com A 10.0.0.1
Anyone know a simple way to do anything like this on OS X? I’d prefer to avoid installing a DNS sever.
Thanks, Ben.
I’m completely new to Terminal and scares the bejesus out of me.
I followed your instructions and those at Caxy.com and I thought I succeeded at editing my Hosts file.
But when I tried to save it, using “SHIFT + Z twice in vi” it didn’t do anything (I just got a sound alert) and I couldn’t tell if I was “in vi.”
Furthermore, when I terminated that window and opened a new shell to see if the changes took, they seemed to be there but Terminal was now telling me that I had a duplicate swap file of my hosts file and to proceed with caution.
Can you elaborate on how to make these edits correctly, how to make sure one’s made those changes, and how to make sure one has not created duplicate and or problematic copies of the host file, in this case?
Thank you!
@Melton
If you are more comfortable editing with the GUI text editor in Mac OS X, TextEdit, you can modify the command to open up the hosts file using the following command:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hostsOnce you edit it in that application, just use the menus to save or press Command + s to save.
Hope this helps.
That’s awesome. Thanks!
On the Ubuntu forum I found “If you don’t know vi, use nano to edit the file:” and “Ctrl-X exits, and asks if you want to save.”
I did that, then scared myself because when I tried to view “host” again, using Terminal, I kept getting a blank window with [New File] (Oh, nevermind, I should have spelled it “hosts”….).
(What would you recommend as a good thing to read to learn Terminal?)
I’ve installed XAMPP on my Mac to install WordPress MU. I got up to the “successful installation” window that gave me a user id and pw and now I’m getting an Internal Server error.
I thought maybe the Hosts file was the issue… Any thoughts?
Thanks a million.
mc
@Ben
maybe you’d like to try
MacBook:~ axel$ sudo open /etc/hosts