How To Easily Block Websites Directly From The /etc/hosts File On The Most Popular Linux Systems

Hello Linux Geeksters. In this article I will show you how to block sites on your local computer (in order to forbid yourself or others to visit some sites, obviously) directly from the /etc/hosts file, on your Linux system.

There were some times when I needed to block facebook, 9gag and some other distracting sites, in order to stay focus on reaching my goals.

How To Easily Block Websites Directly From The /etc/hosts File On The Most Popular Linux Systems

Blocking websites can be easily done from the commandline, by editing the /etc/hosts file. There are two methods for doing this.

Method one: Append 0.0.0.0 http://the-site-you-want-to-block.com at the end of the /etc/hosts file. Because it is a configuration file, stored under /etc, you need root privileges.

Before you do this, backup the /etc/hosts file, in order not to erase it accidentally: sudo cp /etc/hosts /etc/hosts.bak

Example: Block Facebook.

$ sudo sh -c 'echo "0.0.0.0 http://facebook.com" >> /etc/hosts'

Example: Block 9gag:

$ sudo sh -c 'echo "0.0.0.0 http://9gag.com" >> /etc/hosts'

Method two: Append 127.0.0.1 http://the-site-you-want-to-block.com at the end of the /etc/hosts file. Because it is a configuration file, stored under /etc, you need root privileges.

Example: Block Facebook.

$ sudo sh -c 'echo "127.0.0.1 http://facebook.com" >> /etc/hosts'

Example: Block 9gag:

$ sudo sh -c 'echo "127.0.0.1 http://9gag.com" >> /etc/hosts'

Because this guide is generical, it works on the most popular Linux systems, including Ubuntu, Linux Mint, Elementary OS, Debian, Fedora, OpenSUSE, Mageia, Arch Linux, CentOS, ROSA Desktop, Slackware and many others

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , , , , , , , , , , ,
Posted in The Linux and Unix Articles!
5 comments on “How To Easily Block Websites Directly From The /etc/hosts File On The Most Popular Linux Systems
  1. Formoza says:

    What is style http://www.gatellier.be/blog/wp-content/photos/macosx-sudo-pico-etc-hosts.png ? and graphical environment ?
    Thank you in advance for your answer:)

  2. M Arenas says:

    For me it didnt work with the http:// . It worked this ways:

    sudo sh -c ‘echo “0.0.0.0 http://www.facebook.com” >> /etc/hosts’

    (with root privileges)

  3. Bla says:

    I’m wondering why this happens (writing 127.0.0.1 http://www.site.com blocks site.com), or even writing 127.0.0.2 http://www.site.com , what is the reason?

    • Geekster says:

      the reason is that the computer first looks in the /etc/hosts file resolving websites and if there is no matching entry, it asks the DNS service.
      127.0.0.1 is a localhost IP used for blocking websites.
      it would work if 0.0.0.0 was used, or 8.8.8.8, which is the Google DNS.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!