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 absolutely 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, which contains the username and password for the said network share. Credential files should be saved in a safe location with restricted permissions.
The syntax here is pretty straightforward.
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.
And add the following:
Description = My Share mount
Requires = dbus-org.freedesktop.nm-dispatcher.service
After = network-online.target
Wants = network-online.target
What = 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 = 60
WantedBy = multi-user.target
A few notes:
- If your network is not managed 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
NetworkManageris a different one. Have a look in
/etc/systemd/system/to find out.
vers=3.1.1needs 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.
Next, we need to create the automount file in the same location.
Add the following:
Description = Automount for My Share
ConditionPathExists = /mnt/network/myshare
Where = /mnt/network/myshare
TimeoutIdleSec = 0
WantedBy = multi-user.target
The above example is for CIFS shares. With minor tweaks this can also be used for NFS shares. In order to use this with NFS shares change the following:
What =to the remote location of your NFS share. Example:
What = example.server:/srv/myshare
And … action
Now it’s time to let
systemd know about the new files and mount your network share.
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
What: Process: 13005 ExecMount=/bin/mount Tasks: 0 (limit: 4915)
Apr 29 10:10:03 your_pc systemd: Mounting My Share mount...
Apr 29 10:10:03 your_pc systemd: 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.