Saturday 16 June 2018

linux - Is there a scenario where rm -rf -no-preserve-root is needed?


I've seen some questions on here where people accidentally do rm -rf --no-preserve-root or rm -rf *, wiping out most or all of their file system before they can react.


Is there ever a reason to use --no-preserve-root, whether in normal use, as a developer or as an administrator?



Answer



IMPORTANT: Modern UEFI systems mount the firmware under the /sys directory and make it available to the OS. DO NOT run this command on a modern system since it will remove this firmware, essentially bricking your machine.




The simplest scenario I can think of is someone wanting to delete all the data from their drive. There can be perfectly legitimate reasons to do this and the simplest way I can think of is


rm -rf --no-preserve-root /

Turns out this one is actually given as an example in info rm:


`--no-preserve-root'
Do not treat `/' specially when removing recursively. This option
is not recommended unless you really want to remove all the files
on your computer.

Another perfectly good reason is that you want to delete a mounted file system that you've chroot-ed into. In that case, rm -rf --no-preserve-root / will delete the system in the chroot environment but will leave yours intact.


I am sure there are more possible reasons, but in general it seems a very reasonable approach that my system allows me to do whatever I want with it. It's my job to be careful, the system should only enable me to do what it is that I want done. If what I want is stupid, that's my problem and not the OS's.


Anyway, this is a relatively new restriction, it was added in the 7th version of the POSIX specification (the previous one is here), before that rm -rf / was a perfectly valid command. On a historical note, the . and .. directories have always been protected from rm, ever since 1979, when rm first acquired the ability to delete directories. More on that here.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...