[HowTo] Automount ANY device using systemd

Disk device recognition

Manjaro uses udev (see Arch Wiki) to load devices at boot time. The loading of devices is arbitrary and therefore you cannot predict which device will be available at a given path.

But static device names do exist and you can assign specific locations to your device and thus ensure e.g. scripts will work as expected.

What to learn

  1. Overview of system mount units
  2. Structure and Content of a mount unit
  3. Mount at boot (immediate mount)
  4. Mount at demand (mount on first access)
  5. Conclusion

1. What is systemd mount units

The systemd manual has the following somewhat contradicting statement

Mount units may either be configured via unit files, or via /etc/fstab (see fstab(5) for details). Mounts listed in /etc/fstab will be converted into native units dynamically at boot and when the configuration of the system manager is reloaded. In general, configuring mount points through /etc/fstab is the preferred approach. See systemd-fstab-generator(8) for details about the conversion.- system.d.mount.5.html#FSTAB (Emphasized by me)


As mounts listed in fstab will be converted into native units dynamically at boot - logically we create the units ahead so the system don't have to bother with converting fstab - the contradiction in above statement is fstab is the preferred approach.

What? Why would would it be preferable to use a configuration which must be parsed and converted when you can create native units?

I recommend mount units because

  • They are easy to create
  • They provide clear logic of
    • What
    • Where
    • When
    • Why
    • How
  • Simple auto mount defines

Auto mounting a device using systemd is a simple secure approach. For one - you don't have to edit your fstab - the slightest error in fstab can render your system unbootable.

This article will use an USB device which may or may not be available at boot time and when you plug it you want it to be mounted on a special path - it could be dedicated backup disk.

The basics of this guide can also be used to mount any extra devices available in your system, or remote shares e.g. WEBDAV, SMB, FTP and NFS. You can find link to a topic containing sample mount units at the end of this article.

2. Structure and Content of a mount unit

There is no rules as to where you can mount the device. Just remember a few things

  • Structure
  • Recognizable

By structure I mean organizing a directory tree - I usually recommend create a data folder on root and populate the tree with your devices and locations like below.

By recognizable I mean naming your mount point with a logical description of the content - but don't use dashes anywhere in the mount point - see explanation below. If you really need a double word in your data tree - use a low dash ( _ ). You should also avoid local accented characters (e.g. danish Γ¦ ΓΈ Γ₯ Γ† Ø Γ…)

Immediate branches are your devices - and network share mounts in a branch named for the type of fileservice e.g. NFS and SMB.

Sample tree
➜  ~ tree /data -L 2
β”œβ”€β”€ backup
β”œβ”€β”€ build
β”œβ”€β”€ nfs
β”‚   β”œβ”€β”€ data
β”‚   β”œβ”€β”€ music
β”‚   β”œβ”€β”€ photo
β”‚   β”œβ”€β”€ video
β”‚   └── web
β”œβ”€β”€ private
β”œβ”€β”€ projects
β”œβ”€β”€ smb
β”‚   └── web
└── virtualbox

Naming the mount units uses a mandatory convention.

Lets say you have a disk for backup and the disk mounts to /data/backup then the filename becomes.

  • data-backup.mount

:exclamation: Dashes in the file name represents the directory separator - so don't use dashes anywhere in the mount point.

Create a file as root

$ sudo touch /etc/systemd/system/data-backup.mount

Open the file with your favorite CLI editor or a gvfs compliant editor (xed or gedit)

$ xed admin:/etc/systemd/system/data-backup.mount


Insert the following template into the file - and modify the description - continuing our backup disk example

Description=Mount Backup disk (/data/backup)




The section [Mount] is the same data as you see in your fstab - except the What which must the full path to the device. The nature of systemd makes it impossible to rely on the traditional path /dev/sda1 - luckily there is another /dev/disk/by-uuid/ we can use.


To get the device uuid you can use lsblk command and the -no UUID to retrieve the uuid you need for the mount. (substitute sdy1 with your actual device and partition)

$ lsblk -no UUID /dev/sdy1

Then insert the UUID into the file



