While the procedure outlined below works, setting up systemd automounts is actually far easier than I realized and can be done with just the
You can just put the following into
<ip or hostname>:/path/to/nfs/export /mnt/test nfs x-systemd.automount, \
x-systemd.DirectoryMode=true,x-systemd.TimeoutIdleSec=10min 0 0
systemctl start mnt-test.automount
And that's it. You don't need to create the systemd unit file because systemd does it for you in the background based on what's in the fstab file.
Systemd has the ability to manage file systems in the same way it can manage services on a Linux system.
A Systems Administrator can replace the autofs service with the systemd automounter.
Why should I replace autofs?
Having a consistent interface to manage services and mount points on a system is an advantage all on its own.
Beyond the consistency with the interface, using the systemd automounter means you get to eliminate a running service (autofs).
- One less moving part.
- systemd manages the autofs service, which in turn manages automount points.
- why not cut out the middle man and just use systemd?
- One less package to manage.
- One less potential package to keep you up over the weekend patching for a vulnerability.
- The autofs configuration files are cryptic and not very user friendly.
- systemd unit files are well understood and the automounter hooks into fstab which is a universally known location for identifying mount points.
The real question is: Why are you still installing autofs?
How to set up systemd automount with NFS
For this example you'll need an NFS file system available to you, though you could also use an lvm based file system for testing. See my linux lvm guide for more information on setting up lvm if you aren't sure how.
Step 1 - Create a unit file
Using your favorite text editor create a file at
vim isn't your favorite text editor, then you should feel bad about yourself... run
vimtutor in your terminal someday when you have 45 minutes to burn and learn all about what you've been missing.
mnt-test.automount file should contain the following.
Description=Mount nfs export
What=<ip or hostname>:/path/to/nfs/export
About this file.
This is a pretty bare bones unit file. If you've used systemd to create service files this should look pretty familiar.
Description: The description can be anything you want it to be. I would include the name of the volume you are mounting and maybe the source server or application it's for.
[Automount]: Systemd will use this section to determine what to mount, where to mount it, what type of file system it is, and other options.
What: What are we mounting. For nfs this would be something like
Where: Where systemd should mount the volume. Seems pretty self explanatory.
Type: What type of file system is this? xfs, nfs, ext4... whatever it is put it here.
DirectoryMode: This tells systemd to create any paths that don't already exist. If this works correctly you shouldn't have to create /mnt/test, systemd will do that for you.
TimeoutIdleSec: How many seconds after no activity has been detected on a mounted file system should systemd wait to unmount it.
Options: You can put any mount options you want here. I just used defaults to illustrate it as an example.
Step 2 - Update the fstab file
Once again using your favorite text editor, open
/etc/fstab and add your nfs export to the file specifying
x-systemd.automount where the mount options would normally go, as shown below.
<ip or hostname>:/path/to/nfs/export /mnt/test nfs x-systemd.automount 0 0
Step 3 - Refresh services
Reload the system daemon, start, and enable the mnt-test.mount unit.
sudo systemctl daemon-reload
sudo systemctl start mnt-test.automount
sudo systemctl enable mnt-test.automount
Otherwise you can reboot the system if you want.
Things to Note:
systemd.automount man pages:
Automount units must be named after the automount directories they control. Example: the automount point /home/lennart must be configured in a unit file home-lennart.automount.
Make sure you name the unit file to match this syntax. If you wanted to mount a volume to
/backups/laptop then you would name your unit file
backups-laptop.automount. Otherwise you'll get errors and the mount unit will refuse to start.
If you found this useful please support the blog.
I use Fastmail to host my email for the blog. If you follow the link from this page you'll get a 10% discount and I'll get a little bit of break on my costs as well. It's a win win.
Backblaze is a cloud backup solution for Mac and Windows desktops. I use it on my home computers, and if you sign up using the link on this page you get a free month of service through backblaze, and so do I. If you're looking for a good backup solution give them a try!