See more...

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.

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.

Contact Us for News Tips, Corrections and Feedback
Leave a message...

    giratina6 years, 8 months ago

    Thank you it works very well.However sudo should never be used for graphical applications like gedit, only for work in the terminal like with nano.If you want to use gedit you should use gksudo!Like this:gksudo gedit /etc/hosts

    Peter6 years, 11 months ago

    It worked. Thank you so much!

    suresh7 years, 1 month ago

    i disable that facebook and install some app means  unblocker tool  holo blocker and still running give me the solution for permanent please

    desprit8 years, 7 months ago

    Sorry, this page deletes carriage returns — To open hosts hosts file with admin privilage you can also; — open
    terminal, alt+t — enter – sudo nautilus — enter admin password — nautilus will
    open with admin privilages — navigate to etc/hosts and open it with gedit.  — 
    You can edit hosts and save — close gedit, nautilus, and terminal

      ito8 years, 4 months ago


    steven8 years, 9 months ago

    I try this and it opens up a blank document. I got through with the file manager, and it doesn’t allow modification. Please help.

    raja8 years, 10 months ago

    i need password protection…like k9 ….does linux hav that?

    Santosh Shetty8 years, 10 months ago

    Its really a good source to website in Linux. Thanks for such a great help…..

    baldtaco8 years, 11 months ago

    works in 12.10

    Tulgaa9 years ago


    hrit.path9 years ago

    good. working in 11.4 also.

    Gdsmr0789 years, 7 months ago

    It’s working fine for me, 


    Calviccool9 years, 8 months ago

    how to unblocke a website after blocking it. thx

    Lech9 years, 11 months ago

    Thanks :) it’s easy and very helpful

    Stefan Lalev10 years, 1 month ago

    Extrimely helpful.Finnaly I get rid of

    Ganesh Kulpe10 years, 3 months ago

    Thanks it is working and it is really helpful

    Ronnie10 years, 3 months ago

    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

    Phirun10 years, 3 months ago

    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?

    Fred10 years, 4 months ago

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

    Csaba10 years, 5 months ago

    Not working at all on Natty Narwhal!

    yooyo10 years, 10 months ago

    Do you have a way to block only a specific site of a web address? For example, I would like to block only, but do not want to block

    in the hosts file does not work.

    (This is used only as an example, blocking 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)

    enmae11 years ago

    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,’s IP address seems .
    and I tried to add in /etc/hosts like this: (<- facebook)

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

    btw, this is cool blog! thanks .

      Wolverine11 years ago

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

    Tommy11 years, 1 month ago

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

      Wolverine11 years, 1 month ago

      You need to use SUDO command. (via Sathis)

    aditra11 years, 3 months ago

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

      Wolverine11 years, 3 months ago

      Just remove the lines you added in HOSTS file.

    Jaxov11 years, 5 months ago

    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

      nyandwi8 years, 6 months ago

      hello ! i have a computer working as a firewall/router and it uses Ubuntu linux 12.04. so, I want to block some websites so that no one can access them inside the organization’s LAN. HELP ME. THANKS !

    muhin11 years, 5 months ago

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

    Lozu11 years, 7 months ago

    Thanks very much, that was hell lot useful.

    Ruslan11 years, 7 months ago

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

    SM209911 years, 8 months ago

    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.

    rozi11 years, 8 months ago

    thx, it’s really efficient and helpfull