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:

  1. modifying /etc/fstab to add per-drive mounts by UUID/label/device.
  2. udev rules (apparently 'raw rules' might conflict with existing systemd policies)
  3. udisks2 running as a systemd service or via udiskie
  4. udevil
  5. usbmount
  6. automounting provided by the desktop environments, i.e. on XFCE via thunar + thunar-volman packages, or the nautilus automount in Gnome with the gnome-volume-manager package (apparently they rely on udisks).
  7. autofs kernel automounter
  8. 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?


After fiddling with all the options I found usbmount to (almost) just work after I edited /lib/systemd/system/systemd-udevd.service and changed MountFlags=slave to MountFlags=shared as described in this issue. No need to manually add any UUIDs or labels to any config files. The downside is that it creates the mount points under /media/usbN which is not perfect, so I have switched to automount-usb which was surprisingly easy to set up (just ran the configure.sh script) and which creates mount folders like /media/<device>_<disk_label> e.g. like /media/sda2_mylabel.

Relevant links:

Auto-Samba mounting: There is also a fork of automount-usb which automatically adds the attached USB drives as samba read-write mount points.

  • What precisely do you want to automount? A limited set of known devices, or just whatever USB stick gets connected anytime in the future? There are different answers to both. – user1686 Nov 4 '17 at 13:00
  • @grawity: I am more interested in the second (i.e. whatever USB storage device), since for the first I can just add the entries to /etc/fstab. – ccpizza Nov 4 '17 at 13:02

Entries in /etc/fstab should still be honored on a systemd-based system.

A .mount unit can be used instead, and should be considered equivalent to an entry in fstab.

A .automount unit can be used if the mount is not required at startup or permanently; systemd will unmount it after an idle period given in the unit file has elapsed.

See the systemd.mount(5) and systemd.automount(5) man pages for details.

| improve this answer | |
  • 1
    I would like to avoid directly editing /etc/fstab because I don't want to add UUID's for every single USB device. Do you have an example of a .mount or .automount unit? Where would it have to be placed? Under /lib/systemd/system? – ccpizza Nov 4 '17 at 11:56
  • 1
    Yep, small typo there. pkg-config systemd --variable=systemdsystemconfdir – Ignacio Vazquez-Abrams Nov 4 '17 at 12:01
  • No, the sysadmin should never place custom units there. They can use /etc/systemd/system for that. – user1686 Nov 4 '17 at 13:03

it is not clear which is the current 'officially' supported approach.

Officially supported by whom? If e.g. GNOME includes automount functionality based on udisks, you can be sure it's officially supported by GNOME themselves.

I am more interested in the second (i.e. whatever USB storage device), since for the first I can just add the entries to /etc/fstab.

There's no "standard way" for doing that. At best, the majority of existing systems have settled on adding automation on top of udisks2. On its own, udisks doesn't automount anything at all – but it's the "backend" used by many graphical desktop environments (at least both GNOME and Xfce use it; I'm only 80% sure about KDE and Enlightenment).

(So your option 3 would be "udisks2 + automount by udiskie", and option 4 would be "udisks2 + automount by desktop environment".)

Regarding udev rules: whether it's about mounting filesystems or starting services, the short answer is "don't do it" (for various reasons); but the long answer is: "don't do it directly, but you can ask init to do it". So it wouldn't be horrible to run systemd-mount from an udev rule, which then just passes the mount request to init just like a .mount unit would...

However, expect this to trigger a systemd bug/wart/misfeature: as udev reports the device to be ready only after processing the rules, you could end up with init automatically unmounting the disk because it thinks the device isn't there yet.

Instead, udevil would work better, since it doesn't run anything via rules but only reacts to "device ready" events emitted by udev.

fstab entries are oriented towards static devices. However, they can be sort-of abused for miscellaneous USB sticks by using /dev/disk/by-path/..., which corresponds to the physical path of a device (e.g. PCI slot 3, USB port 1, partition 1...) This way you can write a fstab entry which matches any disk plugged in to the same USB port.

The autofs kernel automounter is, similarly to udisks, just the backend upon which various automounts can be implemented by userspace. Once an autofs mount is set up, all attempts to access it are reported to the corresponding daemon. The most common implementations are the traditional (maps-based) autofs, and recently systemd with the .automount units.

So the "dynamic" USB device logic would still have to be implemented in userspace, and either way it's more work than just using udisks.

  • With plain systemd, your only option is to build on top of the above fstab "by-path" hack. Once you write a fstab entry for the desired USB port, you can mark it with x-systemd.automount,x-systemd.idle-timeout=300 to use the autofs automounter. (Or, of course, create standalone .mount + .automount units for the same result.)

    If you want to dynamically generate automounts for all USB disks on all ports, systemd cannot do that without third-party scripts.

  • I don't know whether autofsd can do what you want, but I remember that it supports some kinds of dynamic maps (for user home directories). Perhaps using the program map-type (and a script which enumerates all connected disks) would work.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.