On my main workstation running Fedora 36, I use both autofs and Systemd automount to mount NFS on demand. Gradually, I’m leaning toward just to use Systemd automount.

Here is just a brain dump with an example of mounting an NFS share with Systemd autmount. Though, I had done this before, I found this blog post, available at [1], provides details on how to do this.

Assumption

  • The NFS server and share: banan.example.com:/srv/music
  • The preferred local mount point: /srv/banan/music

Create required Systemd unit files

We will need 2 unit files: the mount unit and the automount unit.

Since I’ll be mounting the NFS share to /srv/banan/music, the name of the systemd unit files should be: srv-banan-music.mount and srv-banan-music.automount by convention.

Here is what I have for srv-banan-music.mount, also showing the location where tha file should be.

➜ cat /etc/systemd/system/srv-banan-music.mount
[Unit]
Description=music share on banan

[Mount]
What=banan.example.com:/srv/music
Where=/srv/banan/music
Type=nfs4
Options=defaults

[Install]
WantedBy=multi-user.target

And here is the content of srv-banan-music.automount:

➜ cat /etc/systemd/system/srv-banan-music.automount
[Unit]
Description=music share on banan

[Automount]
Where=/srv/banan/music
TimeoutIdleSec=60

[Install]
WantedBy=multi-user.target

Note the TimeoutIdleSec=60 will instruct the automount to unmount the NFS share if it’s idled for 60 seconds.

Enable and start NFS share mount

First, we need to ensure the Systemd is aware the newly created systemd units.

➜ sudo systemctl daemon-reload

Then, we start and enable the automount unit, so that the mount point will be mounted (for the lack of a better word) on-demand.

➜ sudo systemctl enable srv-banan-music.automount --now
Created symlink /etc/systemd/system/multi-user.target.wants/srv-banan-music.automount → /etc/systemd/system/srv-banan-music.automount.

If everything is working correctly, you should see a similar output:

➜ mount | grep -i systemd
systemd-1 on /srv/banan/music type autofs (rw,relatime,fd=59,pgrp=1,timeout=60,minproto=5,maxproto=5,direct,pipe_ino=231283)

Finally, we can test the NFS share and verify it. Note, we also need to create the local mount point too.

➜ sudo mkdir -p /srv/banan/music

➜ mount | grep banan
systemd-1 on /srv/banan/music type autofs (rw,relatime,fd=59,pgrp=1,timeout=60,minproto=5,maxproto=5,direct,pipe_ino=231283)
banan.example.com:/srv/music on /srv/banan/music type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.222,local_lock=none,addr=192.168.1.111)

➜ ls -1 /srv/banan/music/
Classical

You can learn more about this from the link at [1]. The author in that blog also shows how to use Systemd Automount to mount Samba share too.

Reference

[1] On-Demand NFS and Samba Connections in Linux with Systemd Automount