1

As some sites like facebook are distracting to me, I have decited to edit /etc/hosts file in order to block (temporarily) those sites as an extra way to avoid these sites. As an example, my hosts file is like below:

127.0.0.1   localhost
127.0.1.1   user
127.0.0.1 www.facebook.com
127.0.0.1 www.youtube.com

However, when I'm home, I want to have access to those websites, and therefore I comment out those websites manually by opening the file again, and commenting one by one all the websites as shown below:

127.0.0.1   localhost
127.0.1.1   user
#127.0.0.1 www.facebook.com
#127.0.0.1 www.youtube.com

How can I create a script that does exactly this job? Commenting out the websites that are included in the hosts file, and removing the comment when I want?

3 Answers 3

3

It's pretty pointless to seek a technological solution to this problem IMHO, however

sudo sed -i '/facebook/ s/^[^#]/#&/' /etc/hosts

to insert a # at the start of any line containing the string facebook if it doesn't already start with one; and

sudo sed -i '/facebook/ s/^#//' /etc/hosts

to remove a leading # from any line containing the string facebook if it does.

1

you can write all of them on a single line like this:

127.0.0.1 www.facebook.com www.youtube.com

1
  • Thanks. However, I would like to avoid that, as I want to manually disable certain sites at times, and having them all in one line makes it a little bit more difficult. Also, as I mentioned at my question, I'm looking for a script that does the commenting automatically.
    – Jim Blum
    Jul 25, 2014 at 15:54
1

Who needs their own Self Control when there's an app to do it for ya?

http://www.omgubuntu.co.uk/2011/02/self-control-ubuntu

http://svn.jklmnop.net/projects/SelfControl.html

This app blocks whatever website(s) you tell for the time you tell it to. Best of all, it can't be undone once you set it. If you block site X for an hour, you'll have to wait an hour before you can use it again. Its persistent across reboots.

You can find the download for it under the "Installation" section at that second link.

1
  • Yes, I know the program, and I've used it in the past. However, as I mention at my question I am just looking for a script that helps me to comment and uncomment the websites automatically.
    – Jim Blum
    Jul 25, 2014 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .