mount unmount without sudo

13,698

Solution 1

Desktop linux distributions use udisks to grant non-root users limited mounting priviliges.

udisks version 2

udisksctl mount -b [device]

udisks version 1

udisks --mount [device]

Of course, if we are talking about a server VM, then these tools might not be installed. Installing them would require root access (once)

Solution 2

You must add /nfs entry to /etc/fstab on the server host.

In the list of options of the entry must be option user or users (depends on that if you want that user could unmount the filesystem or not).

Example:

xx.xx.xx.xx:/ /nfs nfs rsize=4096,wsize=4096,user 0 0
Share:
13,698
Shehbaz Jaffer
Author by

Shehbaz Jaffer

Updated on June 15, 2022

Comments

  • Shehbaz Jaffer
    Shehbaz Jaffer almost 2 years

    I am trying to write a script that would ssh into a host, perform mount operation there, run some other commands and exit.

    other commands (cd, cp) do not require sudo privelages but mount option requries sudo permission. I want to write a script that would do:

    ssh user@server "mount -t nfs xx.xx.xx.xx:/ /nfs -o rsize=4096,wsize=4096 ; cp pqr rst ; umount /nfs ;" 
    

    and some other non-sudo commands. How can I do this without a sudo option and without entering any passwords when the script is running.

  • ElementalStorm
    ElementalStorm about 10 years
    This allows user to use sudo without a password. He is asking how to avoid sudo at all.
  • matt2000
    matt2000 over 9 years
    But modifying /etc/fstab requires root, so this still doesnt work in the case where the mount directory or device id is not know in advance.
  • Igor Chubin
    Igor Chubin over 9 years
    @matt2000: No, the administrator can modify /etc/fstab before. Of course if you have no root rights, it is ABSOLUTELY IMPOSSIBLE to mount a filesystem (if it is not explicit allowed in /etc/fstab), else it would a BIG security breach
  • matt2000
    matt2000 over 9 years
    What can the administrator add to /etc/fstab to allow mounting an unspecified device to an unspecified location? Does fstab allow wildcards? E.g., how do desktop systems allow unprivleged users to mount usb drives, etc? They don't use fstab, as far as I know. This is not a security breach at all when the only users of the system are trusted to mount devices, but not to do other things.
  • white_gecko
    white_gecko about 4 years
    If you are using udisksctl the user might not have the necessary permissions and you need to configure polkit: github.com/coldfix/udiskie/wiki/Permissions (cf. wiki.archlinux.org/index.php/Udisks#Configuration)