Auto-Update your Hosts File to Disable Adware and Other Web Junk

There's a great resource you should install on your Mac(s): a "hosts" file called "how to make the internet not suck (as much)." This is a huge file that masks tons of nasty websites that you probably don't want to see any content from: shock-sites like GoatSe, user-tracking sites, nasty popups, known trojan/malware sites, adware sites like doubleclick.net, and so forth.

I don't want to see any content from any of these domains, and you probably don't either. Fortunately, Dan Pollock has created this mammoth file, which, if properly installed, will block these domains from being reached. And, he updates the file pretty frequently as new honorees are inducted into the hall of shame.

Here's a technique I came up with to install and update this file on your system. Once a week, it fetches a fresh update to the file and installs it on your system.

It does take a bit of command-line-fu, however.

First, you need to become root in the terminal.  Type this, and then enter your password when prompted:

sudo su -

You should see a new prompt, something like this:

DansMac:~ root# 

First, get a current copy of the file, by pasting this line:

curl --silent http://someonewhocares.org/hosts/hosts | grep '^127.0.0.1' > /etc/hosts

Now type this to edit your system's (not your user's) crontab file. These are the periodic tasks that your system will perform.

crontab -e

This will open up a terminal-based text editor. Maybe you have configured that to your editor of choice (*cough*vi*cough*) or you can stumble your way through whatever is offered as a default.

Paste in a line similar to this:

10 10 * * 3 curl --silent http://someonewhocares.org/hosts/hosts | grep '^127.0.0.1' > /etc/hosts

You can adjust the parameters before the curl command.  In this case, it's going to fetch a fresh copy at 10:10 on the third day of the week (Wednesday).

Close and save the crontab editor, and you should be set!

There you have it.  My Christmas* gift to you.

* If you don't celebrate Christmas, consider it a belated birthday gift! Next year I'll try to send you a card!

Update: I've updated the command to only bring in lines that start with 127.0.0.1 so that it's impossible for this file, if somehow compromised, to do anything other than redirect a domain to your own machine.

© Dan Wood 2012-2016. My Comments Policy