18

I want my USB filesystems to automount when I connect the device.

How do I setup automount with systemd via /etc/fstab?

Martin Schröder
  • 939
  • 1
  • 10
  • 35
Tom Hale
  • 28,728
  • 32
  • 139
  • 229

1 Answers1

27

Connect your device and find out the UUID of the filesystem by running either blkid or lsblk -f.

Add a line to /etc/fstab such as:

UUID=05C5-A73A  /mnt/32GBkey  vfat  noauto,nofail,x-systemd.automount,x-systemd.idle-timeout=2,x-systemd.device-timeout=2

Then execute:

systemctl daemon-reload && systemctl restart local-fs.target

Explanation:

  • noauto - don't mount with mount -a
  • nofail - boot will continue even if this mount point is not mounted successfully
  • x-systemd.automount tell systemd to automount this etnry
  • x-systemd.idle-timeout=2 - wait 2 seconds before unmounting the device after last usage
  • x-systemd.device-timeout=2 - wait only 2 seconds before giving No such device if the device is not connected

Note:

  1. There are no quotes around the UUID number.
  2. The mount point directory doesn't need to exist - it will be created

For more information about the options available, see systemd.mount(5)

Philippe Gachoud
  • 1,559
  • 17
  • 17
Tom Hale
  • 28,728
  • 32
  • 139
  • 229
  • 2
    Is there any advantage to this over a `udev` rule? This seems very specific for a single device (given the `UUID`-dependency of `fstab`), where a `udev` rule could cover e.g. any USB flash drive. – FelixJN Feb 23 '17 at 09:52
  • `/etc/fstab` doesn't have the dependency you assert, accepting `LABEL=...`, `PARTLABEL=...` and good old `/dev/usbkey`. You could always setup a `udev` rule to make `/dev/usbkey`, but I don't know how to mount using `udev` alone. – Tom Hale Feb 23 '17 at 10:28
  • 1
    Update 2019: I tested this solution successfully on Debian 9.9 and found that it works fine. Note that since this is an automount, `df` might not show the filesystem as mounted. – Jonathan Ben-Avraham Jul 03 '19 at 09:07
  • while this may work, why go through the trouble? if you do just as you have shown above in fstab but for options you specify (perhaps in addition to other useful ones) `nofail` (noting that you _do not_ use `noauto`) then the USB will be mounted at boot if it is plugged in. And, in fact, will be mounted any some point in the future if the USB device were to be plugged in at some later time. No need to involve systemd as directly as shown. – sherrellbc Jul 12 '22 at 17:47
  • With this config, how to prepare device for ejecting? – Dims Feb 07 '23 at 22:38