I know about SelfControl app, but it has to be started everytime and the block hours to be set.

I want for example to forbid loading certain sites during work hours, but make it possible during the rest. I want to set it once and forget. It has to do the switch automatically everyday.

What would be the easier way to do this, considering that i'm a super newbie.


I would recommend you looking at something like Dansguardian:


The documentation is quite good and there some resources on time based browsing restrictions.

  • Thanks, that will do but it kinda looks like a overkill for my super simple needs. Infact i need to block just 1-2 sites from 9 to 5 that's it :) – user240891 Jan 31 '14 at 17:19

We'll use a combination of Iptables, Bash Scripting and Cron to achieve this.


Iptables command to block a domain looks like this:

iptables -I INPUT -p tcp --dport 80 -m string --string "Host: yourdomain.com" --algo bm -j DROP

Similarly, unblock command looks like:

iptables -I INPUT -p tcp --dport 80 -m string --string "Host: yourdomain.com" --algo bm -j ACCEPT

Bash Scripts

Make a file, say, block.sh, in which you define Iptables rules to block outgoing connections the domains you want to block, and a reverse script to unblock them.

#! /usr/bin/env bash
iptables -I INPUT -p tcp --dport 80 -m string --string "Host: yourdomain.com" --algo bm -j DROP
# other stuff you want to do when you block a domain
# add blocking commands for each domain here

Run chmod +x block.sh to make the script executable. Now you can run it as /path/to/file/block.sh.

Thus, you'll have two scripts like:

  1. /path/to/block.sh
  2. /path/to/unblock.sh


Now, add a crontab entry to run your scripts like this:

Run crontab-e. In first run, it'll ask you to configure a text editor. Use nano, if you are not sure.

Add these lines to the end of the file:

0 9 * * * /path/to/block.sh
0 17 * * * /path/to/unblock.sh

Save the file with CTRL+O, press ENTER and then, CTRL+X to exit.

Now, at 9 am everyday, the first script will block all the domains, and at 5 pm, the second script will unblock them back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.