You are on page 1of 5

3 0 3 5

MAC / TIPS

How to Edit the Hosts File on Mac OS X


By Jim Tanous on December 15, 2013 at 11:55 PM @mggjim

Your Macs hosts file is a small, but important text document that has the ability to map hostnames to specified
IP addresses. Although the modern Internet uses a variety of public and private DNS servers to map IP
addresses, the hosts file is a handy way to override those DNS servers, allowing you to manually point a
website address to a desired IP address, or block access to a site altogether by pointing to an unused or
internal IP address. Heres how to edit the Mac hosts file on OS X.

Stand out at the party or promote your business with colorful powder coated and custom engraved Yeti
tumblers from Perfect Etch.

Edit Your Mac Hosts File with Text Edit


There are two primary ways to edit the hosts file in Mac OS X. The first is by using TextEdit, since the hosts file is
merely a simple plain text document. However, you cant open the file directly, since it resides in a protected
area of the file system. Instead, we need to copy the file to an unprotected location, like the Desktop, edit it,
and then copy it back.

To find the hosts file, open Finder and, in Finders menu bar, select Go > Go to Folder. In the box, type the
following location and press Return.

/private/etc/hosts
A new Finder window will open and your Macs hosts file will be selected. Click and drag it out of the Finder
window and drop it on your desktop. This will let us freely edit the file.

To open it, simply double-click and it will display the files contents in TextEdit (or the text editor of your
choice). By default, the hosts file is relatively simple. It contains a number of lines of descriptive text which are
commented out with the pound or number sign (#). For each line, any text after the pound sign is ignored by
the computer, making it a good way to add notes and descriptions to your files. It also contains default IP
values for localhost and broadcasthost. To edit the file, youll add your own lines after broadcasthost.

A popular use for an edited hosts file is to block access to specific websites. In our example, well pretend that
the computer were using is a work system, and we want to prevent employees from accessing Facebook
(because were mean). To do this, type the IP address you wish to assign followed by a hostname. In our case,
we want to block Facebook so well map www.facebook.com to 0.0.0.0 which, as an invalid IP address, will
result in an error. Now, whenever anyone using the Mac tries to go to www.facebook.com, their Web browser
will fail to load the page.
Alternatively, you can enter the IP address of a valid site instead of 0.0.0.0, which will result in users attempting
to access Facebook being directed to the site of your choosing. To determine a websites IP address, you can
ping the site via Terminal. Simply open Terminal and type the following command, replacing
www.website.com with the website of your choice:

Mac
ping www.website.com
Windows
Mobile
Make note of the IP address thats returned and use it in your Mac hosts file mapping. For example, The New
Games
York Times website at www.nytimes.com returns an IP address of 170.149.172.130. If we map that to Facebook
Home Theater
in our hosts file, any time someone using the Mac tries to go to Facebook, theyll see the The New York Times
Reviews
load instead.
Editorials
Tips

Twitter
Facebook
Google+
Feed

You can add as many entries to the hosts file as you wish by entering new mappings on each line. Once youve
made your desired changes to the hosts file, save it in its current location on the Desktop. Then, drag and drop
the hosts file from your Desktop back to its original location at /private/etc. If youve closed the Finder
window pointing to this directory, simply use the Finder > Go > Go to Folder command, discussed above, to
reopen it.

When you drop the hosts file back in its original location, OS X will ask you what to do about the unmodified
hosts file thats already there. Choose Replace and then enter your administrative user password to
authenticate the transfer.

With the modified hosts file now in place, fire up your Web browser to test the changes. In most cases, the new
mapping should work immediately, but if youre not seeing the correct behavior, you may need to flush your
DNS cache. For OS X Lion and OS X Mountain Lion, open Terminal and use the following command. Note that
youll need to enter your admin password to execute it:

sudo killall -HUP mDNSResponder

For OS X Mavericks, use this command instead:

dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Edit Your Mac Hosts File In Terminal With Nano


The steps in the previous
Mac section are easy enough, but if you want to avoid copying the hosts file you can edit it
directly in Terminal using the UNIX Nano Text Editor, which is built in to OS X.
Windows
Mobile
To get started, launch Terminal, type the following command, and press Return. As with all sudo commands,
Games
youllTheater
Home need to also enter your admin password to execute it:
Reviews
Editorials
sudo nano /private/etc/hosts
Tips
Youll now see the hosts file open in the Nano editor.
ToTwitter
navigate and edit the file, use the arrow keys on your
keyboard.
Facebook
Google+
Feed

Just as we did with the TextEdit method, above, we can add, edit, or remove hostname mappings at will.
Because we launched Nano using sudo, any changes will be authenticated and saved directly to the original
hosts file, without the need to copy it outside of its home directory.

When youre done making changes, press Control-X to exit the editor, Y to save, and Return to overwrite the
existing hosts file. As we mentioned earlier, make sure to flush your DNS cache if you notice that your new
mappings arent working properly.

Our examples mentioned blocking and redirecting distracting sites in a work environment. You can also use
these steps to manually block access to malicious websites or, of course, to play a prank on friends and family
members. If you ever make a mistake and youre not sure how to fix it, you can always restore the default hosts
file contents by using one of the methods above to enter the following default information:

## # Host Database # # localhost is used to configure the loopback interface # when the syst

Want news and tips from TekRevue delivered directly to your inbox? Sign up for the TekRevue Weekly
Digest using the box below. Get tips, reviews, news, and giveaways reserved exclusively for subscribers.

3 0 3 5

CATEGORIES: MAC / TIPS Mac


Windows #OSX #TERMINAL #TEXTEDIT #TIPS #WEBSITES
TAGS: #HOW TO #NANO #NETWORKING

Mobile
Games
Home Theater
Reviews
Editorials
Sign
Tips Up for the TekRevue Weekly Digest

Twitter
Enter your email address
Facebook
Google+
SUBSCRIBE
Feed

READ MORE IN MAC OR TIPS ON TEKREVUE

MAC

You might also like