16

Is there a way to temporarily block websites in Ubuntu?

For example let's say ... Facebook (or any other website) is just available from 8pm or just for five minutes within e.g. two hours?

Thank you!

Stücke
  • 872
  • 3
  • 9
  • 17
  • On unix.stackexchange, an answer to the question "[Two different /etc/hosts depending upon the time](https://unix.stackexchange.com/a/41498/85530)" explains how to have two versions of `/etc/hosts` called `/etc/hosts_worktime` and `/etc/hosts_playtime` and two cron jobs. One cronjob copies the given file to `/etc/hosts` at work time and the other copies the file at play time. – Paul Rougieux Sep 16 '20 at 06:57

5 Answers5

12

The tool I'd recommend is Get-Shit-Done. It edits your host file so that you cannot access blocked domains using any browser. However, it does not have a scheduler (although you could use cron for that).

Stay Focusd is Chrome Extension that does something similar.

I personally use a secondary profile in Chrome, with the Whitelist extension, so that it blocks all websites, other than absolutely necessary ones

Nemo
  • 9,332
  • 8
  • 47
  • 67
10

Open Terminal (CTRL+ALT+T).

Type sudo -i in the input field. Check the Run in terminal option. Finally click Run button.

Type your password if necessary and press enter. Then do the following command.

gedit /etc/hosts

You will get gedit Text Editor window.

For example, if we need to block Facebook just add the following lines just after 127.0.0.1 localhost.

0.0.0.1 facebook.com    
0.0.0.1 www.facebook.com

By doing this, it will block the site in all browsers including Google Chrome, Chromium, Mozilla etc.... In fact, you can't access the website at all by any means.

That's it. When you now open www.facebook.com or facebook.com, you cannot access it. To enable back Facebook, remove the lines we added from the file /etc/hosts or as Eliah Kagan said you can add a # character before the 0.0.0.1 text we added in /etc/hosts to make it a comment and to block it again just remove # character from the beginning of the line.

Example

Blocking :

0.0.0.1 facebook.com

Enabling It Back by making the line a comment :

#0.0.0.1 facebook.com

Source

Subin
  • 750
  • 4
  • 16
  • 32
  • 1
    I know the terminal is great and all but is there a GUI for this? – Parto Mar 27 '14 at 16:59
  • @AvatarParto See this http://askubuntu.com/q/114415/87199 – Subin Mar 27 '14 at 17:30
  • The block didn't work for `0.0.0.1 www.youtube.com`. Youtube was open in the web browser at the time. Probably due to DNS cache? I restarted networking with `sudo /etc/init.d/networking restart` and then youtube.com was blocked. – Paul Rougieux Sep 16 '20 at 07:48
6

If you use Firefox, the LeechBlock add-on looks like it might do what you wish.

Mark Tully
  • 838
  • 4
  • 8
  • Does anyone accidentally knows a similar add-on for Chromium? – Stücke Oct 25 '11 at 09:18
  • The link is broken, alternative on addons.mozilla.org [search/?platform=linux&q=leechblock](https://addons.mozilla.org/en-US/firefox/search/?platform=linux&q=leechblock) – Paul Rougieux Sep 16 '20 at 06:50
0

I think that this is important topic, so I've made python script to deal with issue https://gist.github.com/josifoski/3d1406bba37694e19fce9d1e032bcb99

josifoski
  • 121
  • 5
0

Ubuntu 20.04

Open a terminal type sudo nano /etc/hosts.deny or with any editor in sudo privileges.

Then add all the urls you want to block

www.facebook.com
www.xxxxx.com

Save the file and reopen your browser

Anand Jose
  • 173
  • 8