Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm interested in being able to temporarily prevent myself from editing my /etc/hosts file for the sake of productivity. When I am trying to get work done, I like to block access to the websites that are the biggest timesuck for me and I generally choose to do so through the /etc/hosts file.

I was hoping to be able to write a script that I can run when I need something like an hour or two of purer productivity. From what I know about all of this stuff (and that isn't a great deal), it seems like the ideal script would be capable of going into my /etc/hosts file and blocking the sites I don't want access to. Then ideally it would somehow lock that file and prevent me from accessing it. Then at the end of the predetermined time period, maybe a cron job would unlike the sites again.

Any ideas on the best way to handle this? Specifically with regards to preventing my own access to the file for a predetermined period of time.

share|improve this question
If you do it yourself, you can always get round it if your willpower's that weak. Perhaps see How can I block a website in a way that isn't easily reversible, or requires a password to reverse? for suggestions. – Karan Feb 18 at 1:18
Ultimately, this sounds like a bad idea. What if it breaks somehow, and you aren't able to fix it? Not even that, though -- there is always a way to undo something you've done to yourself. You are looking for a technical solution to a meat problem. – JDS Feb 18 at 1:29
> maybe a cron job would unlike[sic] the sites again - and what's stopping you from manually running the unlock script? What if you decide to use a proxy? You just need some more self-control.. or maybe get a friend to slap you when you get distracted. – Bob Feb 18 at 1:58

closed as not a real question by Xavierjazz, Diago, Nifle, Renan, Everett Feb 18 at 13:28

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

2 Answers

You could maintain 2 files and then swap them with a script...

share|improve this answer

While your question does not make much sense to me, I can give you an idea, which could be applicable when you want any program, even running as root, from being able to edit or remove some file - which could be /etc/hosts, or /etc/resolv.conf or something else.

Idea is to change Linux file attributes using chattr. From man chattr:

A file with the `i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

So, you use this command to make it immutable:

sudo chattr +i /etc/hosts

This effecively "nails" file down hard - no changes are permitted by anybody (including root) as long as immutable bit is set.

And you can use this command to make it editable once again:

sudo chattr -i /etc/hosts

Granted, you can still remove this immutable bit easily, but you have to do it as root, and also you need to remember another command to do it - using anything else like mv, cp, vi will not be able to edit or change it.

share|improve this answer

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