4

This question already has an answer here:

When I installed Linux for the first time in my computer, I always liked to use root, because I didn't need to add sudo and type the password every time I executed a command that needed root permissions.

So one day, I just wanted to remove a directory, and I ran rm -rf /, so my system just broke down. I wonder why the Linux designers didn't forbid such a dangerous command.

marked as duplicate by Ƭᴇcʜιᴇ007, Steven Penny, Sathyajith Bhat Apr 16 '16 at 19:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    It isn’t forbidden on Windows either. – Daniel B Apr 16 '16 at 15:44
  • 3
    I believe a modern Linux distribution won't allow this without --no-preserve-root – Vinayak Apr 16 '16 at 15:49
  • 2
    @DanielB I think this has nothing with Windows – fangxing Apr 16 '16 at 16:14
  • 2
  • 1
    I don't think this question is a duplicate of the linked one. This asks why Linux allows rm -rf /, while the other is asking how to stop people from shooting themselves in the foot with it. – Ben N Apr 16 '16 at 21:18
17

Why shouldn't it allow you to do whatever you want to your own computer? Logging in as root or using sudo is saying to the machine, "I know what I'm doing." Preventing people from doing dubious things usually also prevents them from doing clever things, as expressed by Raymond Chen.

Besides, there's one singularly good reason to allow a user to torch the root directory: decommissioning a computer by completely erasing the OS and file system. (Danger! On some UEFI systems, rm -rf / can brick the physical machine too.) It's also a reasonable thing to do inside a chroot jail.

Apparently, people accidentally ran the command so much that a safety feature was added: rm -rf / does nothing on most systems unless --no-preserve-root is also supplied, and there's no way you can type that by mistake. That also helps guard against poorly-written but well-intentioned shell scripts.

8

TL;DR: The simplest answer would be: why not? It's just a feature more, a tool that the operative system provides. You use it at your own risk, knowing what you're doing.


Explanation

Using root as your user is a very bad idea, since everything you execute is run with elevated privileges, a huge security flaw.

For example, if you downloaded a malicious program, or if the software installed in your computer had some critical bug, that could lead to devastating damage to your files and even your hardware.

Even if you're a bit too lazy to be writing sudo many times, there are some commands like sudo -i, that you can use for a while and then return to your normal privileges status. But I wouldn't recommend this unless you have more experience using the terminal.

However, this isn't attributable to Linux neither its developers. Actually, as @DanielB stated, Windows allows this, too. You have the liberty and the tools to do it, but that doesn't mean you have to.

There are packages that prevent you from rming your root directory, such as Safe-rm. That may be a good choice if you think there's a risk it happen again.
Other solutions and worarounds, like rm's parameter --preserve-root are discussed in this Server Fault question.

  • Thanks your nice answser , I say 'why Linux ... ', is just because I use Linux mostly. Sorry for my english is poor :) , – fangxing Apr 16 '16 at 16:19
  • No problem! It was just for clarifying that it's something that happens on other OS, a liberty that's there... just in case. ;) – Hewbot Apr 16 '16 at 16:21
  • downvoted, as it doesn't explain the asked question. – toogley Apr 16 '16 at 17:46
  • 1
    @toogley It's a bit difficult to answer to a question which hasn't got a specific answer like this one; still I've updated the question to be slightly more precise. Thanks for the feedback! :) – Hewbot Apr 16 '16 at 18:23

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