S. J. Kelley

Finding time to write: Blocking internet access at certain times of day in linux (and making it a recurring task)

Yet another productivity post. My switch to 3 days/week at the full-time job happened this week, but I had to use Tues/Thurs to do lecture prep as I was hired as a last-minute emergency replacement (teaching is my part-time job). My writing time will be less than optimal for the next 2 weeks until I finish all the lectures (or at least catch up a bit), but I did find myself with some spare time tonight around 9pm when the kids were in bed. I figured I’d get in a good 1.5 hours or so, but I wasted the time. Sigh. Warning: the rest of this post might be boring for Windows/Mac users, but if you run Linux, it may be helpful:

I run Linux Mint on my desktop and laptop, and for the last few months I used a modified hosts file to prevent myself from accessing distracting sites on my desktop computer. This is fairly simple. Just type:
sudo gedit /etc/hosts
Then add the following to the end of the file:
0.0.0.0 www.facebook.com
0.0.0.0 www.upworthy.com
Etc. When you try to access the site, it won’t be able to connect. This worked fine for awhile, but then I would do google searches and just get lost on other sites (like I did tonight). Sure, I’m reading and learning, but I really didn’t need to learn tonight; I needed to write. 
Ideally I wanted a script to turn off all internet traffic at certain times of day (like between 7:30pm and 11:30pm, after the kids are in bed but before I am!), but only on my computer. I found a script by zengargoyle that can do this. First you’ll need to install the “at” command (it’s not installed by default on Mint):
sudo apt-get install at
Then make a file:
sudo gedit /usr/bin/SelControl
Copy the following into the file:
#!/bin/bash
#from http://ask.metafilter.com/160649/Block-internet-on-ubuntu
PORTS=”80″ # OR enter PORTS=”80 443″ for https as well
UNDO=””

for p in $PORTS
do
  iptables -I OUTPUT -p tcp –dport $p -j DROP
  UNDO=”$UNDO iptables -D OUTPUT -p tcp –dport $p -j DROP;”
done

echo $UNDO | at now + 1 min # <– change to time you want internet off for
I chose not to block port 443, because I still wanted Google Drive to sync my files in the background. This means that secure websites (those starting with https) still work, so I can still go a simple google search to get a quick definition, but the results are duds 🙂 Once you save the file, you have to make it executable:
sudo chmod +x /usr/bin/SelfControl
And then you just run it whenever you want to block internet access:
sudo ./SelfControl
Now this was all well and good in principle, but what often gets me is that I’ll “just check one little thing” before writing… and that ends up being quite a lot of things; a whole writing block of time of things. I needed something I could set and forget. For that, I had to set up a cron job (as root, so I could change IPTABLES).
sudo gedit /etc/crontab
Then at the end of the file add:
# added by [your name] to shut off internet at certain times
30 19 * * * root iptables -I OUTPUT -p tcp –dport 80 -j DROP
30 23 * * * root iptables -D OUTPUT -p tcp –dport 80 -j DROP
The first number refers to minutes, the second to hours on a 24 hour clock, and the last 3 *s refer to day, month, and day of the week, so * for all three means every day (learn more here). The above turns off access to port 80 (http internet sites) from 7:30 pm to 11:30 pm daily. If you’re between your start time and end time on the first day you set this up, just type in:
sudo iptables -I OUTPUT -p tcp –dport 80 -j DROP
to turn off access, and the cron job will turn it back on again at the time you set. If there’s an emergency and you ABSOLUTELY NEED to access a website (that isn’t https), you can type:
sudo iptables -D OUTPUT -p tcp –dport 80 -j DROP
So my hope is that this will force some productivity on me by the sheer lack of anything else to do. That said, blogger is https, so I can still post during writing time, haha. If you’d rather turn off everything (at the risk of not syncing files in the background) just add port 443 to the above. I’m not quite there yet!

Leave a Comment

Your email address will not be published. Required fields are marked *