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 www.bestvpn.com to its IP address of 184.108.40.206.
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.
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:
- The Adaway hosts file, updated regularly.
- MVPs.org Hosts file at http://winhelp2002.mvps.org/hosts.htm, updated monthly, or thereabouts.
- Dan Pollock at http://someonewhocares.org/hosts/ updated regularly.
- Malware Domain List at http://www.malwaredomainlist.com/, updated regularly.
- Peter Lowe at http://pgl.yoyo.org/adservers/, updated regularly.
- My own small list in raw form here.
StevenBlack defines a “high quality” source as one that is actively curated, with the bigger the hosts file, the more curation required!
The actual file in the repository you want is called hosts. To use, simply cut & paste the host entries into your hosts file, then Save.
Hosts file vs. browser - based ad blocker
Host file pros
- Domain resolutions take place at a very low level (so are quick)
- Use no memory or processing power
- Blocks ads and malware across the entire OS, not just the browser
Host file cons
- Difficult to enable/disable on a per-site basis
- Can only block entire domains (not individual elements)
- Hosts file lists usually updated less often
- Easy to enable/disable from with the browser on a per-site basis
- Higher granularity (page elements, wild cards)
- Only blocks elements within the browser
- High memory and processing demands
Personally, I use both, as I really hate ads!
Editing the hosts file makes a great alternative to using parental control software.
Using a VPN often causes the OS to bypass checking the hosts file. The uBlock Origin browser plugin, however, can be set to use hosts files such as those by Dan Pollock's and MVPs.org.
Go to the uBlock Origin dashboard -> 3rd party filters (second tab at top) -> Multipurpose (about half way down)
My thanks to reader ihavenoname for sending this tip in.