hosts file

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.

Location of the hosts file

MacOS / Linux / Unix:




Editing the hosts file

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 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 or 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. is the loopback address (also known as localhost). is a non-routable meta-address used to designate an invalid, unknown or non applicable target.

Using the prefix may give a very small performance benefit compared to the traditional loopback address
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.

Examples  # Hateful content # 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.

Copyright © 1999-2023
Some rights reserved