The standard hosts file on most operating systems can be used to block websites or redirect URL's to a different host (overriding DNS).
There are a number of reasons for editing this file, you can make an addictive social media site hard to reach or prevent some scammy clickbait web site from getting even a single accidental page view from your machine.
Editing the hosts file is never going to provide much in the way of comprehensive anti-virus or anti-tracking protection, but if you just want to block a few websites from every web browser on your machine the hosts file provides a quick and easy way to do that.
MacOS / Linux / Unix:
The hosts file is protected against accidental or malicious changes. therefore editing it will require administrator or SUDO rights.
From the command line sudo vi /etc/hosts or launch a text editor 'As Admin', typically right click and Run as Administrator and then open the hosts file. In both cases you will be prompted for a password. On a Mac BBedit will automaticaly prompt to unlock the file.
Before making any changes, copy a backup of the hosts file somewhere else.
Notice that the hosts file does not have a file extension, (it is not hosts.txt) some text editors will only save a file without an extension if you surround the filename with quotes "hosts"
The default hosts file may have some comments and a localhost loopback which should be left alone.
Additional lines can be added to the hosts file, one address per line:
The syntax is
0.0.0.0 web_url #optional_comment
Each of the 3 parts must be separated by at least one space.
The first item is the IP address that we will redirect to, typically 0.0.0.0 or 127.0.0.1 either of which will block the content.
Blocked pages will return: "can't establish a connection to the server". Any host name can be redirected to to any IP address.
The second item is the web_url to be redirected, and the third item is an optional comment.
In cases where a website works both with and without a www. prefix, then you will need to add a hosts rule for both urls.
127.0.0.1 is the loopback address (also known as localhost).
0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target.
Using the 0.0.0.0 prefix may give a very small performance benefit compared to the traditional loopback address 127.0.0.1
This is only likely to be noticable if you add thousands of lines to the hosts file.
On Windows, a very large hosts file will cause high CPU usage from the DNS Client. The performance degradation from a large hosts file, or from disabling the DNS client altogether, is likely to completely negate the small performance benefit of blocking some urls.
0.0.0.0 www.dailymail.co.uk # Hateful content
127.0.0.1 www.boredpanda.com # clickbait
“Joel: You look familiar. Have we met?
Wilhelmina Slater: We're not even meeting now” ~ Ugly Betty, 2006 TV series
IPCONFIG /flushdns - Flush any cached DNS entries.
Q813878 - How to block specific network protocols and ports.
Q313190 - Use IPSec IP Filter Lists.