[root tip] [How To] systemd mount unit samples

Difficulty: ★★☆☆☆

Example mount units for systemd

The examples here are a supplement to the guide on systemd mount units

:warning: Remember to remove the comments placed in the units - they are not writing for copy-paste but as examples. (comments is text beginning with a #)

Only use an automount unit if the device is not readily available at boot like removable devices and network locations.

Important rule

:warning: :information_source:

  1. :information_source: For Internal devices enable and start the mount unit .
  2. :information_source: For a removable device - never enable the mount unit only the automount unit.

:information_source:

  • The mount unit will create the mountpoint if it does not exist.
  • The automount unit will fail if the mount unit is active
  • The automount unit will start the mount unit when the mountpoint is accessed whether this is terminal or application access.

Unit file names

Local system unit files are stored in /etc/systemd/system.

Mount unit

Mount units must be named the mount point with the extension of .mount so if you use a mountpoint named /data/backup the unit file must be named data-backup.mount

Automount unit

Automount units use the name of the mount unit with an extension of .automount so the automount unit for the example data-backup.mount must be named data-backup.automount.

Automount units starts the mount unit on demand e.g. when the mount path is accessed. The automount unit will fail if the mount unit is active, so always - in the context of automount - ensure the mount unit is disabled.

:warning:

You can create the files by using the output like this

touch $(systemd-escape -p --suffix=mount "/data/home-backup")
touch $(systemd-escape -p --suffix=automount "/data/home-backup")

Content of a mount unit

Depending on the Type, the What is different and so is the Options

What do you want to mount (disk, server, share)?
Where do you want it to mount (a path on your system)?
Which Type is your mount (filesystem, cifs, nfs)?
What Options should the mount use(rw, auto, netdev)?

Example

If you want to mount a Samba share named video provided by a local NAS named server

  • What is the name of server and the share e.g. //server/video
  • Where is the path where you can browse the data provided by the share e.g. /data/smb/video
  • Type will be cifs because that is what this type of connection is called
  • Options will instruct the system it is a net device _netdev, it should be possible to read and write rw, and connect using a specific workgroup, username and password.

Sample mount units

If the partition is on an internal disk - there is no need for automount - simply enable the mount unit.

Disk PARTITION

[Type] and [Options] are optional for disk devices.
To reduce unnecessary writings to SSD devices the noatime option is recommended.

mount unit

Name the file according to $YOUR_MOUNT_PATH.mount

[Unit]
Description=Mount build partition

[Mount]
What=/dev/disk/by-uuid/$UUID
Where=$YOUR_MOUNT_PATH
Type=ext4
Options=rw,noatime

[Install]
WantedBy=multi-user.target

USB device partition

[Type] and [Options] are optional for disk devices.
To reduce unnecessary writings to SSD devices the noatime option is recommended.

mount unit

Name the file according to $YOUR_MOUNT_PATH.mount

[Unit]
Description=Mount USB backup device partition

[Mount]
What=/dev/disk/by-uuid/$UUID
Where=$YOUR_MOUNT_PATH
Type=ext4
Options=rw,noatime

[Install]
WantedBy=multi-user.target

USB partition automount unit

Name the file according to $YOUR_MOUNT_PATH.automount

[Unit]
Description=Automount USB backup partition
ConditionPathExists=$YOUR_MOUNT_PATH

[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

SMB network share

The package smbclient is enough. The credentials can be stored in location readable only by root. Replace the $VARIABLES with the values for your system

NOTE: According to the archlinux wiki the uid and gid can cause I/O errors.

Warning: Using uid and/or gid as mount options may cause I/O errors, it is recommended to set/check correct File permissions and attributes instead. - Samba - ArchWiki

More information on Samba can be found on the archlinux wiki

SMB credentials

Create a file /etc/smb.cred or in user’s home with content

user=$SMBUSER
password=$SMBPASS
workgroup=$WORKGROUP

Make the file readonly to user

sudo chmod 600 /etc/smb.cred

SMB version

If needed you add a version to the options string e.g. vers=NT1

mount unit

Name the file according to $YOUR_MOUNT_PATH.mount

[Unit]
Description=NAS SMB video share

[Mount]
What=//$YOUR_SERVER/$YOUR_SHARE
Where=$YOUR_MOUNT_PATH
Type=cifs
Options=_netdev,iocharset=utf8,rw,file_mode=0666,dir_mode=0777,credentials=/etc/smb.cred,vers=$SMBVERSION
TimeoutSec=30

[Install]
WantedBy=multi-user.target

automount unit

Name the file according to $YOUR_MOUNT_PATH.automount

[Unit]
Description=Automount video share using SMB
ConditionPathExists=$YOUR_MOUNT_PATH

[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

NFS network share

mount unit

Name the file according to $YOUR_MOUNT_PATH.mount

[Unit]
Description=Mount NAS Video share using NFS

[Mount]
What=$YOUR_SERVER:/$YOUR_SHARE
Where=$YOUR_MOUNT_PATH
Type=nfs
Options=_netdev,auto

[Install]
WantedBy=multi-user.target

automount unit

Name the file according to $YOUR_MOUNT_PATH.automount

[Unit]
Description=Automount video share usuing NFS
ConditionPathExists=$YOUR_MOUNT_PATH

[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

MOUNT unit for FTP server

Using curlftpfs. It is also possible to store the username and password in a safe location readable only by root.

You can put the user and password in a .netrc file in the home directory of the user that executes CurlFtpFS. It can have 600 permission. It’s still clear text but at least is not accessible by all.
The format is:

machine ftp.host.com
login myuser
password mypass

See curlftpfs on archlinux wiki. Replace the $VARIABLES with the actual values for your use case.

Name the file according to $YOUR_MOUNT_PATH.mount

[Unit]
Description=Mount FTP server (ftp.server.tld) using curlftpfs
Wants=network-online.service

[Mount]
What=curlftpfs#ftp.server.tld
Where=$YOUR_MOUNT_PATH
Type=fuse
Options=rw,nosuid,uid=$UID,gid=$GID,allow_other,user=$FTPUSER:$FTPPASS

[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target

automount unit

Name the file according to $YOUR_MOUNT_PATH.automount

[Unit]
Description=Automount ftp server ftp.server.tld
ConditionPathExists=$YOUR_MOUNT_PATH

[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=300

[Install]
WantedBy=multi-user.target

WEBDAV

See the Arch wiki on webdav. Install davfs2 from the AUR (Arch User Repository)

Edit the file /etc/davfs2/secrets and append your webdav service and credentials

http(s)://address:<port>/path    davusername    "davpassword"

Or create a user file at ~/.davfs2/secrets for user specific mounts

mount unit

Name the file according to $YOUR_MOUNT_PATH.mount

[Unit]
Description=Mount WebDAV Service on server https://host:port/path
Wants=network-online.service

[Mount]
What=http(s)://address:<port>/path
Where=$YOUR_MOUNT_PATH
Options=rw,_netdev
Type=davfs
TimeoutSec=15

[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target

automount unit

Name the file according to $YOUR_MOUNT_PATH.automount

[Unit]
Description=Mount WebDAV Service
ConditionPathExists=$YOUR_MOUNT_PATH

[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=300

[Install]
WantedBy=multi-user.target
14 Likes
How to automatically mount network shares?
Mount only if source is available
How to automount at boot
Mounting smb share using fstab, getting "home" permission denied error
Getting permissions right with systemd mount unit
Automounting NFS as user
Automount systemd unit needs a restart to work on every boot
Why can't I automount NFS directory using systemd units?
Setting up a SD card as /home(?)
Need help automounting NAS storage?
How to automatically mount network share upon connecting to my home wifi network?
Unable to mount freshly encrypted usb stick partition
Steam game doesn't work
How to process files from a different computer?
Most Apps I Open Do Not Display All Drives On My PC
Every Time I Boot Into Manjaro A Popup Is Shown To Enter Password For Encrypted Internal 4TB HDD?
Choice home at boot
Automounting fails with mount-start-limit-hit
Fusermount permission error
[root tip] [How To] Mount partition using fstab
Issues with SystemD Automount for NFS Shares
Issues with SystemD Automount for NFS Shares
[root tip] [How To] Basic Samba Setup and Troubleshooting
[root tip] [How To] Use systemd to mount ANY device
Instructions to permanently mount network drive
Dolphin SFTP issues on a certain server
Cant share a folder
Virtuelle Maschinenverwaltung - VMs lassen sich nicht starten oder importieren
Network manager will often not load on boot & causes problems with automountss
Updated: Qonos upgrade breaks Samba - reproduced on two systems
Fstab custom drive throws me into emergency mode!
How can I switch /home partition?
Mount.nfs: remote share not in 'host:dir' format - how to resolve?
Shutdown / Unmount
Scribus Open file dialogue doesn't show network / online account drives
Cannot move files between SMB shared folders with Dolphin
Unable to "control" the mounting of servers (or other removable media)
My drive won't show up
Apple timecapsule 2T - manjaro
Fstab cannot mount ntfs drives
Help with AutoFS for NFS mount
Multiple Issues when installing manjaro with KDE Plasma as Dual Boot
How to open and write Libreoffice Writer files to a NAS using Samba
Second Partition filling up Root Partition
How do I stop the amnesia occuring when reconnecting to my NAS?
Permission issues mounting SMB share
I need help to mount my data drive at boot and dir links from my system ssd to my data sata drive
How do I get my Plex media server running on startup?
System will not mount NAS at boot, although the mount does work fine after its finished booting
Programmers Partitioning Scheme
Need Help With Two Issues On Boot Of Manjaro KDE
Timeout when sshfs mount not reached
Timeout when sshfs mount not reached
[root tip] [How To] Basic Samba Setup and Troubleshooting
Remote accessing server drives
Auto Mounts No Longer Working After Updates
Best way for live mapping a NAS shared folder
Install manjaro plasma and use other hard disk to files
"Folder empty" on Samba-accessed NAS after a file operation
Differences in permanently(FSTAB) auto mounting NFS share (client) Gnome vs. XFCE
Best way for live mapping a NAS shared folder
Hard disk is not mounted proberly after booting
Manjaro doesn't mount USB storages anymore
System not mounting network drive on boot
System not mounting network drive on boot
Slow boot time/restart
Windows Shared Folders Keep Prompting for Credentials
Can't mount external drives after updating kernel
Autofs won't work on new install
10GB Network slow on NFS SMB TCP
Selecting a mounted NAS on Dolphin causing high network activity
Network Profiles - home with SMB mounts / public without
Cifs: "mount error(20): Not a directory" while mounting
Problem mounting Nas (NFS) with systemd
Fstab: mounting nfs with noauto,x-systemd.automount works in Dolphin, but not other applications
Make second internal SSD usable
Systemd per Script mit root rechten Prüfen lass ob Netzlaufwerke gemounted sind
Wie sorge ich dafür das Netzwerklinks gleich von Anfang an bereit stehen?
Samba Share - ls: Directory: Resource temporarily unavailable
Systemd NFS Automount Not Working
Curl and wget able to connect to my friend's webdavs server.. but dolphin can't. it just keeps waiting for ages
Issues with my NFS systemd mount units
Troubleshooting Systemd Automounts Causing Boot Delays
Manjaro maintenance
Is it possible to bypass a directory using org.gtk.vfs.UDisks2VolumeMonitor?
Systemd per Script mit root rechten Prüfen lass ob Netzlaufwerke gemounted sind
Problem trying to automount a drive
Mount Partition Automatically
"wrong fs type, bad option, bad superblock on /dev/sdb1" error mounting HDD
System freezes after sleep
Failed to start Mount removable media on sda1
System suddenly wont suspend/wake while any smb are mounted?
Disk local trash in KDE?
[root tip] Topic Collection List View
Hot swapping SATA devices (ThinkPad) / fstab questions
NVMe storage (not boot) drive is being recognized as an external drive?
NVMe storage (not boot) drive is being recognized as an external drive?
Having a strange and obscure issue happening with Dolphin/KDE and Network Drives!
Randomly getting logged out
Mounting the own samba share automatically / Photos and Videos collection for Digikam
Cannot move files between SMB shared folders with Dolphin
Problems with dolphin and webdav
Mount SMB share via CLI gives no write permission
After installation I can only see one partition and its a read only
Udev, cannot create name for USB drive
Auto mount Windows partition on startup
Networking computers together
Manjaro refuses to boot unless a specific drive is removed from fstab
Cifs - mount error(13): Permission denied
Problem mounting network drive
Probleme mit Nautilus und Netzwerkerkennung ( WSD)
Nemo and slow transfer rate issue
Thunar hanging - Synology NAS
Partition that I use for data won't automatically mount after reboot
Mounting CIFS/SMB share with fstab? Pi fails to mount share and crashes into emergency mode at boot
How to run a script on system startup
Programs forget paths from Other locations after reboot
How do I give read/write/execute permission to myself for newly partitioned SSD and Harddrives?
Manjaro not committing file system changes until the (external) drive has been ejected
External optical disk drive (CD/DVD) not showing in Dolphin file manager (doesn't work with different USB ports)
[root tip] [How To] Use systemd to mount ANY device
How do I mount a share with a space in the name
Zyxel nsa320s access
Directory structure question
NFS4: Couldn't follow remote path
How to make permanent links and shortcuts to Windows partition
What am I doing wrong when I set up SMB or FTP automount for /etc/fstab?
Portable USB drive permission denied
Como sincronizar carpeta de NAS con Manaro KDE
Samba help - drive available in Dolphin
Unable to show in Dolphin fuse mounted locations
Mounting external devices
Dump to the command line if a drive is not found in the fstab file
Cifs/smb mount with hidden credentials
CIFS Shares used to work, now won't mount at all
CIFS Shares used to work, now won't mount at all
[root tip] [How To] Step By Step - File Share Using Samba
Dolphin problem with automounting removable drive
Boot process is not terminated (former: Time-Out for USB-devices)
Troubleshooting mount unit naming
Odd Issue With Samba Cifs Auto-Mount
Path to NAS / network shared folders from /
Auto mount shared network folder in wine applications and edit /etc/fstab
When I open a folder using vscode in KDE, I cannot find the remote location
Hyper-V: Access to USB-stick and shared directory
Automouting nfs using systemd - dolphin is erratic
Troubleshooting mount unit naming
Freeze @ reboot, shutdown etc
SSD and HDD mount after manjaro install
Where is the automount option for hard drives on gnome
Fusermount3: user has no mount access to mountpoint (SSHFS)
Fstab NAS mount fails after update
User groups not lining up
Chown user:group directory not changing user or group (automonted partition)
/home on a separate HDD. Nautiuls/file picker slooooow to open
Welcome and introduce yourself - 2022
NTFS-3G mount / unexplained change
Mounting my enterprise dropbox that has spaces in the dir name as ~/Dropbox in fstab works, but then it uses double the diskspace! Can you avoid this?

Thank you for your great tutorial, it helped me a lot to learn about systemd. Never used it before, always made changes to fstab.

Here

the path has to be:
/etc/systemd/system/data-webdav-service.automount

I used your guide to setup a webdav-mount and webdav-automount unit (nextcloud). Everything worked fine, except at shutdown I got:

[Failed] Unmounting /run/user/1000

and at reboot some

[Skip] Ordering cycle found, skipping ∆∆xtcluod (/media/cloud)

After reboot the automount unit was disabled.

Checked with:
sudo systemd-analyze verify media-cloud.automount and sudo systemd-analyze verify media-cloud.mount
and got ordering cycle messages.

So I commented out the lines with network-online.target in both units, reloaded daemon and enable automount. After that, everything went fine, no errors, automount enabled after reboot.

Do I need the After=network-online.target & Wants=network-online.target lines and if so, how to avoid the ordering cycle?

To enhance the info, here is an example of a bind-mount which can become very useful:

boot.mount
[Unit]
Conflicts=umount.target

[Mount]
Where=/boot
What=/efi/Manjaro
Type=none
Options=bind

[Install]
WantedBy=local-fs.target

This assumes you have your ESP mounted on /efi and all your files that normally are under /boot have been installed under /efi/Manjaro :wink:

Would you be able to provide an example of mounting a LUKS encrypted drive using systemd? I can mount the drive once I’ve run cryptsetup luksOpen /dev/sda1 cryptdata, but I’m not sure how to specify this within a systemd mount unit.

@Feakster
You don’t mount encrypted volumes, but as the command says you “Open” them.
Therefore you need to specify it in /etc/crypttab.initramfs.
That extension will place it in the ramdisk only, you can use the plain (without extension initramfs) if you only want it in your regular system.

Afterwards you can mount the volume as usual.
Eg. in your example that would become /dev/mapper/cryptdata as drive/partition name.

Either the above or create a service unit that opens the encrypted volume :wink:

You may also read systemd-cryptsetup(8) as a related documentation.

1 Like

This isn’t a good example for a SMB mount. There is no : in a samba mount URL. Also it should start with //

For example

[Mount]
What=//IP-address/video

Thank you for spotting that - I will check it - and done :ballot_box_with_check:

A post was split to a new topic: Samba low transfer rates using systemd mount unit

The following fact is not completely clear from the tutorial and cost me a bit of research until I found out:
For an automount, both .mount and .automount files are needed. Maybe you can add this to the description.
Other than that: Great article, I managed to (auto)mount my network shares in a good manner for the first time - thanks a lot!

I may have missed the above sentence

1 Like

Well, yeah, I was wondering if a “mount unit” could also be a logical internal data structure (so I started merging the automount and mount examples into one single automount file).
But I guess you are right, this might have been my own stupidity…

A post was split to a new topic: Nfs share setup to automount in a laptop client