3 min read
Free System-Wide Website Blocking to Increase Productivity
Published on:
Create a free system-wide website blocking with a domain list. Use existing ones or create your own with this simple script.
How I Improved My Productivity
I wanted to block social media and Hungarian news sites on my PC and phone. I knew a free way of doing it: hosts file. In this post I will write down the details of how I did it.
What Is a Hosts File
The key for our website blocking is the hosts
file. It is a simple text file that maps hostnames to IP addresses. It is present on all popular operating systems. You can look at it like a local DNS server. Each line is an IP address and a hostname separated by a whitespace. When a hostname is needed, the operating system checks this file if there is a match for it, and if so, it uses the IP address specified.
There are several purposes it can be used for, but now we will use it for blocking. We will achieve blocking by mapping hostnames to invalid public IP addresses. Typically, 127.0.0.1
or 0.0.0.0
is used as the IP address. Editing the hosts
file requires administrative privileges on most operating systems.
Example of blocking youtube.com
. Because of a www
version is available we have to write that out too.
0.0.0.0 youtube.com
0.0.0.0 www.youtube.com
Using an Existing List
There are a countless number of lists that you can use. The most popular repository is GitHub: Steven Black’s hosts. Here you can choose from 15 different variations (for example, social media and adult). I recommend using at least the Unified hosts base list to remove adware and malware hostnames from your device. You can download your chosen list by following the methods it describes or simply by downloading the variation hosts file from GitHub. I decided to download the social list from GitHub.
Making My Own List
First, you will need a list of domains you want to block. You can write it on your own or search for it. My target was Hungarian news sites, so I searched for an article about the top most used sites in this category and used that as my source.
Snippet from my list.txt
file:
index.hu
origo.hu
24.hu
blikk.hu
I wrote a simple python script which transforms my file into a hosts file. It will process my list and transform it into the correct format. During this, it also checks for the www.
format of the hostname. If it exists, then it blocks that too.
from requests import get as requests_get
input_file_path = "list.txt"
output_file_path = "hosts.txt"
target_ip = "0.0.0.0"
def is_www_available(domain):
try:
return requests_get(f"http://www.{domain}").status_code == 200
except:
return False
with open(input_file_path, "r") as input_file: hostnames = [l.rstrip() for l in input_file.readlines()]
with open(output_file_path, "a+") as output_file:
for hostname in hostnames:
output_file.write(f"{target_ip} {hostname}\n")
if is_www_available(hostname): output_file.write(f"{target_ip} www.{hostname}\n")
After having a long existing list and my own list I combined the two into one hosts
file.
Using the Hosts File
If you are new to this topic, use the guide by Steven Black: Updating hosts file on Windows. On phone, you will need to download a DNS application and in it import your block list.
I use Linux as my primary OS on my PC, so all I had to do is replace the existing hosts file with the new one. I use an Android phone, so I downloaded personalDNSfilter in which I quickly imported my file. Now I blocked sites I did not want to see both on my PC and phone.