Home Technology Networking How to Edit the hosts File on Mac OS X

Tags: DNS Dynamic Name Service , Hosts File , Mac OS , Networking , Unix Administration

By Andrew B. Bartels, August 23rd, 2010

Editing the hosts file is a way associate browsable host names with IP addresses, without the need to set up a DNS entry.  It's best to use DNS where it's available, but sometimes the only way around a networking
Open Terminal  )Applications->Utilities->Terminal
cd /private
sudo nano ./hosts
enter password for your user account, should be an admin
Add new entry, as shown
CTRL-O to write
Press ENTER on filename
Confirm lines written
CTRL-X to exit
dscacheutil –flushcache
open browser and browse to the new address

The Unix hosts file is a way to associate host names with IP addresses, without the need to set up a DNS entry.  Editing the hosts file is a good way to map names for a few machines on a network.  You should use DNS for network-wide name mappings.

Below are step by step instructions for editing the hosts file on Mac OS X:

Note - if you aren't comfortable typing Unix commands, see this alternate easier method, which is a round about way of doing the the same thing - but does not require using Terminal.

1. Make sure you are signed in to your Mac under an account that has administrative privileges.  You MUST have a non-blank password on your account.  Aside from the obvious security implications of having an administrator account with no password, these instructions will not work if the password is blank.

2. Open the Terminal application.  Do this by clicking on the Applications folder in the tool bar, then go to Utilities, then Terminal.

3. In the terminal window, type the following commands:

cd /private
sudo nano ./hosts

When prompted, enter the password for the user account you are logged in under.  This should be be an administrator account.

4. You'll be taken to a text editor with the contents of the hosts file:


5. Add a new hosts entry and save.  Use the arrow keys to navigate to the first empty line.  In the screen below we've added an entry to map the name www.newdomain.com to


6. Once the new line is added, write the changes to disk:

6a. Press CTRL-O to write

6b. Press ENTER on filename (to save as './hosts' without changing the filename)

6c. The save was successful when you see 'Wrote xx lines'.

6d. Press CTRL-X to exit the editor


7. Flush the name cache.  From the prompt in Terminal, flush the name cache by entering the following command:

dscacheutil –flushcache

8. Test it out. Open browser and browse to the name.