Friday, September 18, 2015

unix - Is it possible to remove the root directory?


I noticed in the documentation for rm as obtained by rm --help the following flag:


--no-preserve-root  do not treat `/' specially

What does this mean? Is it actually possible to delete the root directory, apart from its contents? What consequences would that have?


Answer



You cannot delete the root directory itself. However, you can use rm's recursive mode to delete everything in that directory – the infamous rm -rf / command.


The "preserve root" mode stops rm from recursively operating on the root directory:


$ sudo rm -rf /
rm: it is dangerous to operate recursively on ‘/’
rm: use --no-preserve-root to override this failsafe

The --preserve-root option was added to GNU rm in 2003 (commit 9be74f6f125b2be), and was made the default behavior in 2006 (commit aff5a4f2ab86f).


Some say it is because pranksters in #ubuntu kept telling newbies to run rm -rf / – and many did. Some say it is because it is too easy to mistype rm -rf / tmp/junk. Some say it is to prevent accidents when running rm -rf $dir/ when $dir is empty. All we know is, he's called th


Either way, it is part of POSIX requirements nowadays. Solaris rm also has similar protection, as does OpenBSD.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...