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
:

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

And that's it.
Now you should be able to see the commands in your phone, which you can toggle back and forth:

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.