Next type in the path where you want the device mounted. Normally we would create the path beforehand as this is required for fstab - but not with systemd. If the path do not exist it will be created.

:information_source: default permissions is 755 owned by root

If your actual device is an existing device and the owner:group UID:GID do not match your device will be mounted read-only.


:exclamation: Comment by @FadeMind

Caveat using dashes in a folder-name

Don't use a dash - in your path because a dash refer to a new branch on the folder tree and this will break the naming of the mount/automount file

  • Invalid /data/home-backup
  • Valid /data/home_backup
  • Valid /data/home\x2dbackup

To get the correct name for your mount/automount filename when you use dashes in your folder-naming

$ systemd-escape -p --suffix=mount "/data/home-backup"


The Type is optional for disk devices - but from the viewpoint - the more information we provide - less work for the system.

You can retrieve the file system the same way as the UUID.

$ lsblk -no FSTYPE /dev/sdy1

Other types like NFS, SMB and FTP requires Type to be set.


The Options is optional for disk devices.

As with fstab it takes a comma separated list of options for the filesystem mounted.

Other types like NFS, SMB and FTP requires Options to be set.


Save the file

3. Mount on boot

This works just like your fstab - if the device is not available - the system will use the default 90 seconds for the device to be available. The behavior can be changed by setting TimeoutSec= to a value matching the device actual mount time or use an additional automount unit.

First reload the systemd daemon

$ sudo systemctl daemon-reload

Note the state of the mount

$ systemctl show -p ActiveState -p SubState --value data-backup.mount

Mount the device

$ sudo systemctl start data-backup.mount

Now you can verify the service

$ systemctl show -p ActiveState -p SubState --value data-backup.mount

Note that the folder was created by systemd - list the content of the mount
:information_source: note permissions changed to partition content

$ ls -la /data/backup

If the device is an internal device always available you can enable the mount and your device will be mounted during boot.

sudo systemctl enable data-backup.mount

If - on the other hand - this is an USB device - which is not available at boot time - do not enable the the mount unit but proceed to create an automount unit for your USB device.

Check permissions

If this is a newly formatted device or a reused device - it is wise to check the permissions. If you get issues with the device being mounted read-only - depending on filesystem - it may be due to the permissions stored on the device.

If the permissions are not correct - change permissions. There is no definitive answer - search and you will find.

4. Mount on demand

An automount unit is a special unit which takes care of activating the corresponding mount unit when the mount path is accessed. The difference for mount and automount is when it is mounted.

To demonstrate the automount - first unmount the data-mount

$ sudo systemctl stop data-backup.mount

Verify the content of the mount point - it should now be empty.

$ ls -la /data/backup

Now we proceed to create an automount for the mount. Create a file - it must be named using the same rule as the mount but with the extension of .automount

$ sudo touch /etc/systemd/system/data-backup.automount

:information_source: the automount service will fail if the device path is mounted

Automount define

Edit the file and insert the following content and save it.

Description=Automount backup partition



Test automount

Now enable and start the automount for data-backup

$ sudo systemctl enable --now data-backup.automount

You know you unmounted the device - don't mount it - just access the /data/backup folder from terminal or a file manager - you will find systemd mounts it behind the scenes.

5. Conclusion

This approach is a safer way than editing your fstab. Worst case the device is not mounted - if you make a mistake in fstab - the system breaks.

You can mount all kind of devices - NFS, SMB, FTP, WEBDAV, SFTP, SSH - using this recipe. Sample files for different kind of mounts can be found in this topic.

