Approaches to auto-mounting devices in Linux keep changing, and googling returns quite a few solutions with various degrees of applicability for modern systemd-based boxes.
The following approaches seem to exist:
- manually modifying
/etc/fstab
to add per-drive mounts by UUID/label/device. udev
rules (apparently 'raw rules' might conflict with existing systemd policies)- udisks2 running as a systemd service or via udiskie
- udevil
- usbmount
- automounting provided by the desktop environments, i.e. on XFCE via
thunar
+thunar-volman
packages, or thenautilus
automount in Gnome with thegnome-volume-manager
package (apparently they rely on udisks). - autofs kernel automounter
- systemd automounting, example of usage: automount-usb
The choices can be overwhelming and it is not clear which is the current recommended approach. Moreover, it seems that different auto-mounting subsystems can conflict which each other leading to situations when a partition is mounted by one tool and then in a matter of seconds is automatically unmounted by another tool.
For systems with a desktop environment, it is straightforward since most of them handle USB-mounting automatically, so no extra action is necessary apart from enabling the automounting option in settings.
What would be the current approach for a headless system which mostly operates in text-mode?
Update
After fiddling with all the options usbmount
(apt install usbmount
) turned out to be the easiest and to just work after editing /lib/systemd/system/systemd-udevd.service
changing MountFlags=slave
to MountFlags=shared
as described in this issue (❗️UPDATE: For recent systemd versions locate and change PrivateMounts=yes
to PrivateMounts=no
moreover, since the file is under /libs/...
keep in mind that this change will be ❗️overwritten whenever the udev
package gets updated). No need to manually add any UUIDs or labels to any config files. The only downside is that it creates the mount points under /media/usb<0..N>
rather than /media/<DISK_LABEL>
.
There is also automount-usb
which can be installed by cloning the repo and running configure.sh
. It creates mount folders like /media/<device>_<disk_label>
e.g. like /media/sda2_mylabel
(❗️UPDATE: automount-usb
appears to be unmaintained, yet it still keeps working with recent linux versions).
Relevant links:
- https://github.com/rbrito/usbmount
- https://github.com/six-k/automount-usb
- Automatically mount external drives to /media/LABEL on boot without a user logged in?
- https://askubuntu.com/a/457904/74172
- https://unix.stackexchange.com/questions/119973/mounting-usb-automatically-having-usbs-label-as-mountpoint
- https://www.freedesktop.org/software/systemd/man/systemd.automount.html
- https://www.freedesktop.org/software/systemd/man/systemd.mount.html
- http://blog.tomecek.net/post/automount-with-systemd/
- https://serverfault.com/questions/766506/automount-usb-drives-with-systemd
Auto-Samba mounting: There is also a fork of automount-usb which automatically adds the attached USB drives as samba read-write mount points.
/etc/fstab
.