How to Edit the Hosts File in Mac OS X with Terminal

The hosts file in Mac OS X is stored at /private/etc/hosts but can also be accessed at the more traditional location of /etc/hosts. If you’re looking to edit hosts, you’ll want to target the file located in /private/etc/ though.
We’ll walk through how to manually edit the hosts file in OS X Lion and OS X Mountain Lion with the command line using the simple text editor called nano. If you’d prefer to avoid the Terminal, you could try the easier method of using a preference pane.
- Launch Terminal , found in /Applications/Utilities/ or launched through Spotlight
- Type the following command at the prompt:
- Enter the administrator password when requested, you will not see it typed on screen as usual with the command line
- Once the hosts file is loaded within nano, use the arrow keys to navigate to the bottom of the hosts file to make your modifications
- When finished, hit Control+O followed by ENTER/RETURN to save changes to /private/etc/hosts, then hit Control+X to exit out of nano
- Quit out of Terminal when finished
sudo nano /private/etc/hosts
You can verify your hosts modifications immediately with ping, Safari, or any other network app. Changes take effect immediately though some adjustments may need to be accompanied by a DNS flush.
If you’d like to see how this entire process is accomplished before doing it yourself, watch the video below to see a demonstration of the hosts file being modified on OS X Mountain Lion to block the website ‘yahoo.com’ from loading:
Note: the procedure is the same with older versions of Mac OS X, though the path to hosts could be /etc/hosts.
Tips to Consider When Editing Hosts Files
The following tips go beyond OS X and apply to any hosts file, be it on a Mac, Windows, or Linux.
- The preceding IP address is where the following domain will resolve to
- Always add new hosts to their own unique line
- The # symbol functions as a comment, it can be used to add comments to hosts entries or to comment out hosts modifications
- You can block websites through hosts by adding them to the file and sending them nowhere, preventing access
- You can redirect websites locally using the same logic, perfect for setting up test domains
- With some modifications, it can be necessary to flush DNS cache with dscacheutil before the changes take effect
- For juggling multiple hosts files consider using a manager app like GasMask
- If the hosts file claims to be locked, it’s because you did not prefix the edit with the “sudo” command
Let us know in the comments if you have any questions or tips.

