The Hosts file on a computer basically is like a phone book that you create that your computer checks to see if a URL should go someplace other than what a DNS server tells it too. You can tell your computer to load duckduckgo whenever anyone types in google or just to block all google access entirely.
You can think of it as a website, address, domain blocker as well. The problem is that most advice and how-to's on the internet regarding how to edit it on a Mac to block things is actually quite wrong. Sometimes so wrong that it won't block anything.
Most searches will return some method of editing the Hosts file via the Terminal. It works, doesn't require anything else to download and once you memorize a few simple commands it's not a big deal.
But there's a more familiar way if you just take a few moments to set it up.
1. Head to BareBones.com and download the free text editor TextWrangler.
2. Install it where you want. The Applications folder is a good place.
3. Put the icon in your dock or leave the Applications window open so you can see TextWrangler's icon.
Why are we using TextWrangler instead of something that comes with the Mac like TextEdit? It has to do with how the text in the hosts file is encoded. TextEdit doesn't really play nicely with the encoding in the hosts file because the hosts file is, to put it simply, a special kind of file. It's not just a simple plain-text file, and TextWranger knows how to handle this slightly different kind of file.
4. Hit Command + Shift + G for Go To in the Finder. An Open dialog will appear.
5. In the "Go to the Folder" text entry box type or paste in (without the quotes) "/private", just /private then hit Return. We do this this way because the folder "private" is hidden by default and we need to access a folder that's inside of it.
6. Look for a folder called "etc". Option + Drag it to your desktop to create an alias of it. (this is optional, but helps if you plan on altering your Hosts file frequently. If everything has gone correctly you now have an alias to "etc" on your desktop.
7. Double Click the etc alias to open it.
8. Tap the letter H to get close to the hosts file and scroll down if need be until you see a file simply named "hosts".
9. Now this file is a little different than files you deal with on your Mac day to day. It has a few permissions about who can access it set differently than files you created or use most of the time. That's okay, this is why we're setting all this up. It's fairly error-proof.
Okay, so now to edit the hosts file DRAG it on top of the TextWrangler application icon and let TextWrangler open it. It will very likely ask for your admin password. That's okay. It's asking because the hosts file is protected from inadvertent changes and it just wants to make sure you're you and you really want to change it.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
0.0.0.0 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
That's great. We're almost there.
Keep in mind this type of address blocking is just for this computer, the one where the hosts file actually resides, not your home network. That's for another post or you can just use a free domain blocker like OpenDNS which lets you block up to 50 domains for free, I think.
Let's say we want to block on this particular computer a website (or ad site, or music site, or spam site) called reallybadwebsite.com for whatever reason.
10. In TextWrangler directly under the last line start a new line (just like a regular word processing document) and type this:
0.0.0.0 reallybadwebsite.com
(note: that's a single space between the zero -those are ZEROS by the way- and the website url)
11. Hit save and enter your password if it asks. It asks because hosts being a special file it's protected from inadvertent changes. But since this is your computer and your password and your account it's okay. So now you have something like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
0.0.0.0 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
0.0.0.0 reallybadwebsite.com
And you just saved it. Guess what? You're done.
Try loading that address and it shouldn't work. Your web browser should complain that it can't find the site anymore. Why is this happening?
Your computer is checking this file to see if reallybadwebsite.com is listed there, it finds that it is and then looks where it should go, in this case an IP address of 0.0.0.0 which is now where. Spiffy, eh?
Some tutorials will insist you use the Terminal and I'll briefly explain that below. But you'll often see them mention something like this:
127.0.0.1 reallybadwebsite.com
With a TAB in-between the number and the address.
It turns out that 127.0.0.1 on a Mac isn't really a reliable place to send an address. Sometimes, believe it or not, the Mac will still figure out how to get to the address. It kind of says, "Oh that IP address is being used for something else already, so I'll just check a DNS server and see where else that address should go." It's just trying to help. Sending an address to all zeros sends it into a black hole.
Close TextWrangler and you're done, done.
Next time you want to edit the hosts file just open up the alias to "etc", drag the hosts file on top of TextWrangler and edit away.
THE TERMINAL:
I used to do it this way but over time I'd forget the exact commands or a computer would use a different type of editor and commands would change so I'd get confused but here's the basic idea:
- Launch the Terminal which is in /Applications/Utilities/
- Type in exactly (or copy paste)
sudo nano /private/etc/hosts
- Hit return and type in your administrator password and hit return. It won't show your password as you type, btw.
- The hosts file will be loaded but you'll have to use the arrow keys (no mouse) to get the cursor where you want it.
Type away.
- When you're done hit the key combo Control + O then Return, this will save the changes you made.
- Hit Control + X to get out of the text editor in the Terminal.
- Now quit the Terminal. You're done.
NOTE FOR LION USERS:
I haven't test this but I've seen a few replies on message boards saying that adding the 0.0.0.0 address.com at the end of the hosts file doesn't work in Lion.
What apparently works is adding it AT THE VERY TOP of the file, even above the double-hash "##".