One hosts file to block them all
Tags: webperf, tools, productivity
Published: 2018-09-13 14:35:57

---
title: "One hosts file to block them all"
published: true
canonical_url: https://borisschapira.com/2018/09/one-host-file-to-block-them-all/
description: "As a field expert, people often ask me how to improve websites' performance. Sometimes, they also ask me how to improve the Web's performance on their own machine, for their own browsing experience. In that case, my answer is always the same: the lowest hanging fruit is most certainly the **hosts** file."
cover_image: https://thepracticaldev.s3.amazonaws.com/i/bnmxgbp221a3wtw1eddl.jpg
tags: webperf, tools, productivity
---

As a field expert, people often ask me how to improve websites' performance. Sometimes, they also ask me how to improve the Web's performance on their own machine, for their own browsing experience. In that case, my answer is always the same: the lowest hanging fruit is most certainly the **hosts** file.

## Keeping the dirt away

Today's web is full of dirt. Most sites are full of trackers, ads, and lots of other nasty stuff penalizing websites' loading. To avoid this, AdBlockers are blooming. They offer a quick and simple solution (most of the time, a browser extension) that blocks most of the unwanted content.

[^pr]: Adblocking penetration rate in the US is ~30% on Desktop, ~12% on mobile, according to this [eMarketer study on Ad Blocking from March 2017](https://www.statista.com/statistics/351862/adblocking-usage/).

Unfortunately, as for the UX, AdBlockers don't deliver. They often increase the amount of memory and CPU cycles used by your web browser, slowing your browsing experience instead of boosting it. Some are doing better than others, and whole browsers[^brave] have been conceived over the idea of blocking unwanted content, and are doing an incredible job. But the web is not confined to your browser, is it?

[^brave]: If you've never tested [Brave Browser](https://brave.com/), I can only encourage you to do so, and join the 4 million people that trust it to fix the web.

The Web is requested from everywhere in your computer. Most mainstream applications are as crammed with trackers as your next media website. Sometimes, you can even see the ads displayed in your UI (_Hello, Skype. Yes, I'm talking about you, you naughty boy_).

The AdBlockers can't do anything about that. But a simple hosts file does.

## Hosts… what?

> The computer file **hosts** is an operating system file that maps hostnames to IP addresses. It is a plain text file.
> <cite>"[hosts (file)](https://en.wikipedia.org/wiki/Hosts_%28file%29)", on Wikipedia</cite>

Whenever your browser interacts with a website, it actually requests a server, located through its IP address, like 185.31.40.11 (IPv4) or 2a00:b6e0:1:20:2::1 (IPv6). It's pretty similar to a postal address.

Now think about accessing a webpage, like <https://borisschapira.com>. How does your browser know which IP address to contact? Well, the browser simply asks the Internet phone book. At least, one of them. And the phone book, called a <abbr title="Domain Name Server">DNS</abbr>, resolves the IP address for the browser.

I don’t know about you, but I never use a phone book. Most of the time, I’ve collected the address of the people to whom I want to write in my personal address book. That’s the **hosts file** for you. Every time a process on your computer needs to access a resource on the Internet, it first goes through the hosts file to find out where to find it.

Now, let’s say that you don’t want this process to find the resource on the badthings.com domain. _Easy peasy_, throw it on the wrong track by associating the badthings.com domain to an unspecified address like 0.0.0.0.

So basically, if someone makes a list of all the domains where bad things happen, we can redirect them all to 0.0.0.0 in our hosts file, and make **our own Web** a much cooler place.

## One project to gather them all

There is nothing new in what I am describing here. Cool people have been doing this for years, sharing their host files. Fake news websites, gaming platforms, pornographic hubs, encryption pages, fraud attempts, and scams are all patiently identified and listed in open access files.

I use [Steven Black's "hosts" project](https://github.com/StevenBlack/hosts), a python script, to cram them into [a single 2MB hosts file](https://raw.githubusercontent.com/borisschapira/hosts/master/hosts) containing more than 70k domains. In case you're wondering, I can and do alter the result with my own whitelist (otherwise blocked domains whose requests I don't want to prevent) and redirections (which allows me to write this article and check the result on https://borisschapira-dev.com:10443/, which actually points to my own machine).

If you're not familiar with the command line and use Windows 10, the [hostsman](http://www.abelhadigital.com/hostsman/) app will help you achieve the same goal (_unfortunately, as I just published the post, the website looks down. Fortunately, [a portable version exists](https://portapps.github.io/app/hostsman-portable/)_). I will not recommend modifying your host file on previous versions of Windows even if [I did it, before 2015 (FR)](https://borisschapira.com/2015/08/de-windows-a-mac/), unless you like to run `ipconfig /flushdns` every 30 minutes. On Linux, I've heard people talk about using [dnscrypt-proxy](https://github.com/jedisct1/dnscrypt-proxy)… but I've never tried myself.

<figure>
![A screencapture from hostsman sources manager](https://thepracticaldev.s3.amazonaws.com/i/xp1xk5w8trkjx6cxblqt.png)
<figcaption>You can manage Update Sources in hostsman</figcaption>
</figure>

## Some tips from my daily life

Sometimes, I need to temporaly disable this hosts file. For example, when I need to help [Dareboost](https://www.dareboost.com/) clients understand the impact of third-party scripts on their pages (thus, I need to load these 3ps myself).

To enable or disable my hosts file, I use a command line function that I've develop and put in my `~/.profile` file[^ozsh]:

[^ozsh]: Actually, I put this function in my `~/.zprofile`, as I use [Oh My ZSH](https://ohmyz.sh/).


```bash
function togglehost() {
    if [ -e /etc/host.bak ]
    then
        sudo mv /etc/host.bak /etc/hosts
        echo "Hosts file is active again"
    else
        sudo mv /etc/hosts /etc/host.bak
        echo "Hosts file is set aside"
    fi
}
```

Once this function is loaded, activating or deactivating my hosts file is very simple:

```terminal
$ togglehost
Hosts file is set aside
$ togglehost
Hosts file is active again
```

But as I sometimes forget to put it back in place, I have set up a routine that checks every minute that the file is in place and, if it is not the case, warns me by using Jaime Piña's [noti](https://github.com/variadico/noti) to trigger a notification. To perform this regular check, I use `crontab`.


```
* * * * * if [ ! -e /etc/hosts ]; then /usr/local/bin/noti -t "Host file" -m "does not exists"; fi
```

I have been using this technique for years now and, sometimes, I forget that my hosts file protects me so much. Whenever I need to use someone else's computer, or temporarily disable my hosts file, I realize the level of comfort it provides me.

I'm well aware that tweaking its own hosts file is a good but technical solution. It doesn't completely replace an AdBlocker (or I haven't aggregated enough files yet) but it's an incredible performance gain, which I highly recommend for everyday browsing.

Try it for yourself, and tell me what you think!

***

## Summary

Hosts files are your computer internal address books that guide Web requests to the right servers. Fill your hosts file with domains pointing to nothingness, and those requests will quickly and surely fail. People are sharing their hosts file for years. Solutions now exist to concatenate them, and crowdsource a solution to the dirty Web we've to deal with everyday.