Ubuntu-MATE: make USB Sticks automount available to every user (udev rules?)
I want every USB media (ext, ntfs, FAT…) inserted to be available with full read-writable to every user. Preferably under mnt
and as group common
.
( Ubuntu-MATE 21.04, though my issues actually date back even to Ubuntu-MATE 16.04...)
- I boot into my machine as user frank. When I mount an USB drive (FAT) it ends up under
/media/frank/4C8C-E4BC
– just fine, as it should. When I switch to another user (keeping the first session open, not logging off, i.e.dm-tool switch-to-user lisa
) I get
Unable to mount 16 GB Volume Device /dev/sda1 is already mounted at
/media/lisa/4C9C-xxxx
Making sense so far. But not what I want.
Sure, for individual drives I could change the mount location via gnome-disks
or manually add a line to etc/fstab
(which essentialy is what gnome-disks
is doing). However I do not want to do this for each and every USB stick and SD card I posess...
Is there a better way for an all-user automount?
[ I understand/guess there's something called autofs handles the automatic mounting of USB sticks? But as far as I can tell, that's no part of my Ubuntu-MATE install... ]
And then there seems to be “udev-Rules”, is that, what I want? Apparently it's even an “inevitable” part of Ubuntu, as it is part of systemd (?)? There are rules on my machine under /etc/udev/rules.d although seemingly rather specific ones about brave and skype and signal...)
There's promising stuff MODE
and GROUP
and UDISKS_FILESYSTEM_SHARED
but (provided this is the right approach) I can't set the ultimate puzzle pieces together. It should truly only apply to removable drives like USB sticks and memory cards, otherwise I get into deep trouble... this source mentions SUBSYSTEMS=="usb"
, that would be a good filter, if true...)
Is udev
rulez the way?
If yes, can somebody set the puzzle pieces together? I am almost suspecting all I look for is a one-liner file in /etc/udev/rules.d.
addendum:
Not my main problem, but if it went away for the same reasons, I would be happy: Inserting a Windows Boot USB Stick (slightly more commplex, comes with an UEFI Boot FAT and a Windows Install NTSF partition) under the second user, I instantly get:
(and no, intense googling didn't get me anywhere) …switching back to first user frank (stick still inserted) I am welcomed by these authorization requests:
addendum:
Not a fix, but if those two users are essentially you, and the other one is not truly active (just switched-away from instead of logged-out) this is the quickest way before a trouble-free USB mount:
who -u
sudo kill <that other user's pid>
Solution 1:
The following script may be called mounter
or some [other] unique name. A for
loop looks for the output of lsblk
with suitable options. It will detect partitions in drives connected via USB and mount them (if not already mounted) at dedicated subdirectories of /mnt.
If you want to use the shellscript in your system, you will probably tweak it to manage already mounted partitions (unmount them in order to get them mounted like you wish).
#!/bin/bash
if [ "$(whoami)" != "root" ]
then
echo "run with sudo or as root"
exit
fi
for i in $(lsblk -lo name,fstype,hotplug,type|grep '1 part$'|tr -s ' ' ' '|sed 's/ 1 part$//'|grep ' ..*$'|tr ' ' '_')
do
printf "$i\n"
dev=/dev/${i%_*}
fss=${i#*_}
# echo "$dev -- $fss"
mkdir -p /mnt/"$i"
if [ "$fss" == "ntfs" ] || [ "$fss" == "vfat" ] || [ "$fss" == "exfat" ]
then
mount -o rw,user,exec,umask=0000 "$dev" /mnt/"$i"
else
mount -o rw,user "$dev" /mnt/"$i"
fi
done
This shellscript can do what you want, at least with Microsoft file systems.
-
It needs
sudo
or running asroot
. You can allow using the shellscript ormkdir
andmount
without password viavisudo
. -
You may or may not want to modify the
umask
to keep 'others' from writing. -
It is a bad idea to tamper with the permissions in a Linux file system, e.g. ext4. So this shellscript uses different mount options in this case. If you want to make a Linux file system available to everybody, set the permissions with
chmod
manually. -
It should work to start a shellscript manually, but it is also possible via
cron
orudev
or maybe some other way to watch for plugging in external devices. -
Additional shellscripts (or command lines) to unmount and clean up mountpoints are neceasary and should be easier to create.