Mounts (.mount)

Auto-mount an NFS or CIFS/SMB share with a .mount unit

4 min read

A .mount unit is systemd’s native alternative to an /etc/fstab line. The one rule everyone trips on: the unit’s filename must be the escaped mount path.

The naming rule

A unit mounting at /mnt/data must be named mnt-data.mount — slashes become dashes, the leading slash is dropped. Let systemd compute it for you:

systemd-escape -p --suffix=mount /mnt/data
# -> mnt-data.mount

NFS share

Save as /etc/systemd/system/mnt-data.mount:

[Unit]
Description=NFS share at /mnt/data
After=network-online.target
Wants=network-online.target

[Mount]
What=nfs-server.internal:/exports/data
Where=/mnt/data
Type=nfs
Options=rw,noatime,_netdev

[Install]
WantedBy=multi-user.target

CIFS / SMB share

[Unit]
Description=SMB share at /mnt/share
After=network-online.target
Wants=network-online.target

[Mount]
What=//fileserver/share
Where=/mnt/share
Type=cifs
Options=credentials=/etc/cifs-creds,uid=1000,gid=1000,_netdev,iocharset=utf8

Keep the password out of the unit — put it in /etc/cifs-creds (chmod 600):

username=svc_share
password=s3cret
domain=CORP

_netdev marks the mount as needing the network (so it isn’t tried too early on boot). Where= must already exist as a directory.

Reload, enable, mount

sudo mkdir -p /mnt/data
sudo systemctl daemon-reload
sudo systemctl enable --now mnt-data.mount
systemctl status mnt-data.mount
findmnt /mnt/data

Gotchas: the filename must match Where= (systemd-escape -p --suffix=mount <path>), or the unit silently won’t apply. Install nfs-common (Debian/Ubuntu) / nfs-utils (RHEL) for NFS, and cifs-utils for SMB. To mount only when something actually opens the path, pair this with an .automount unit (see the next recipe).

Open the full version (with copy buttons) ↗

← All recipes