2

I mount a USB HDD used for intermittent plugin backup via the fstab entry:

# <file system>                           <mount point> <type>  <options>                                                                        <dump> <pass>
UUID=cd9f3fc4-f67f-42c4-8190-21d2766d2b65 /mnt/Bu-ehd2  ext4    rw,nosuid,noexec,nodev,noauto,nofail,relatime,user_xattr,acl,comment=x-gvfs-show 0      2

To unmount, sudo umount /mnt/Bu-ehd2 works, but trying to unmount as a regular (non-root) user:

$ umount /mnt/Bu-ehd2
umount: only root can unmount UUID=cd9f3fc4-f67f-42c4-8190-21d2766d2b65 from /mnt/Bu-ehd2

Why it is so is covered by @MariusGedminas' answer on AU, but not how to circumvent the usage restriction on umountwithout sudo.

  • Adding user to the mount options does not help.

  • Adding users does help but unmounting by any user becomes possible even after mounting was done based on an /etc/fstab entry. A possible but poor solution.

  • Adding uid=1000,owner breaks the mount process altogether with:

Error mounting system-managed device /dev/sdc1:

Command-line `mount "/mnt/Bu-ehd2"' exited with non-zero exit status 32: mount: wrong fs type, bad option, bad superblock on /dev/sdc1,...

I checked:

$ df -l | grep Bu-ehd2
/dev/sdc1        192162396     60744 182317284       1% /mnt/Bu-ehd2

$ ls -lAsF /dev/disk/by-uuid | grep sdc1
0 lrwxrwxrwx 1 root root 10 Mar 29 11:24 cd9f3fc4-f67f-42c4-8190-21d2766d2b65
-> ../../sdc1

$ stat /mnt/Bu-ehd2 | head -4
File: ‘/mnt/Bu-ehd2’
Size: 4096          Blocks: 8          IO Block: 4096   directory
Device: 821h/2081d  Inode: 2           Links: 3
Access: (0770/drwxrwx---)  Uid: ( 1000/someuser)   Gid: (    0/    root)

$ stat /dev/sdc1 | head -4
File: ‘/dev/sdc1’
Size: 0             Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d   Inode: 176539      Links: 1     Device type: 8,21
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)

$ blkid | grep Bu-ehd2 # yields nothing on /dev/sdc1 when actually 
$                      #+ mounted on `/mnt/Bu-ehd2`

Q: Is unmouting as a regular user impossible due to the fact that the mounted device is owned by root ? If so, how do I make the device umountable by someuser just by issuing cmd umount /dev/sdc1 ? Ideally that would be by making someuser the owner of its own external usb HDD device.

Note: I prefer not to resort to sudo visudo in order to write a sudo-exception rule for every different user, for umount. It would still force every someuser to type sudo umount /mnt/Bu-ehd2 instead of just umount /dev/sdc1 anyway.

Cbhihe
  • 2,761
  • 3
  • 24
  • 47

3 Answers3

3

Unless root, a user can not umount any filesystem mounted by some other user.

If you have used the user option in mount (/etc/fstab), you can check the output of mount command, you will see the user= showing the user who has mounted the filesystem. Only that user (and root) can use umount to unmount the filesystem.

There are hacky ways to do unmounting as a different user but i don't think there is any direct umount way.


Here is a function (based on OP's idea) that will run umount or gvfs-mount based on EUID. This could be put in /etc/bash.bashrc so that it will be available on all user's interactive shells:

unmount () {
    if [[ "$#" -eq 1 ]]; then
        { [[ "$(id -u)" -eq 0 || "$1" =~ ^-.* ]] && "$(command -v umount)" "$1" || gvfs-mount -u "$1" ;}
    else
        printf "Usage: unmount [option -h [|-V]|mount-point|device-path]\n"
    fi
}
Cbhihe
  • 2,761
  • 3
  • 24
  • 47
heemayl
  • 91,753
  • ok. I get it. @MarcelStimberg provides an old but valid workaround from cli in https://askubuntu.com/questions/405/why-do-i-need-root-privileges-to-umount-a-drive-at-the-command-line-but-not-in , in the form of gvfs-mount -u /media/the_device. But just introducing user in the mount options doesn't help (see OP) and introducing uid=1000 (for instance) breaks the fstab entry (see OP), at least for an ext4 FS... – Cbhihe Mar 29 '16 at 11:15
  • @Cbhihe If you use user, then the user mounted it can unmount it..not any random user..also i have said there is no direct umount way.. – heemayl Mar 29 '16 at 11:21
  • ...a slight misunderstanding perhaps. I am indeed talking about a user unmounting the USB ext4 fs it previously mounted based on the fstab entry. Or is it that when there is an fstab entry, the usb FS is actually mounted by root from within the non-root user session ? In any case placing user in the options and letting fstab do its job does not allow me to unmount as non-root. I have the feeling that mounting according to an fstab entry is tantamount to root mounting the FS. True or False ? – Cbhihe Mar 29 '16 at 12:40
  • 1
    True..mount does not depend on session, it depends on user..if root has mounted something , only root can umount it..also if a non-root user has mounted something, no other non-root user except it can umount it (root can obviously)....also i am talking about mount - umount here, not any other tools as tools like udisks manipulate the device directly and offer services via DBus.. – heemayl Mar 29 '16 at 12:46
  • +1. Got it despite being thick today. Finally I just went ahead and included users in the /etc/fstab entry mount options. It's a poor cop-out, a bit like leaving yr house door wide open so you can be sure to always be able to get in if you forget yr key... So can anyone else. Another better way would be to (just for someuser) "overload" (borrowed terminology!) the umount cmd with a small function placed in ~/.profile and in ~/.bash_profile (or in ~/.bash_login), which would catch the error message and redirect $1 to be handled by gvfs-mount -u $1. Just an idea. – Cbhihe Mar 29 '16 at 13:22
  • @Cbhihe Lol..good analogy..yes, that idea is very simple and straight forward.. – heemayl Mar 29 '16 at 13:23
  • Working on that "overloaded" umount () idea now. Will post shortly in a comment so that, if you like it enough, you can include it in yr answer before I mark it as accepted. – Cbhihe Mar 29 '16 at 13:41
  • Here (https://paste.ubuntu.com/15593311/) is my little overloaded umount() function to be placed in .bashrc or in ~/.profile and in ~/.bash_profile (or in ~/.bash_login). Feel free to modify it if necessary and to add it to yr answer, as a workaround to the limitation of /bin/umount. I will then mark yr answer as complete. – Cbhihe Apr 02 '16 at 21:42
  • @Cbhihe modified..check my edits.. – heemayl Apr 03 '16 at 02:07
  • ok, although you don't test for a lone option syntax such as: umount -h or umount -V, which were correctly handled in my pastebin suggestion. Also I believe you can actually call yr function umount instead of unmount. I think that in that case, using the syntax command umount ... inside the function block prevents a spurious recursive call from happening. It behaves well in my test on two 14.04.4 boxes. – Cbhihe Apr 03 '16 at 06:46
  • @Cbhihe duh..i forgot about those, add the checking at your will please..yes, command will explicitly search for external binaries and you can call it umount but i prefer to remain cleaner :) – heemayl Apr 03 '16 at 06:48
  • Done and tested. I got rid of "command -v" in "$(command -v gvfs-mount)" "$1" because upon adding the missing option -u to implement gvfs-mount unmout capability, the construct "$(command -v gvfs-mount -u)" "$1" broke. – Cbhihe Apr 03 '16 at 09:16
1

You can use udisksctl to unmount without root privileges:

udisksctl unmount --block-device /dev/sdc1
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • yes Serg, thanks. But it is already well covered by gvfs-mount -u /dev/sdc1 in comments to heemayl's answer above. I see the two approaches as equivalent even as udisksctl is "finer grained" than the simpler gvfs-mount ... – Cbhihe Apr 03 '16 at 06:49
0

Use udisksctl mount/unmount -b /dev/sdX. To use mount or umount without root privileges you need to edit /etc/sudoers file with visudo and add the lines:

username ALL=(ALL) NOPASSWD: /bin/mount, /bin/umount

Then reboot.