sudo vi /private/etc/hosts
I can’t remember how to do anything in vi and I’m probably not alone, I think unless you’re a frequent user vi/vim are too easy to forget.
But, to make it super easy, you can do this:
sudo open /private/etc/hosts
That takes it into Text Edit
Agreed that vi/vim are probably not something for the feint hearted. As mention in another comment, using Text Edit is not a good option for unix files. Best to stick to nano instead
Once you learn vi, you will be glad you did.
You can do things like search and replace based on regular expressions, sort lines, convert case, indent etc.
I have tried this in the past using but using large hosts files like from http://winhelp2002.mvps.org/hosts.htm caused my processor to run hot constantly when the internet is connected. Is it still like that?
You should never ever use Text Edit to edit unix based files.
If you want a free editor use TextWrangler, if you download TextWrangler from the Mac App Store you need to install the command line tools separately.
http://www.barebones.com/support/textwrangler/
There are 2 ways to edit system files with TextWrangler.
Open the file from the Finder with TextWrangler and then enter you admin password, or use the sudo command with the TextWrangler command line tool to open the file.
TextWrangler is a fantastic free text editor, highly recommended!
Ive never had any problems using Text Edit as long as you ignore all the problems of permittion issues.
You can use Gas Mask too
http://code.google.com/p/gmask/
What is a difference between /etc/hosts and /private/etc/hosts?
/private/etc/hosts is the source, /etc/hosts is hard linked to it. If you change one, both change. The proper one to edit is /private/ though.
It’s even simpler than that. There is only one host’s file and that’s in /private/etc/, /etc is a symlink (not iirc, a hard link) to /private/etc/. A symlink is, to all intents and purposes, the same as a Finder alias.
Therefore if you go to /etc/hosts (via the symlink) you’ll end up at the exact same file as if you got there via /private/etc/hosts (the ‘real’ path). It’s the same as if you make a Finder alias on your Desktop of your Documents folder and double click on it. That’ll take you to the same place in your file system as going directly to the Documents folder.
~W
I did NOT put that spurious apostrophe in the word host…
DAMN YOU AUTOCORRECT!
What I need is a list of known attack sites to add to my hosts file. I’ve seen a ton of them in my logs but I’ve never bothered to collect their domains.
Heres my set up
hosts file blocking
http://someonewhocares.org/hosts/
You can also use the hosts file to rickroll people, just add the youtube iframe code to the index.html in /Library/WebServer/Documents
then add an entry to the /etc/hosts file
127.0.0.1 somedomain.com
I used the rickroll hosts trick when setting up a laptop for my mate who didnt want his kids accessing things they shouldnt, by adding a list of unsuitable domains to the hosts file.
So whenever they try to look at naughty sites they get rickrolled.
Then install icefloor the gui frontend for the pf firewall in mountain lion
icefloor is made by Hany El Imam who also made the excellent noobproof and waterroof for previous versions of osx
http://www.hanynet.com/icefloor/
Then set up the emerging threats list with the pf firewall
http://ikawnoclast.com/2012/04/using-the-lion-pf-firewall-with-the-emerging-threats-list.html
I have been talking to Hany the developer of icefloor and he should be adding the emerging threats list to the next version.
Something is causing my hosts file to be reset on each reboot mt. lion on macbook pro 7,1
this is not happening on imac running same software…
anyone experiencing this?
ok, did some more looking and found this http://superuser.com/questions/354902/etc-hosts-getting-reset-in-lion
idk if you guys have ever done a post on this but it would be worthwhile. I run cisco vpn for work, go figure…
Is there anyway to use the hosts file to block facebook in Mountain Lion? I’ve added the entry to the hosts file, and trying to ping http://www.facebook.com shows that it won’t connect, however if I type http://www.facebook.com into Safari, it will still manage to go to facebook’s website. Does this have something to do with the facebook integration in Mountain Lion?
facebook redirects to https:// these days
you’ll need to add facebook.com and http://www.facebook.com to block it effectively
HUGE MISTAKE in these instructions.
The changes will not save with:
When finished, hit Control+O to save changes to /private/etc/hosts, then hit Control+X to exit out of nano
DO THIS:
When finished, hit Control+O, THEN CLICK ENTER to save changes to /private/etc/hosts, then hit Control+X to exit out of nano
Thanks! I was wary of doing anything wrong and messing things up beyond my own (very) limited abilities, and I would have spent a while googling more and trying to figure it out.
Hi I got a question, I just got my MacBook and I’m running mountain lion. I’ve tried editing my host file but when I open the window, there is nothing in there. I’m seeing everyone with default settings that you’re not supposed to mess with but I don’t have them in there. I have nothing! Is that normal?
It’s “Hosts” with an “s”, from the terminal if you open a file that doesn’t exist, it will create that file (but it’s empty, of course).
Actually with a small “h” too.
hosts
Funciono perfecto, Gracias!!
I messed around with terminal to unblock facebook in Vietnam and now I can’t re-access it in the States. What do I need to do? Can someone help?
why whenever i save my or hit command+o it looks for the hosts file…
can someone help me. whenever i save it, it looks for the host file…
the commands have to be :
1. sudo nano /private/etc/hosts
1.1. put your changes
1.2. CTRL+O (press ENTER in order to save)
1.3. CTRL+X (to exit nano)
2. sudo killall -HUP mDNSResponder
3. dscacheutil -flushcache
enjoy
My mac shows “access denied” even though I am the administrator. It suggests that I find the file and unblock it but I dont know how…
You need root access to edit the hosts file, this is NOT administrator access.
Use the sudo command in the terminal.
Mountain lion pretty much ignores /etc/hosts
it uses directory services now, and you need to learn to use dscl in terminal in order to tame it.
thank you so much!!!:)