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,801
  • 3
  • 27
  • 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,801
  • 3
  • 27
  • 47
heemayl
  • 93,925
1

You can use udisksctl to unmount without root privileges:

udisksctl unmount --block-device /dev/sdc1
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.