Is it possible to prevent accidental mounting and/or reformatting of a disk or partition by the root user during normal operation of a system?

For example, this happened to me a while ago:

  • Under normal operation I have /dev/sda1 mounted as / and /dev/sdb1 mounted as /var/disk2.
  • On one particular day I rewrote a thumbdrive (/dev/sdc) with different LiveCD images three or four times while trying to repair another computer.
  • One of those times I unmounted /dev/sdb1 instead of /dev/sdc1 and overwrote sdb1 with a new filesystem, by mistake.

Normally you have to be root to do anything with low level filesystem access on any storage device.

What I'd like is to be able to lock myself out of /dev/sda and /dev/sdb any time I'm running the live operating system from /dev/sda1 (as opposed to having booted from a LiveCD which I would never want to place any limits upon.)

Update in response to loevborg (thanks): I think what I really need is complete unrestricted access to /dev/sdc (or any other device that might show up when I plug in a USB block device) as a non-root user. Is this possible -- erasing, creating a filesystem, mounting -- as non-root if I set permissions somewhere or add myself to the correct group?


Instead of doing everything as root, you can make use of file permissions. The /dev/sd* files can be seen as regular files which can be assigned file permissions.

  • You're logged in as ubuntu, which is a member of the group ubuntu
  • You need to be able to write to /dev/sdc1
  • You need to be able to read from /dev/sdb1, but not write to it
  • By default, the permissions on the /dev/sd* files are set to 660 (rw-rw----), owned by root with group root

Solution: give the group ubuntu write permissions to /dev/sdc1 and read to /dev/sdb1:

sudo chmod 640 /dev/sdb1
sudo chgrp ubuntu /dev/sdc1 /dev/sdb1

From now on, you can safely overwrite your disks with an image as non-root user (ubuntu), with the system blocking you from accidentally writing to the wrong disk. Don't forget to change the permissions / group again when the disk is reinserted in the system.

I found this behavior useful when recovering data from a disk (read-only operation) without having to worry that a command writes to it.

Additional information: /dev/sdc is a disk. /dev/sdc1, /dev/sdc2, ... are partitions on that disk. Permissions are not inherited, so even if you can read from /dev/sdc directly, it does not imply that you can read from /dev/sdc1. If your disks are labeled, you can avoid mistakes by verifying that /dev/sdc1 is really your USB disk by showing a list of label-devices:

ls -lA /dev/disk/by-label

That is not possible. By definition, "root" is the user that is allowed to do everything. Think about it. If there was something root wasn't allowed to do, it wouldn't be at the root of the hierarchy of privileges.

You could probably introduce manual checks, but it would probably be no safer than just making sure you don't do anything stupid when using "sudo".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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