You could configure sudo
to let users execute some specially crafted script to mount usb drives on a fixed mount point indicating only the device. As an example, they could run:
sudo mount-usb /dev/sdb
where the script will try to mount /dev/usb and mount it, for example, in /mnt/usb, and inform of that to the user. Similarly, we can have an umount-usb
script to unmount the drive with:
sudo umount-usb
The mount-usb script could be like this:
#!/bin/bash
# mount-usb: mount drives in /mnt/usb
[ -z "$1" ] && echo "Use: $0 device" && exit
if ! [[ $1 =~ ^/dev/.*$ ]] ; then
echo "Use /dev/xxx as device"
exit
fi
mount -t auto -o uid=$SUDO_UID,gid=$SUDO_UID,ro "$1" /mnt/usb
mount | grep /mnt/usb #-- show result
We use SUDO* vars to get the real id/gid of the calling user and have a simple checking of the device specification with a regex. The corresponding umount-usb
will be then:
#!/bin/bash
# umount-usb: unmounts the device in /mnt/usb
umount /mnt/usb
The configuration of sudo
is done editing the /etc/sudoers
file with the visudo
command (or VISUAL=nano visudo
to use another editor instead of vi), where we can let some users or group execute certain commands as root without asking for a password. Example for the user john and the group usb-mounters:
Defaults !lecture
Cmnd_Alias USBMNT = /root/mount-usb ^/dev/.*$, umount-usb ""
john ALL=NOPASSWD: USBMNT
%usb-mounters ALL=NOPASSWD: USBMNT
Note the regex to enable only /dev/xx arguments in mount-usb
and the null string to disable them in umount-usb
(As described in the sudoers manual). We can omit the test for /dev/xx in the script with this definition, but I prefer to keep it in any case.