How to edit the hosts file on Mac OS X

How to edit the hosts file on Mac OS X

The hosts file on your Mac is a small text document that allows you to map hostnames to IP addresses. The hosts file is located in a protected area of the OS X file system, but there are still ways you can edit it.

You may wish to edit the hosts file for a number of reasons. For example, web developers commonly use it to spoof live URLs of websites when they are testing new features on their local machines.

The hosts file can also be used to block access to specific websites by remapping their hostnames to invalid IP addresses.

Editing the hosts file using Terminal

If you're comfortable using the OS X Terminal application, then it provides the most hassle-free method to edit the hosts file. As the hosts file is protected you can't simply open it with a text editor, make changes and save it again (though there are some workarounds for this if you'd prefer not to use Terminal, but more on that later).

To edit the hosts file with Terminal, begin by opening a new Terminal window and type the following command:

sudo nano /private/etc/hosts

Press Enter and the hosts file should open in the Terminal window. If you've not made any changes to your hosts file before, it should look similar to the image below.

The default OS X hosts file

The default OS X hosts file

The nano part of the above command refers to the Nano text editor, and tells Terminal you wish to open the hosts file in Nano so you can make edits to it.

To add a new IP address mapping, navigate to the last line of the hosts file by pressing the down arrow on your keyboard - you should see the grey cursor move as you do so. When it reaches the end add an IP address and then the Hostname you wish to map to it. For example:

Will map to the IP address And because is not a valid IP address, this will result in anyone trying to visit Facebook on that Mac seeing an error - effectively blocking the website.

If you have a local web server running on your Mac, you could do something like the following: testing.local

You can now use http://testing.local to access the local web server instead of http://localhost.

When you're done making edits, you can save the hosts file by pressing Ctrl + x to exit Nano. When it asks if you wish to save your changes, hit y.

Flushing your DNS cache

Usually the new mappings in your hosts file will begin to work instantly. In some cases however you may need to clear your Mac's DNS cache.

If you're using any version of OS X other than Mavericks, the following command should do the trick:

sudo killall -HUP mDNSResponder

Mavericks users should use:

dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Once the DNS cache has been cleared, your new mappings should take effect.

Edit the hosts file using TextEdit

If you would prefer to use TextEdit to edit your hosts file, then don't despair! While saving changes to the hosts files requires a little more effort a normal document, it can still be done.

As the hosts file is in a hidden part of the file system, we need to use the Go to Folder... option, available through the Go option in the Finder's menu bar. In the box that pops up, enter the following path:

Direct path to the hosts file

This should open a standard Finder window with your hosts file pre-selected. Click and drag the file onto your desktop - this moves it out of the restricted /etc folder and allows you to freely edit it. Double click to open the file in TextEdit (or your default text editor) and make whatever edits you require.

When you're done, save it and drag it back to the /etc folder. OS X will ask you whether you'd like to replace the existing file and then prompt you for your password. Once the file is saved open your web browser and test your changes - flushing your DNS cache as outlined above if necessary.