How to edit your hosts file on Mac OS X

Share to:

codesmSometimes you need to view a site on a development server. It’s something that I use regularly to connect to a development server using the regular site domain but mapped to a development server’s IP address. This way you can maintain your DNS as it is, and work with the development site’s future domain in a real and meaningful way. Some CMS and applications work best this way when you’re doing active development, and you’re not ready to adjust your domain’s zone file quite yet.

It’s a pretty straight forward and easy process, but a word of caution: if you’re not familiar working with the command line, proceed with caution. You can screw stuff up in a quick hurry if you’re not careful!

Start Terminal on Your Mac

In order to change your hosts file on Mac OS X, you need to use the Terminal app. You can find it by going in to your LaunchPad and starting to type “terminal” and hit enter to launch it, or going to your applications folder, and then to your utilities folder, and it will be in there too.

Open Your Hosts File

Once you get terminal started you will want to run the following command:

sudo nano /private/etc/hosts

This will open up your hosts file in Terminal allowing you to make edits. Make sure you include the first slash before private to get you to the right directory.

Enter Your Password

Next, Terminal is going to ask you for your Mac password, so go ahead and enter that next. As you’re typing your password the cursor will not move, so hopefully you know your way around your keyboard. You can also copy and paste your password in.

Once you enter your password, you’ll now be in the edit screen for your hosts file.

Make Your Entry in Your Hosts File

The lines that start with # are “commented out” so that they’re not read by the system, but allow you to read any important info that the creator of the file wants you to know about. Learning how to comment things is a big help when you’re working with code, and if you need to toggle back and forth between a live site, and one that’s on a remote server, you can simply comment out the line that routes that domain to the development server, in order to get back to the original site.

It should look a little like this:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##     localhost

Use your arrows to scroll down to an empty line below the ones that are already there by default. Once you’re on a new line, add your new entry by adding the IP address of the server you want to point your domain to, then followed by your domain name. You may want to include both and That way it won’t matter whether you include the www or not when trying to get to your new destination that you’re mapping – both the regular domain and the www domain will both resolve to the same place.

Save The Hosts File

Once you’ve made your adjustments by adding a new line to the hosts file you’ll need to save it so hit control+o to save the file. The system will prompt you if you want to write to the same file. Since you’re writing to the same place, just hit enter to save it as the existing file. Next to complete the edit and get out of the hosts file, hit control+x to exit and that will bring you back to your main command prompt.

Make note: it’s control not command.

Flush Your DNS Cache

You should now be good to go. If for some reason your new site mapping isn’t working correctly you may want to flush DNS cache and you can do that while you’re still in Terminal.

dscacheutil -flushcache

Once you’ve got everything done, go ahead and check your hosts file edits. Your new mapping should be in place! Once you’re done and you’ve pushed your site live, make sure to remove your domain mapping, either by commenting it out, or just completely removing it, so that it resolves naturally. You don’t want to be stuck looking at your development server while the rest of the world is looking at the live version.