Score:1

Program to remotely block websites

nf flag

I am trying to remotely block websites on my ubuntu computer to help with procrastination. So something like a program I can install on ubuntu and on a smartphone (or a secondary windows PC) and when I run it on the smartphone it blocks access on certain websites on ubuntu, ideally on all browsers. Something like what Qustodio does for windows, or a remote leechblock.

I don't have massive discipline issues or technical knowledge, so it doesn't need to be some difficult to circumvent system. If I can turn it on and off from a secondary device, and need to go through some minor trouble to disable it on ubuntu, like uninstalling it or needing to look up a password or technical knowledge it's good enough. I don't live alone, so I can't just block websites on the entire network.

Thanks!

Score:0
vg flag

Host files

What we are gonna do is basically copy your original /etc/hosts.conf then edit it and make all troublesome websites (e.g. Facebook) to point to your own computer (127.0.0.1) so that loading these sites fails (unless you are running your own web server).

Block file

Create the following file and call it /etc/hosts.bob.block (the exact name doesn't matter though):

127.0.0.1   localhost
# WARNING: I am assuming your computer's name is bob. If not, grab it from /etc/hosts.conf
127.0.1.1   bob

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

127.0.0.1   www.youtube.com
127.0.0.1   youtube.com
127.0.0.1   www.facebook.com
127.0.0.1   www.facebook.com.ar
127.0.0.1   www.9gag.com
127.0.0.1   9gag.com
127.0.0.1   www.twitter.com
127.0.0.1   twitter.com

Enable file

We need to make a backup copy of your existing, original Host file and we'll call it hosts.bob.enable:

sudo cp /etc/hosts /etc/hosts.bob.enable

Toogle it locally

To block forbidden websites:

sudo cp /etc/hosts.bob.block /etc/hosts

To enable the forbidden websites again:

sudo cp /etc/hosts.bob.enable /etc/hosts

Personally, I've wrapped these two into scripts called enable_internet.sh and disable_internet.sh and placed them in my home folder.

Preparing for running as sudo from KDE Connect

We are gonna use KDE Connect to toggle internet from your phone. It should already be installed in your computer.

Otherwise just run:

sudo apt install kdeconnect

But KDE Connect cannot run commands as sudo. We need to fix that.

You can follow this answer from Unix Stack Exchange that creates a simple C program and workaround that (using SUID bit):

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
  
int main()
{
  setuid(0);
  system("cp /etc/hosts.bob.block /etc/hosts");
  return 0;
}

Make one app for enabling it, another for disabling it.

For security reasons, install the app you've created in in /usr/local/bin and make sure it's owned by root.

We are gonna call these two C programs enable_internet and disable_internet.

Toggling support from your phone

KDE Connect supports issuing remote commands from your phone; pre recorded on your system.

First pair your phone to your Computer. You can look online how to do that, but it is fairly easy (it doesn't need Bluetooth, just being on the same Wi-Fi is enough)

Second, open KDE Connect Indicator on your computer (launch KDE Connect Indicator app if it's not running), right click -> Configure.

Select your phone. Then look for Run commands: Run Commands in KDE Connect

Hit the blank box and add the option to enable and another to disable it:

Commands in KDE Connect

And that's it.

Now you should be able to see the commands in your phone, which you can toggle back and forth:

Commands as seen from the phone

Remarks

Browser (e.g. Chrome) cache DNS queries internally, thus if you disable internet and you've already visited those websites; you may still be able to access the blocked websites (like Youtube) for a few more minutes.

Restarting the browser should fix that.

The same goes for enabling internet again, although browsers are much more aggressive when DNS resolution failed; and usually it takes refreshing once to thrice to be able to visit the now-unblocked website.

master bob avatar
nf flag
Thanks for the answer. However the point of this system is that I can't disable it impulsively. With this idea, I can just run the script locally, which needs just 2 clicks.
Matias N Goldberg avatar
vg flag
Running the script locally will ask you for the password; is that enough? Also in my experience this is enough w/ procrastination. As muscle memory will immediately go to e.g. facebook and I will be met with "Error" and I'm like "oh right, I'm working". Making it more difficult just means making it so in the script.
master bob avatar
nf flag
Yeah, adding a big necessary input to run the script should do.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.