mount or unmount volumes as a non-root user

I recently needed to be able to mount volumes on a Linux server from a script and I ran into the "mount: only root can do that" error, even with an intermediate SUID script (because I believe mount checks the real user id, not just the effective one). The device and mount point are not consistent so adding an entry to /etc/fstab wasn't an option, nor was passing a password to sudo.

Enter the /etc/sudoers file.  By default sudo requires that a user provide their password, but you can use the NOPASSWD option to bypass this requirement.  This was perfect.

To allow the user corey to run /bin/mount and /bin/umount on all machines without a password add the following line to /etc/sudoers:

corey  ALL=NOPASSWD: /bin/mount, /bin/umount

To allow all members of the group 'wheel' to run /bin/mount and /bin/umount on all machines without a password add the following line to /etc/sudoers:

%wheel  ALL=NOPASSWD: /bin/mount, /bin/umount

Now one of the privileged users can run:

sudo mount /dev/sdb1 /some/path/to/mountpoint

And mount without issue – or a password prompt.

 

Tags: ,

Leave a Reply


© 2007-2012, Corey Gilmore | Posts RSS Feed | Comments RSS Feed | Contact

 

The views expressed on these pages are mine alone and not those of any past or present employer. All information presented on this site was obtained lawfully and not through disclosure under the terms of an NDA.