Adverts are a common annoyance for most internet users. If you want to get rid of them then there are plenty of third party ad blockers around. However, if you want to get super-geeky then there’s a clever trick you can do using your Mac’s hosts file, and that’s what we’re going to look at today.
Apart from earning you many Geek Points, a hosts based approach has the advantage of not being bound to any one app (e.g. Safari) – it comes into play whenever any app accesses the internet.
About hosts files
Firstly, what is a hosts file? Put simply, a hosts file is a text file containing a look-up list of hostnames to IP addresses. For example, a web server’s IP address might be something unwieldy like “http://123.345.789.012”.
However, by editing your computers hosts file you can tell your computer that whenever you enter the address “http://myserver”, what you actually mean is “http://123.345.789.012”, and the computer will do the switcheroo for you automatically whenever you try to access that server.
So how does that help?
Adverts are usually hosted on servers other than the page you’re looking at. If we had an extensive list of known ad servers then we could use our hosts file to redirect links to those ad servers to something else. For example, that “something else” might be the ip address 127.0.0.0, which is almost certainly your Mac’s home address. Since we’re not running an ad hosting server from our Mac (hopefully!), those requests will fail and the ads would not load.
All we’re missing is a long list of known ad servers, right? Luckily, there is a list of them at someonewhocares.org which is regularly updated, and they also include many ‘dodgy’ sites including those with offensive content, known malware sources etc. So we have all the pieces of the jigsaw. Lets see how we can edit our hosts file to use this list.
Firstly, select Finder>Go To Folder, type “/etc/” in the box then click the “Go” button. (NB. without the quotes, but with the slashes!)
You should see a file called “hosts” in the folder you just opened. This is your hosts file. Make a backup copy of it and keep it somewhere safe, just in case.
Next, go to http://someonewhocares.org/hosts/ and copy the text from “# This hosts file brought to you…” down to the bottom (omitting the date that’s on the last line).
Open the hosts file in a text editor. I strongly recommend TextWrangler because word processors e.g. Word can add unseen mess into your file which can break things. If your Mac asks if you want to unlock the file, say “Yes”.
Paste your copied text into your hosts file and save it, making sure you save it as “hosts”, not “hosts.txt” or any other variant. NB. You may need your Mac’s admin password to save the file.
If you experience any problems, or want to undo what we just did then just replace your edited hosts file with your backup copy. If you find a site that you want to visit is blocked, simply open the hosts file, find the line with the site in it and either delete that line or add a # before the line to disable it.
If, after some time, you find you start seeing ads again then you may need to update the contents of your hosts file. Just go through the same procedure, but using the current list from someonewhocares.org – they update quite regularly.
Happy (mostly) add free internet!