Editing A Mac’s Hosts File 14
Tutorial
Hey
On your Mac there is a hosts file that controls certain IP and domain addresses. It works in a similar way to a normal DNS server. It translates web addresses to IP addresses. This file enables you to change this link. As a result you can block certain domains from loading or just have a bit of fun. You should only use this file if you really need to, as well as this changing this file is a little bit complicated and if you are new to the Mac, I would recommend skipping this tutorial.
The hosts file is in the /etc folder. To access this file in Finder to to Go > Go To Folder, and type in /etc. A normal finder window will appear. In this window find the file called “hosts” double click this file and open it in TextEdit. At this point you should be enabled as a root user, so you can quickly edit the file.
In this file you should notice a couple of things, there should be a comment area at the top signified by hashes (#), and a couple of entries in the file. Do not change these. They are very important to operation of you computer. Your file should look like something below.
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
The IP address in on the left and the domain is on the right. You can now add more domains and IP addresses, one per line. For instance if you want to block an ad server you would type the following:
127.0.0.1 ad.doubleclick.net
This will send any requests from the doubleclick.net address to a blank adress. Although I don’t recommend you do this for blocking adverts, something like Adblock Plus is a lot better it can be used for practically anything. Say for example you have a program accessing the web, you can put an entry in the hosts file. As well as this it is also very good for blocking sites that you don’t want your kids accessing. If you have a web server set up, you can forward it to that IP address on your local network telling them they are accessing something they shouldn’t.
To make the changes stick, all you have to do is save the file and flush your cache. Using the methods described in this post, clear your cache in Terminal. Once you have completed, the command the changes should take effect.
If you want to find out more about DNS and all its related workings I recommend the DNS for Dummies, I recommended it in a previous post and many people have said it sa really useful book.
Where To Next?
14 Responses to “Editing A Mac’s Hosts File”
-
1
Thanks a lot! It was really useful to me, being a new Mac user.
Comment By Jordas on September 8th, at 11:02 pm
-
2
it wont save,it said u dont have sufficient privilege to save.
Comment By Troy on December 5th, at 6:51 pm
-
3
Some have said to enter new entries between your example lines 1 and 2; other say between 2 and 3. What’s your take on this?
Comment By tlmurray on May 10th, at 1:47 am
-
4
if I accidentally removed or lost the default hosts file.
can reinstall it from or find it on the Back up disks that came with my machine?I’m referring to this comment
“In this file you should notice a couple of things, there should be a comment area at the top signified by hashes (#), and a couple of entries in the file. Do not change these. They are very important to operation of you computer. Your file should look like something below.”I’m an new user
and have a new mac.this was very helpful already
but before I found you I was sort of stumbling…Comment By limbert on July 24th, at 8:45 am
-
5
For both the file and directory you must right click, then “Get Info” Sharing and Permissions – click the unlock icon at the bottom and change the privilege to Read & Wrote for everyone.
Comment By Bob on September 11th, at 2:53 pm
-
6
Thanks for the helpful article.
For those comfortable with Terminal (the command line), here are equivalent instructions.
- Open the Terminal application (you can find it easily via Spotlight)
- Make yourself superuser with ‘sudo -s’ (password entry required) so you have sufficient rights.
- Run ‘nano /etc/hosts’ to open the hosts file for editing (nano is a Terminal-based plain-text editor)
- Add entries as described in the article.
- Save the file by pressing Ctrl+X, then Y, then Return.
- To make sure that the new settings take effect (Leopard+), run ‘dscacheutil -flushcache’
- Close the Terminal window.Comment By Michael on November 23rd, at 6:13 pm
-
7
Oh, and Terminal? Don’t be a show-off. Another complication that isn’t needed.
Comment By Lance on April 6th, at 10:43 am
-
8
@Riina – I like the NoScript addon for FireFox. I leave Google Analytics blocked, and their stuff never touches my computer. (I use this on all my various platforms.) Be forewarned, it’s a little onerous when you first start using it, but the more you allow the domains you use all the time, the less intrusive it becomes (as you use it, it plays nicer). That said, NoScript has saved me from myself many times when I go places I shouldn’t, and on top of that I never have to deal with stuff like Google Analytics. So, you know, totally worth it IMO. GL;HF. :)
Comment By Lars on June 3rd, at 6:07 pm
-
9
Here is how it’s done properly:
– Drag the Hosts file to the desktop. It will make a copy. Edit the copy in TextEdit and save.
– Drag the edited version back to the /etc window. You will get an “Authentication” message. Click to authenticate, type your password, and click to replace.This is simple and works perfectly
Thanks Lance
Comment By Brian on June 21st, at 10:55 am
-
10
Particularly useful blog post cheers for posting.
Comment By height increasing insoles on August 8th, at 3:41 pm
-
11
@Lance, when I try to replace the original Hosts file with the edited version, and after duly authenticating, I get an error “hosts file the operation cannot be completed because the required item is in use”
Comment By ben on September 18th, at 3:43 pm
-
12
I can’t find my “host” file in my Mac. Can you help me?
Comment By Jollene on November 29th, at 4:06 am
-
13
Thanks Brian, that worked perfectly for me, no need for terminal.
Jollene, I used Cocktail to enable invisible files, then just looked for the etc folder in finder, contains the hosts file, just drag to copy to desktop, open and modify, save back to desktop then drag back to where you found it (I just left the finder window open – easy!)
Comment By Sophie on December 1st, at 6:26 am
-
14
I am also getting the “hosts file: the operation cannot be completed because the required item is in use,” error when replacing the hosts file in the /etc folder. Please advise. Thanks.
Comment By Jason on December 29th, at 10:44 am