Automount external HDs
Can't write to USB HDD after reformating (unless I'm root)
secondary drives/partition not always being mounted
Automount external HDs
Trouble with fstab and susequent automounting
Can anybody help me about automount
access webdavs (https file server) - dolphin on manjaro xfce
How do I map Google as network drive - without sync?
Synology NAS backups
Manjaro Boots slower than Ubuntu
Want to completely switch from windows and don't want to lose any data from my hdd.
KDE wallpaper(from 2nd HDD) becomes blank after restart
Sharing data on multi-boot platform
Problems with Mounting CIFS Share
SMB or NFS mounting not working
mounting NAS-folder
HDD privileges access and issues
SSD fΓΌr Spiele einbinden? - bin blutiger AnfΓ€nger :D
cant do anything on my Secondary hard drive on Dual Boot
strange VLC behavior with synology NAS
Auto mount HDD?
USB hard drive won't mount
Nextcloud Webdav share won't be mounted
Mount a mount point available in desktop/file manager by command line
VirtualBox VMs on a seperate NTFS partition unaffected by re-installs of host OS
Auto mounting a external HDD
Writing Systemd Service Units
How can I ensure the path to my devices
Can't write to Second drive (Game Drive)
Need help booting after a storage partition failed to mount?
Mount points are being switched/reversed between two SSD.
Wine cannot find /run/media/username
Symlinks broken after reboot
Symlinks broken after reboot
Issues mounting an FTP share with curlftpfs
Second Hard Drive
I have built a music server/desktop and documented it on diyaudio
How do you make a drive mount on start up?
gnome disk write fstab with no password
Network Drive Mapping - 19.0.2 - KDE - Dolphin
Writing Systemd Service Units
Replace user folders with cloud based ones?
[HowTo] Sample mount units for systemd
Confused about Folder/File Permissions
Installing dual boot in SSD and HDD
Mount ΓΌber fstab funktioniert nicht immer
Trouble with fstab and susequent automounting
Permissions when using fstab to mount a cifs share
Reformat other partition but keep current installation
Can't make my 2nd drive to automount
Manjaro Openbox 20 no reconoce passaword usuario, solo se puede iniciar como root
Partition on installing
Nutzer-Ordner von /home separat auf eine andere Partition verlegen
Mount my HDD used by Windows 10 without risking files
How to totally reset Non boot able manjaro without resetting disks?
[HowTo] Automount ANY disk device (fstab)
Issues with fstab for automount
Accessing in thunar mounted network folders from terminal
i can't understand how to make a hhd my game drive (steam)
NFS connection between desktop and laptop not working on Manjaro
how to rename an usb stick drive in manjaro xfce ? gparted not helping
How to add more storage to Manjaro
Big Picture overlay refuses to work correctly
Showing partition labels instead of mountpoint
How To Install STEAM Games On External SSD/HDD...
Help with partitioning for new install
Multi storage devices mounting
Cant Reach NAS on my network

Thank you for this great article, @linux-aarhus! :+1:

1 Like

Nice article. Thanks.

1 Like

@linux-aarhus two things:
LABEL cannot have extra marks like -
For automatically creating VALID unit name use (for example PATH /data/backup)

$ systemd-escape -p --suffix=mount "/data/backup"

systemd services units should be placed IN
and at end

sudo systemctl enable foobar.mount

creating SYMLINK in /etc/systemd/system/.

1 Like

What are you referring to?

That would be correct if it was a service or unit installed by the package manager.

Units and services created by a system admin are placed in /etc/systemd/system

Depending on use case and necessary permission a user would place units and services in /etc/systemd/user or in $HOME/.config/systemd/user

1 Like
systemd-escape -p --suffix=mount "/foobar/foo-bar"

and in final name \ is cut = systemd error service. Missmatch Unit = automount service will not work here at all.

You are referring to the situation where you have a - in the desired mount point you are absolute correct.

As the naming convention for the mount is inferred from the actual mount point it seems logical - at least from my point of view - to not use dashes for the desired path simply because the dash in the filename indicates a path separator.

I agree - avoid dashes - use lodash if a separation of words is necessary or use the \x2d as representation for a path with included dash.

I have added a :exclamation: point to the Where part.

Systemd don't accept it /data/home\x2dbackup
cause from FILENAME \ just gone and in final is not valid. I tested self day ago and report to you issue with.

\ is an escape character. You have to write \\ if you mean \

Escape character is skipped in final FILENAME
after enabling or starting service - failed.

I think we mean the same thing - just different ways of saying it.

Of course a dash in the mount path is valid - the problem is that systemd interprets a dash in the filename as a path separator.

So if you use a dash in the folder name - you need to escape it in the filename.

That is why I am saying - don't use dashes as part of a folder name.


Forum kindly sponsored by