rss

How To Block Websites In Ubuntu (Linux)

Just like Windows you can easily block websites in Linux too. However before Windows Vista everyone can edit HOSTS file but now only Administrator can edit HOSTS file in Windows Vista and Windows 7. Similarly in Ubuntu there exist a HOSTS file which can be used to block websites. Only root user is allowed to edit HOSTS file. So in order to edit HOSTS file from a normal account, you have to use SUDO command.

How To Block Websites Using HOSTS File

Open Terminal by navigating to Applications->Accessories->Terminal

Use the following command to edit HOSTS file

sudo gedit /etc/hosts

Suppose you want to block YouTube and Facebook on Ubuntu. Just add the following two lines in file as shown in the screenshot and click save.

0.0.0.0 www.youtube.com
0.0.0.0 www.facebook.com

Restart your computer. Now you won't be able to access these websites, no matter whatever web browser you use ;)

Note: You can find number of custom HOSTS file on Internet created to block advertisements and stuff etc.



  • Lech

    Thanks :) it's easy and very helpful

  • http://www.honda-bulgaria.com Stefan Lalev

    Extrimely helpful.Finnaly I get rid of facebook.com

  • http://www.intelgain.com Ganesh Kulpe

    Thanks it is working and it is really helpful

  • Ronnie

    Thanks!
    We are deleting ours accounts and I dont want accidently to login to facebook so this is perfect. And I also learn something new here.... thanks

  • Phirun

    i want to block facebook in my PC, i use ubuntu but i can't find " use commend line" in terminal. How can i find it?

  • Fred

    Works for me in Ubuntu 11.04. Make sure to restart your computer.

  • Csaba

    Not working at all on Natty Narwhal!

  • yooyo

    Do you have a way to block only a specific site of a web address? For example, I would like to block only http://www.example.com/news, but do not want to block http://www.example.com.

    Putting
    0.0.0.0 http://www.example.com/news
    in the hosts file does not work.

    (This is used only as an example, blocking news.google.com would work for the purpose of this example, but I would like to know how to block the partial websites directly. Adblock pluggins do not work well since it is too easy to change back and forth -- at the end indeed it is not technology but the mental toughness that counts)
    Thanks

  • enmae

    I've tried this. It was really useful.
    But when I type the IP address, it seems it doesnt work...
    I CAN enter the sites.
    for examle, facebook.com's IP address seems 69.63.181.12 .
    and I tried to add in /etc/hosts like this:
    0.0.0.0 69.63.181.12 (<- facebook)

    So how completely block sites on Linux?
    Mine is ubuntu 9.10 Mozilla.

    btw, this is cool blog! thanks .

    • http://jaxov.com/ Wolverine

      Well Facebook has different servers and each one of them has different IP address, so you need to enter site name (www.facebook.com) instead of the IP addresses which may change arbitrarily.

  • Tommy

    When i open the file it comes up as "Read only" so i cant remove the files.
    what do i do !

    • http://jaxov.com/ Wolverine

      You need to use SUDO command. (via Sathis)

  • aditra

    and how to unlock the facebook again?
    same with the procedure or there's something else?

    • http://jaxov.com/ Wolverine

      Just remove the lines you added in HOSTS file.

  • http://jaxov.com Jaxov

    Well only root users are allowed to edit HOSTS file. So either login as a root user or use SUDO before gedit /etc/hosts.

    Example: sudo gedit /etc/hosts

  • muhin

    i can not save hosts file bcz this file is read only..... how can i save hosts file?????

  • Lozu

    Thanks very much, that was hell lot useful.

    • http://jaxov.com Wolverine

      You're most welcome Lozu :)

  • Ruslan

    thank you. that was useful, forced myself to block all social networks + livejournal + some torrent sites as they were killing my time:)

  • SM2099

    Hi. Great tip. Although I was wondering if it could be used on a Linux box enabled as a bridge. Since the bridge is just letting data inside and out... if you ad these lines of codes to the /etc/hosts, will that maybe is a firewall-esque sense block websites to people who are conected to the the bridge and trying to access the websites going through it?

    Great site and thanks in advance.

  • rozi

    thx, it's really efficient and helpfull