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
1 | 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
1 | 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.
1 | 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.
1 2 | mkdir ~/.mount-credentials nano ~/.mount-credentials/myshare |
The syntax here is pretty straightforward.
1 2 | username=your_username password=your_password |
Now make sure only the root user can change the content.
1 | 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.
1 | nano /etc/systemd/system/mnt-network-myshare.mount |
And add the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [Unit] Description = My Share mount Requires = dbus-org.freedesktop.nm-dispatcher.service After = network-online.target Wants = network-online.target [Mount] 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 [Install] WantedBy = multi-user.target |
A few notes:
- If your network is not managed by
NetworkManager
but bysystemd-networkd
, you probably need to change theRequires =
line (line 3) toRequires = systemd-networkd.service
. It might also be possible that the symlink for yourNetworkManager
is a different one. Have a look in/etc/systemd/system/
to find out. vers=3.1.1
needs to be adjusted based on the version of samba running on your remote systemuid=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.
1 | nano /etc/systemd/system/mnt-network-myshare.automount |
Add the following:
1 2 3 4 5 6 7 8 9 10 | [Unit] Description = Automount for My Share ConditionPathExists = /mnt/network/myshare [Automount] Where = /mnt/network/myshare TimeoutIdleSec = 0 [Install] 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
Type =
tonsf
instaed ofcifs
And … action
Now it’s time to let systemd
know about the new files and mount your network share.
1 2 | 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.
1 | systemctl status mnt-network-myshare.mount |
Should output something like this:
1 2 3 4 5 6 7 8 9 10 11 | ● 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: Process: 13005 ExecMount=/bin/mount 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.
1 | 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.