How to block domains using hosts file

A popular method used to block ads or domains in browsers is to use adblocking addons. An alternate method is by using the hosts file of your operating system. This file has mappings of hostnames to IP addresses. By pointing domain names to the 0.0.0.0 IP address they cannot be resolved for access.

  • A popular source for hosts file is provided by Steven Black. Clone that repository to your computer:
$ git clone https://github.com/StevenBlack/hosts.git
$ cd hosts
  • If you have any entries in your local hosts file, then add them to the myhosts file in this repository.

  • To update the adblocking domain files to their latest versions and to add the myhosts entries to create a final hosts file run this command:

$ ./makeHosts
  • Replace your hosts file with this one:
$ sudo cp /etc/hosts /etc/hosts.orig
$ sudo cp hosts /etc/hosts
  • Optionally, you can also restart your name service cache daemon to flush its cache:
$ sudo service nscd restart

That is it! You can use any browser or application and the domains in the hosts file will not be accessible from them.

Tried with: Ubuntu 16.04

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s