Skip to content
Skip to the article
In Linux: 9 articles
Linux

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.

Updated
Applies to
  • Ubuntu 24.04
  • Ubuntu Core 24
  • systemd 255
Tags
  • systemd
  • linux
  • storage
  • ubuntu
Reading time
3 min

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/data
media-data.mount

Paths 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.target
  • Where= must match the unit's file name exactly, or systemd refuses to load it.

  • nofail lets 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

FilesystemType=Typical Options=
ext4, XFS, Btrfsext4, xfs, btrfsdefaults,noatime,nofail
NTFS (kernel driver, Linux 5.15+)ntfs3uid=1000,gid=1000,umask=022,nofail
NTFS (FUSE driver, ntfs-3g package)ntfs-3guid=1000,gid=1000,umask=022,nofail
exFATexfatuid=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.mount

Check 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.target

Enable 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.automount

Make 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/data

Sources

This article is in the public domain (CC0 1.0), code samples included. Use it however helps you.