DISCLAIMER: This works on Debian testing, Debian unstable, Ubuntu 8.04 and Ubuntu 8.10. I have not verified it to work on other systems. If you hose your box, because you gave it a try, and it didn’t work, don’t blame me. You’re the stupid one for trying it out on a production machine. If you’re curious, but unsure, just take my word for it, or install a virtual machine.
I came across an interesting post today, so I thought I’d give it a go on a virtual machine that I didn’t mind thrashing. The subject of the post is in the title, namely as root, running ‘rm -rf /’. Have you tried this on the Ubuntu or Debian? It won’t work:
root@host ~# rm -rf / rm: cannot remove root directory `/' root@host ~# echo #? 1
If you’re nervous about running the above command, then pass the interactive switch to rm, (rm -ri /) to force rm to ask you on every last item to remove (you can answer no, or cancel with Ctrl-c). Why is rm refusing to remove the root directory? From the man page:
do not treat ‘/’ specially
do not remove ‘/’ (default)
Running ‘rm -rf /’ is the same as running ‘rm -rf –preserve-root /’, which of course makes no sense. Has this always been the case for rm? No. First off, Solaris made this a standard in Solaris 10. Second, –preserve-root as default has been the default of Ubuntu since 8.04, as it came upstream from Debian, and I’m guessing further upstream from GNU coreutils (probably v6.10, although I can’t verify).
Preserving root as default prevents easy mistakes, such as missing the assignment of variables:
root@host ~#: FOO="/home/aaron/tmp" rm -rf $FOO/ rm: cannot remove root directory `/'
Notice, I forgot to end my variable statement with a semicolon, so FOO never got assigned, and rm proceeds forth with removing / instead of /home/aaron/tmp like it should have. How about another example:
root@host ~#: rm -rf / tmp/* rm: cannot remove root directory `/'
In this case, I wish to delete all the contents of the /tmp directory, but I typed too fast, and put a space between / and tmp/*, and thus, rm attempts to remove the root directory which is not what I wanted at all!
Good to see this implemented in the latest versions of Debian and Ubuntu.