Mount a Drive with a systemd Mount Unit
Write systemd .mount and .automount units to mount a disk by UUID at boot or on first access, as an alternative to fstab.
On this page
systemd can mount filesystems from unit files instead of /etc/fstab. This is the only option on systems where /etc/fstab is read-only or managed for you, such as Ubuntu Core, and it's handy anywhere you want a mount with dependencies, ordering, or on-demand mounting. Run the commands as root.
1. Find the Partition's UUID
blkid/dev/sdb1: LABEL="data" UUID="<PARTITION_UUID>" TYPE="ext4" PARTUUID="..."Mount by UUID (/dev/disk/by-uuid/...) rather than /dev/sdX, which can change between boots.
2. Name the Unit After the Mount Point
A mount unit's file name must be the escaped mount path. systemd-escape produces it:
systemd-escape -p --suffix=mount /media/datamedia-data.mountPaths with dashes or spaces escape differently (for example /mnt/my-disk becomes mnt-my\x2ddisk.mount), so always generate the name rather than guessing.
3. Write the Mount Unit
Create /etc/systemd/system/media-data.mount:
[Unit]
Description=Data drive
[Mount]
What=/dev/disk/by-uuid/<PARTITION_UUID>
Where=/media/data
Type=ext4
Options=defaults,noatime,nofail
[Install]
WantedBy=multi-user.targetWhere=must match the unit's file name exactly, or systemd refuses to load it.nofaillets the system finish booting if the drive is missing, which matters for removable or external disks.systemd creates the mount point directory if it doesn't exist.
Filesystem-Specific Options
| Filesystem | Type= | Typical Options= |
|---|---|---|
| ext4, XFS, Btrfs | ext4, xfs, btrfs | defaults,noatime,nofail |
| NTFS (kernel driver, Linux 5.15+) | ntfs3 | uid=1000,gid=1000,umask=022,nofail |
NTFS (FUSE driver, ntfs-3g package) | ntfs-3g | uid=1000,gid=1000,umask=022,nofail |
| exFAT | exfat | uid=1000,gid=1000,umask=022,nofail |
NTFS and exFAT have no Unix permissions, so uid, gid, and umask decide who owns the files. Replace 1000 with the user and group IDs from id <USER>. Avoid umask=0000, which makes every file world-writable.
4. Start and Enable the Mount
systemctl daemon-reload
systemctl start media-data.mount
systemctl enable media-data.mountCheck it with systemctl status media-data.mount and findmnt /media/data.
Mount on First Access Instead
To mount the drive only when something first touches the mount point (and optionally unmount it when idle), add an automount unit with the same base name, /etc/systemd/system/media-data.automount:
[Unit]
Description=Automount data drive
[Automount]
Where=/media/data
TimeoutIdleSec=10min
[Install]
WantedBy=multi-user.targetEnable the automount unit instead of the mount unit. Remove the [Install] section from the .mount file, or disable it, so only the automount triggers it:
systemctl daemon-reload
systemctl disable media-data.mount
systemctl enable --now media-data.automountMake Services Wait for the Mount
If a service stores data on the drive, make it depend on the mount so it never starts against an empty mount point. In the service's drop-in (systemctl edit <SERVICE>):
[Unit]
RequiresMountsFor=/media/dataSources
This article is in the public domain (CC0 1.0), code samples included. Use it however helps you.