Block bad stuff on the internet with host file
Published: 2023-01-14

What is /etc/hosts?

A hosts file, named hosts (with no file extension), is a plain-text file used by all operating systems to map hostnames to IP addresses.

In most operating systems, the hosts file is preferential to DNS. Therefore if a domain name is resolved by the hosts file, the request never leaves your computer.

As your machine gets started, it will need to know the mapping of some hostnames to IP addresses before DNS can be referenced. This mapping is kept in the /etc/hosts file.

In the absence of a name server, any network program on your system consults this file to determine the IP address that corresponds to a host name.

Following is a sample /etc/hosts file:

IPAddress       Hostname           Alias
127.0.0.1       localhost          deep.openna.com
208.164.186.1   deep.openna.com    deep
208.164.186.2   mail.openna.com    mail
208.164.186.3   web.openna.com     web

The leftmost column is the IP address to be resolved. The next column is that host's name. Any subsequent columns are alias for that host. In the second line, for example, the IP address 208.164.186.1 is for the host deep.openna.com. Another name for deep.openna.com is deep.

Having a smart hosts file goes a long way towards blocking malware, adware, and other irritants.

For example, to nullify requests to some doubleclick.net servers, adding these lines to your hosts file will do it:

# block doubleClick's servers
0.0.0.0 ad.ae.doubleclick.net
0.0.0.0 ad.ar.doubleclick.net
0.0.0.0 ad.at.doubleclick.net
0.0.0.0 ad.au.doubleclick.net
0.0.0.0 ad.be.doubleclick.net
# etc...

The use of host file

There are a lot of custom host file hosted on the internet, with a huge variety of options. Here are some nice sources:

StevenBlack's repo

github.com/StevenBlack/hosts

It offers 15 different host file variants, in addition to the base variant.

A snapshot of the variants table from StevenBlack's repo

oisd

oisd.nl

Has its own fancy website. Also provide a lot of options. I don't know if this is bigger than StevenBlack's, but at this point I am too afraid to find out. My PC once froze when I tried to open the largest file.

A snapshot from oisd's website

someonewhocares

someonewhocares.org

The host file is embedded directly onto the page. They also wrote a short guide on how to setup host file in some operating systems.

Honorable mentions

References