I use the systemd-mount method (which automatically mounts / unmounts the share, seamlessly.)
I can share a template of what I use, and then let you change it for yourself and explore it further.
First you need to create two systemd units: one for the actual mount, and the other for the automount trigger.
Secondly, if you don’t want the password to be used in the systemd mount unit itself, you can create a hidden / restricted-access file that contains the credentials.
It’s easier to manage if you create a hidden directory named “.cifs” in your home folder, in which only your user has access to it. From here, you can create templates and use them to copy over the the actual system folder.
Replace all instances of “sharename” with the actual share’s name configured in your SMB server. (Do the same for mentions of “winnie” or the home folder location.)
mkdir ~/.cifs
Inside this folder, create a file named .cifs-sharename-credentials
with the contents that look like so:
username=winnie
password=iLovePasta2022
Restrict it for only your user:
chmod 600 ~/.cifs/.cifs-sharename-credentials
Also in this folder, create two files. The names must follow this format. They instruct systemd where the mount paths exist. (Example: “mnt-cifs-sharename” tells it that the mount location will be /mnt/cifs/sharename
)
File name mnt-cifs-sharename.mount
with the contents:
[Unit]
Description=CIFS network mount for sharename
After=network-online.target
Wants=network-online.target
[Mount]
What=//192.168.0.22/sharename
Where=/mnt/cifs/sharename
Options=cache=loose,iocharset=utf8,rw,actimeo=60,x-systemd.automount,uid=1000,gid=1000,forceuid,forcegid,file_mode=0775,dir_mode=0775,credentials=/home/winnie/.cifs/.cifs-sharename-credentials
Type=cifs
TimeoutSec=10
[Install]
WantedBy=multi-user.target
File name mnt-cifs-sharename.automount
with the contents:
[Unit]
Description=Automount for CIFS sharename
[Automount]
Where=/mnt/cifs/sharename
TimeoutIdleSec=120
[Install]
WantedBy=multi-user.target
Now copy those two files into /etc/systemd/system/
and reload the systemd daemon.
sudo cp -v ~/.cifs/mnt-cifs-sharename.mount /etc/systemd/system/
sudo cp -v ~/.cifs/mnt-cifs-sharename.automount /etc/systemd/system/
sudo systemctl daemon-reload
And enable only the automount unit:
sudo systemctl enable mnt-cifs-sharename.automount
Now when you browse to /mnt/cifs/sharename
, it will automatically try to connect and mount it via the CIFS kernel module. After two minutes of inactivity, it will automatically unmount it.
To make things more streamlined, you can create a “Favorite” in your file manager that points directly to /mnt/cifs/sharename
, and it will look like a local folder.
Look over every single option and contents. They will differ greatly on your setup. Everything must be consistent, or it will fail. I used “winnie” and “sharename” among other examples.
NOTE: Starting with kernel 5.17, you may need to include a third entry in your credentials file (“domain”) and set the domain to “workgroup” or whatever is configured in your SMB server, like so:
username=winnie
password=iLovePasta2022
domain=workgroup