Block websites: hosts file

To block websites you can use a browser extension (such as Block Site), a proxy server (such as Squid), but there is also the option of editing the hosts file, a method that consumes very little RAM and, unlike the browser extension, will work for any browser or program that connects to the Internet1.

How does this method works?

You just have to add lines in the following format to the hosts file (/etc/hosts in GNU/Linux) with the pages you want to block:

0.0.0.0 example.com

This line causes that every time you try to connect to the site example.com instead you connect to the IP address 0.0.0.0.0, which is a non-routable address that is used to designate an unknown, invalid or not applicable destination. The result, after restarting the network service (sudo systemctl restart NetworkManager in Debian) and the web browser is the following:

It is also possible to redirect to another website, although most browsers will not let you see the website, as this has been used in the past for phishing.

If you want to block a lot of websites, adding hundreds of pages by hand is not practical. That is why there are people and projects that compile lists of sites. One example is the list managed by Steven Black, which provides block lists according to subject matter, such as pornography, fake news and gambling. Simply copy and paste from the list you want into your hosts file.


  1. This method won't work for programs that connect to the Internet using Tor, because the DNS is resolved inside the Tor network. 

Comments