Linux – On-Demand NFS and Samba CIFS Connections With Systemd Automount

There are times when you want to mount a remote network location into your local file system. Especially when you have a NAS or cloud storage with a provider who provides CIFS or NFS access to it. One way would be to add some lines to your /etc/fstab file to mount them every time your system starts. It is OK to do so. But … how about only mounting the network file system when it’s needed? Automounting on-demand via systemd to the rescue.

Instead of trying to mount the network shares at login or boot, we’re going to mount them on-demand by combining systemd mount and automount units. With this method, the shares won’t appear until you navigate to the designated mount points in the terminal or file manager.

Install Required Packages

First, we need to make sure the needed prerequisites are installed.

Example: Gentoo Linux

emerge -av net-fs/samba net-fs/nfs-utils net-fs/cifs-utils

And make sure to have the appropriate kernel file system options for NFS and CIFS enabled as well.

Example: Debian-Based Linux Distributions

sudo apt install samba cifs-utils nfs-common

Create a Mount Point

You need to create a dedicated directory you want to mount your network share.

mkdir -p /mnt/network/myshare

Create a Credentials File

In case your network shares require authentication, which they definitely should, you need to create a credential file containing the username and password for the said network share. Credential files should be saved in a safe location with restricted permissions.

mkdir ~/.mount-credentials
nano ~/.mount-credentials/myshare

The syntax here is pretty straightforward.

username=your_username
password=your_password

Now make sure only the root user can change the content.

chmod 600 ~/.mount-credentials/myshare

Create the Systemd Unit Files

To make this work, we need (2) unit files for each connection: the .mount unit and the .automount unit. These use a specific naming convention that follows the path of the mount point. For example, if our share is at /mnt/network/my-share the mount file should be named mnt-network-myshare.mount. The same goes for the automount file. If you don’t name the units this way they will not work.

nano /etc/systemd/system/mnt-network-myshare.mount

And add the following:

Example: CIFS

[Unit]
Description = My Share mount
Requires = dbus-org.freedesktop.nm-dispatcher.service
After = network-online.target
Wants = network-online.target

[Mount]
What = //example.server/myshare
Where = /mnt/network/myshare
Options = credentials=/home/your_username/.mount-credentials/myshare,vers=3.1.1,uid=your_username,iocharset=utf8,file_mode=0655,dir_mode=0655,noperm
Type = cifs
TimeoutSec = 30

[Install]
WantedBy = multi-user.target

Example: NFS

[Unit]
Description = My Share mount
Requires = dbus-org.freedesktop.nm-dispatcher.service
After = network-online.target
Wants = network-online.target

[Mount]
What = example.server:/myshare
Where = /mnt/network/myshare
Options = defaults
Type = nfs
TimeoutSec = 30

[Install]
WantedBy = multi-user.target

A Few Notes

  • If your network is not managed by NetworkManager but by systemd-networkd, you probably need to change the Requires = line (line 3) to Requires = systemd-networkd.service. It might also be possible that the symlink for your NetworkManager is a different one. Have a look in /etc/systemd/system/ to find out.

cifs specific:

  • vers=3.1.1 needs to be adjusted based on the version of samba running on your remote system
  • uid=your_username – This is your local system user. You can also provide the user ID here.

nfs specific:

Important to know that nfs doesn’t support credential files for its mount options. So you’ll be probably asked for your credentials when the share is being mounted. I haven’t tested it, since I don’t use nfs on any of my systems.

kernel>=5.18.8

If you’re running a kernel newer or equal to version 5.18.8 you might notice that your CIFS mount doesn’t work as expected, as in it won’t mount at all. To fix this, you need to set the Samba version string in your mount options to a .0 version. For example vers=3.0 or vers=2.0. This is due to auto-negotiation between your kernel and the remote (probably older) Samba server is broken for the newer kernel due to something the Samba server doesn’t quite understand correctly. (See https://lore.kernel.org/all/87edz63t11.fsf@cjr.nz/T/)

Auto Mount

Next, we need to create the automount file in the same location.

nano /etc/systemd/system/mnt-network-myshare.automount

Add the following:

[Unit]
Description = Automount for My Share
ConditionPathExists = /mnt/network/myshare

[Automount]
Where = /mnt/network/myshare
TimeoutIdleSec = 0

[Install]
WantedBy = multi-user.target

And … Action

Now it’s time to let systemd know about the new files and mount your network share.

systemctl daemon-reload
systemctl start mnt-network-myshare.mount

If all went well, you should see your file shares at their designated mount points. To verify, check the status of your service and look for any errors.

systemctl status mnt-network-myshare.mount

Should output something like this:

● mnt-network-myshare.mount - My Share mount
   Loaded: loaded (/etc/systemd/system/mnt-network-myshare.moun; static; vendor preset: enabled)
   Active: active (running) since Fri 2022-04-29 10:10:03 CEST; 1min 1s ago
    Where: /mnt/network/myshare
     What: //example.server/myshare
  Process: 13005 ExecMount=/bin/mount //example.server/myshare /mnt/network/myshare -t cifs -o vers=3.1.1,credentials=/home/your_username/.mount-credentials/myshare,iocharset=utf8,rw,x-systemd.automount,uid=1000 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/mnt-smb-sambashare.mount

Apr 29 10:10:03 your_pc systemd[1]: Mounting My Share mount...
Apr 29 10:10:03 your_pc systemd[1]: Mounted My Share mount.

Next, enable your automount files to start at boot. This will allow your shares to mount on-demand.

systemctl enable mnt-network-myshare.automount

That’s it! To test, reboot your system, open the mount point in the terminal or the file manager, and your share will mount before your eyes.

Leave a Reply

Your email address will not be published. Required fields are marked *