Systemd: How to automount a USB filesystem using /etc/fstab

autofsautomountingsystemd

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

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

Best Answer

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)

Related Question