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.
Contact Us for News Tips, Corrections and Feedback
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
It worked. Thank you so much!
i disable that facebook and install some app means unblocker tool holo blocker and still running give me the solution for permanent please
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
thankyou!!
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.
i need password protection…like k9 ….does linux hav that?
Its really a good source to website in Linux. Thanks for such a great help…..
works in 12.10
Thanks
good. working in 11.4 also.
It’s working fine for me,
Thanks.
how to unblocke a website after blocking it. thx
Dennis
Thanks :) it’s easy and very helpful
Extrimely helpful.Finnaly I get rid of facebook.com
Thanks it is working and it is really helpful
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
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?
Works for me in Ubuntu 11.04. Make sure to restart your computer.
Not working at all on Natty Narwhal!
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
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 .
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.
When i open the file it comes up as “Read only” so i cant remove the files.
what do i do !
You need to use SUDO command. (via Sathis)
and how to unlock the facebook again?
same with the procedure or there’s something else?
Just remove the lines you added in HOSTS file.
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
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 !
i can not save hosts file bcz this file is read only….. how can i save hosts file?????
Thanks very much, that was hell lot useful.
You’re most welcome Lozu :)
thank you. that was useful, forced myself to block all social networks + livejournal + some torrent sites as they were killing my time:)
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.
thx, it’s really efficient and helpfull