How to block ads and malware with your host files

The hosts file is a plain text file called hosts.txt that maps hostnames to IP addresses.

The Domain Name System (DNS) is a remote database used to translate the easy-to-understand and remember web addresses (URLs) that we are familiar with, to their ‘true’ numerical IP addresses that computers can understand: for example translating the domain name proprivacy.com to its IP address of 198.41.187.186.

In most operating systems the host file is resolved in preference to DNS requests, so if the hosts file resolves the hostname, the request never leaves your computer. This means the hosts file can be edited to block the domain names of ad servers, banners, third party cookies, and assorted other malware, adware and spyware.

For example, adding the entry “0.0.0.0 ad.doubleclick.net” to the hosts file will block all ads served by that DoubleClick server to any web page you visit.

0.0.0.0 is “a non-routable meta-address used to designate an invalid, unknown or non-applicable target.” An alternative address often used is 127.0.0.1, which is the localhost address, but 0.0.0.0 is preferred as it is faster (does not wait for a timeout) and it does not interfere if a local web server is running.

How to edit your hosts file Windows

Search for Notepad, right-click, and Run as administrator.

File -> Open > c:\windows\system32\drivers\etc\hosts. Under the last # enter:

0.0.0.0 [domain name] e.g. 0.0.0.0 google.com

Multiple hosts can entered, with each one on a new line.

Edit -> Save.

Note that editing the hosts file may cause an alert from your anti-virus program. If so, then “allow” the change. In our example, if you now visit www.google.com you will find the website blocked. To un-block a host, either delete its entry or add a # symbol in front of it.

Instructions for editing the hosts file in Ubuntu and Mac OSX can be found here.

Host lists

A number of websites provide lists of domains belonging to known adware and malware, etc. domains, which can be added to your hosts file.

The best of these that I know of is the StevenBlack’s Amalgamated host file, which “consolidates several reputable hosts files and merges them into a single amalgamated hosts file with duplicates removed.”

It currently contains 27,148 unique entries amalgamated from the following “high quality” sources:

Has worked for almost six years as senior staff writer and resident tech and VPN industry expert at ProPrivacy.com. Widely quoted on issues relating cybersecurity and digital privacy in the UK national press (The Independent & Daily Mail Online) and international technology publications such as Ars Technica.

18 Comments

ThaCrip

I used the MVPS HOSTS file (i.e. http://winhelp2002.mvps.org/hosts.htm ) which is apparently the most popular HOSTS file. but instead of loading it on Windows, like is typical, I got it running on my router with Tomato (Toastman) firmware using this script... http://www.linksysinfo.org/index.php?threads/auto-dl-hosts-file-and-install.21378/ so where as normally someone has to manually update it about once every month or so, as the MVPS HOSTS file typically gets updates about once a month, the router does it completely automatically (as it updates the MVPS HOSTS file once a week) and as a bonus, since it's running on the router, it will work for ANY device connected to the router (that obtains a IP address automatically from DHCP(which is what the vast majority of people use since it's easy/automatic)). NOTE: you need to have the router for the DNS servers because if you setup a manual IP address on your computer with manual DNS servers it will bypass the hosts file usage on the router. so for those, like me, who have a manual IP address on their primary computer instead of typing in the real DNS server IP address you simply use '192.168.1.1' (or whatever is the IP address of your router) and it will then use the routers HOSTS file. also, it's pretty easy to confirm whether the HOSTS file is active, be it through the router or manual installation on Windows etc, as you simply load up the command prompt and type 'ping ad2games.com' (without the ') and press enter and if pings that address then the HOSTS file is not working but if it shows, "Ping request could not find host ad2games.com. Please check the name and try again." ; then the HOSTS file is working. currently the MVPS HOSTS file contains 12,715 addresses as of it's most recent update on March 4th 2018. while there may be other HOSTS files online I suspect the MVPS HOSTS is a bit safer as it's probably less likely to interfere with legitimate websites and still offers decent protection online.

Douglas Crawford replied to ThaCrip

NoobUser

Hello, Could you please tell me if writing `0.0.0.0 doubleclick.net` would also block access to ad.doubleclick.net? I would have assumed yes, but some host files (e.g. Adaway's at https://adaway.org/hosts.txt) has separate entries for both `127.0.0.1 doubleclick.net` and `127.0.0.1 ad-g.doubleclick.net`. Shouldn't the former make the latter reduntant? Thanks.

Douglas Crawford replied to NoobUser

Hi NoobUser, Hmm. I think doubleclick.net and ad-g.doubleclick.net are different servers, and therefore require their own entries. Perhaps more importantly, given that we are just talking about adding another line of text to the Hosts file, I see no real harm in covering all bases. Even if this does create a little redundancy.

Jake

Douglas Crawford replied to Jake

Hi Jake, Simply open up the hosts file (as administrator), delete any entries you have added, and save. If you are very concerned, then you can always make a backup of the original hosts file. This will allow you to delete the modified file and replace it with the original, should you wish to revert back.

Mozef Kaddas

Thank you for your great effort in explaining the whole issue we face with add Block. I have been visiting few pages that I trust they are hardware reviewers like they review SSD, computers and etc,,, every time I browse this website they block me either I watch their videos or I disable my Adblock. Can you look at that matter with websites starting blocking you till you disable the Adblock ?

Douglas Crawford replied to Mozef Kaddas

Hi Mozef, Thanks for the suggestion. It is a great idea for an article. although I'm not sure there is are any foolproof solutions to the problem at the moment. FWIW, I use Privacy Badger instead of Adblock Plus. This is not banned by most websites (although some, such as Wired, still require me to disable it).