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 (127.0.0.1) 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 "0.0.0.0 facebook.com" >> /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="/usr/bin/curl"
CURL_FLAGS="-o /tmp/hosts"
SUDO="/usr/bin/sudo"
#HOSTS_DIR="alternates/fakenews-gambling"
#HOSTS_DIR="alternates/fakenews-gambling-porn"
#HOSTS_DIR="alternates/fakenews-gambling-social"
HOSTS_DIR="alternates/fakenews-gambling-porn-social"
HOSTS_URL="https://raw.githubusercontent.com/StevenBlack/hosts/master/${HOSTS_DIR}/hosts"

if [ -x "$(command -v ${CURL})" ]; then
    echo "Fetching new ad-blocking hosts file..."
    ${CURL} ${HOSTS_URL} ${CURL_FLAGS} ;
    echo "\n# Custom blocks." >> /tmp/hosts
    echo "0.0.0.0 news.ycombinator.com" >> /tmp/hosts
    echo "0.0.0.0 teddit.net" >> /tmp/hosts
    echo "0.0.0.0 nitter.net" >> /tmp/hosts
    echo "0.0.0.0 tildes.net" >> /tmp/hosts
    echo "0.0.0.0 lobste.rs" >> /tmp/hosts
    ${SUDO} mv /tmp/hosts /etc/hosts
    echo "Placed new ad-blocking hosts file in /etc/hosts."
else
    echo "${CURL} not available\n";
fi

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.