8

I think pretty much people here mistakenly 'rm -rf'ed the wrong directory, and hopefully it did not cause a huge damage.. Is there any way to prevent users from doing a similar unix horror story?? Someone mentioned (in the comments section of the previous link) that

... I am pretty sure now every unix course or company using unix sets rm -fr to disable accounts of people trying to run it or stop them from running it ...

Is there any implementation of that in any current Unix or Linux distro? And what is the common practice to prevent that error even from a sysadmin (with root access)?

It seems that there was some protection for the root directory (/) in Solaris (since 2005) and GNU (since 2006). Is there anyway to implement the same protection way to some other folders as well??

To give it more clarity, I was not asking about general advice about rm usage (and I've updated the title to indicate that more), I want something more like the root folder protection: in order to rm -rf / you have to pass a specific parameter: rm -rf --no-preserve-root /.. Is there similar implementations for customized set of directories? Or can I specify files in addition to / to be protected by the preserve-root option?

Braiam
  • 35,991
amyassin
  • 1,361
  • @mattdm sure.. but I was curious is there any implementation to prevent such mistakes, as the commenter mentioned... – amyassin Jan 20 '13 at 17:35
  • 6
    probably the only way would be to replace the rm command with one that doesn't have that feature. – Keith Jan 20 '13 at 17:40
  • 2
  • 2
    most distros do `alias rm='rm -i' which makes rm ask you if you are sure. besides that: know what you are doing. only become root if necessary. for any user with root privileges security of any kind must be implemented in and by the user. hire somebody if you can't do it yourself.over time any countermeasure becomes equivalaent to the alias line above if you cant wrap your own head around the problem. – Bananguin Jan 20 '13 at 21:07
  • Let the user do it once and clean up afterwards. Worked like a charm in my case (rm -rf * as root standing at /, no less; caught it after eating most of /bin, had to get the machine to a halfways usable state with the few commands left to be able to shutdown and rebuild. Ah, the memories...). – vonbrand Jan 21 '13 at 17:13
  • @vonbrand looks like a good story, similar to that linked above ;) did you tell it any where that I can read?? – amyassin Jan 22 '13 at 00:09
  • @user1129682, I don't know if it's true, but I think the distros doing that by default stopped doing it. Having rm aliasing to rm -i is worse because it gets in your way all the time, and one way to override it is with rm -f, so in the end you end forcing users to do more rm -f. Perhaps rm -I (GNU)? – njsg Jan 22 '13 at 11:49
  • In the end, this question is pretty much like "How do I prevent accidentally shooting something I don't want to shoot when I fire a gun"... Best way is really to avoid firing a gun in first place, although backups are nice too. – njsg Jan 22 '13 at 11:50
  • @njsg no, I was asking more on the direction of 'Is there anything already implemented in the direction of that?' as from the comment in the story linked in the question.. It turned out there is, but only for the root directory! – amyassin Jan 22 '13 at 11:55
  • @amyassin: deleting / is different, because it is a special case which you can treat specially. rm -rf is an explicit call to recursively delete something (meaning no special case) without further ado and an implementation that contradics an explicit request won't to anything but leave you with a not fully functional OS, or it will teach people another explicit request that can't be contradicted in which case you can start the same discussion over. It's like becoming root when rm -rf /usr doesn't work. – Bananguin Jan 22 '13 at 12:59
  • 1
    @amyassin using rm -rf can be a resume generating event. Check and triple check before executing it – midnightsteel Jan 22 '13 at 14:21
  • @midnightsteel Resume generating event is a good expression.. It is new to me actually.. thank you :) – amyassin Jan 22 '13 at 14:27
  • You can change a file or directory to be immutable (chattr +i), and you can't rm -rf it even as root, but you also can't write to it or change it in anyway until you remove the immutable flag. I've used this in the past for various reasons. – Drake Clarris Jan 22 '13 at 20:38
  • @DrakeClarris I think this could serve as an answer well :) – amyassin Jan 22 '13 at 20:42
  • 1
    added to my answer then – Drake Clarris Jan 22 '13 at 20:59
  • Have you looked at chattr? You can make it immutable... Also, there are file access control lists (getfacl setfacl) – JZeolla Jan 24 '13 at 19:25