Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
How to permanently and irrevocably block websites on Firefox?

I'd like to write a script that modifies /etc/hosts for a certain period of time. Motivation is productivity.

My approach would be to define a block of text with the rules I want and then just put that into /etc/hosts. After the time is over I would search for exactly the same block and delete it.

Now I'm just curious if there is any better way to do that. Maybe a similar construct to those /etc/resolv.conf.tail files?

share|improve this question

marked as duplicate by ewwhite, Chris S Jun 20 '12 at 13:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

"Motivation is productivity." <-- Tell the employee if they don't get their work done you're going to fire them. If that isn't motivation enough then you need to actually fire them. No technology will ever motivate someone, you can't block off every way someone can waste time until they actually get something done, they'll just find another way to waste time. Seriously, this comes up on Server Fault about once a month and this "answer" is the only one that ever works. – Chris S Jun 19 '12 at 13:08
Actually I'd like to prevent myself from being unproductive when I have to get stuff done. I'm 20 and I'm not somebody's boss. – Martin Klepsch Jun 19 '12 at 18:19
@MartinKlepsch: Then this sounds like a self-control issue that you would be better off learning to self-manage rather than relying on technical implementations that you yourself designed and can disable on demand. Starting with this might help:… – Scott Pack Jun 21 '12 at 17:08

Are you trying to block or restrict access to certain websites for your employees during business hours? Would a proxy/web filter make more sense?

If that's not the case, you could leverage any of the standard configuration management utilities (Puppet, for example) to either replace the /etc/hosts file or modify it in place. You could also use echo or sed to modify a block of text and schedule via cron. Remember that the hosts file is read top to bottom...

share|improve this answer

After reading your initial post, I'm still not 100% sure what you want to do. If you want to modify /etc/hosts file, you can write a basic script to echo or cat >> the file and then put it in cron based on when you want it. So if you want to modify it at 7:00 am, then have the cron job run at 7:00 am and if you want to modify it back at 6:00 pm, then have another cron job delete your previous entries at 6:00 pm. There are a few ways you can do this on the scripting side. The easiest would probably be to make a few files in some directory called hosts1, host2, hosts3, etc. Then in your script, you can just replace the /etc/hosts file with whatever other host file you have modified. If you don't want to do that, like I said, you can echo or cat individual lines into the file and then use sed to delete them later.

share|improve this answer

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