Blocking Crap with a Hosts File

Fri, 15 Jul 2022

I absolute hate ads on the web, and I prefer to block them whenever possible. There are plenty of ways to do it; if you’re using Mozilla Firefox as your browser then uBlock Origin will do the job nicely, but if you want to block ads on a given machine at the lowest level possible and you haven’t gotten around to setting up your own local DNS using Pi-Hole then you want to get acquainted with /etc/hosts.

What is /etc/hosts?

On UNIX-like systems (GNU/Linux, the BSDs, macOS, Solaris, etc,) the hosts file (located at /etc/hosts) is a file that matches IP addresses to hostnames when DNS is not available, and generally maps the loopback IP address ( to localhost. You can also use it to map machines on your local network if your router lets you set static IPs but doesn’t provide local name resolution.

However, you can also abuse /etc/hosts to block hosts or domains. Here’s how you’d do it, one site at a time…

sudo echo "" >> /etc/hosts

You’ll need sudo (or doas on OpenBSD) to gain root privileges just long enough to run this command; users without administrative access aren’t permitted to tamper with /etc/hosts.

What about Windows?

Windows also has a hosts file. On Windows 10, it’s located at C:\Windows\System32\Drivers\etc\hosts. You will most likely find it in a similar location on older versions. Unfortunately, none of the commands I described above or the shell script I provide below will be of use to you.

Do I have to add every ad server myself?

Of course not. That would be an outrageous waste of time. Fortunately, Steven Black has a repository of hosts files that he regularly updates. He compiles it from various ad-blocking lists, and also includes additional lists for fake news, gambling sites, social media sites, and porn sites.

What if I want to customize it?

I had this problem myself. I had some custom blocks for sites I tend to browse, and it’s a pain to manually update the main hosts file myself. It was also a pain to manually select and download the file. So I came up with a shell script to automate all of that. I run this weekly.

#!/bin/sh -e

CURL_FLAGS="-o /tmp/hosts"

if [ -x "$(command -v ${CURL})" ]; then
    echo "Fetching new ad-blocking hosts file..."
    echo "\n# Custom blocks." >> /tmp/hosts
    echo "" >> /tmp/hosts
    echo "" >> /tmp/hosts
    echo "" >> /tmp/hosts
    echo "" >> /tmp/hosts
    echo "" >> /tmp/hosts
    ${SUDO} mv /tmp/hosts /etc/hosts
    echo "Placed new ad-blocking hosts file in /etc/hosts."
    echo "${CURL} not available\n";

You’re welcome to grab a copy of my update-hosts script and use it yourself. Just put it somewhere in your $PATH and make it executable.