How to block adverts with a custom hosts file

This is a short post on combating unwanted adverts and blocking access to other sites network-wide, its title is ‘How to block adverts with a custom hosts file’ and I will show you a technique that will allow you to setup a file in your router which will be automatically updating, and will block adverts on all devices in your network automatically. Most internet-savvy people will install some adblocker software to block adverts in their browser. This is good :)

They may even install anti-virus and anti-malware software to remove nasties when they do get installed. This is good practice, but it’s not not ideal. Preventing these kind of things from being anywhere near your machine in the firstplace is a better approach :)

someonewhocares.org have a custom hosts file that you can copy to your machine that will block a lot of the crap by not letting your machine connect to their known server names.

Instructions from the site are pretty simple,

# For Linux, Unix, or OS X place this file at “/etc/hosts”. You will
# require root access to do this. Saving this file to “~/hosts” will
# allow you to run something like “sudo cp ~/hosts /etc/hosts”.

Editing the hosts file on a single machine

How to block adverts with a custom hosts file

You can use gasmask in osx to edit your hostfile and save various profiles which allows you to test this configuration out and easily switch to others.
If you do use gasmask, then you can select the text on the someonewhocares site and paste it in to the gasmask app and save the file and it will just work :)

Network-wide host blocking

I have a couple of posts that will interest you if you are looking to enable network-wide blocking, the first details how to setup an internal network DNS-cache (the important pre-req for the advert blocking)
https://jonathansblog.co.uk/ddwrt-dnsmasq-caching-dns

The second post shows you how to set a custom, auto-updating hosts-file to use with your DNS-cache which will block access to the specified hosts for all machines in your network.
https://jonathansblog.co.uk/using-dnsmasq-as-an-internal-dns-server-to-block-online-adverts

The third shows how to get all of this on a raspberry PI which you can plug into your router
https://jonathansblog.co.uk/pihole-on-a-raspberry-pi

Leave a Reply