First, let's see how to mount the remote directory. Assume that there is a shared folder over the network at \\192.168.1.1\users\self\shared
which is accessible with user myuser
and password secret123
.
We could mount it manually in /mnt/winshare
with:
# mount -t cifs //192.168.1.1/users/self/shared /mnt/winshare -o user=myuser,password=secret123
This should work on your Linux box, because systemd will basically call mount with the same arguments: What
(//192.168.1.1/users/self/shared
), Where
(/mnt/winshare
) and Options
(user=myuser,password=secret123
).
To configure systemd to automatically mount that network folder on boot, there are 2 files needed: mnt-winshare.mount
and mnt-winshare.automount
.
The .mount
unit file specifies how to mount a drive (man systemd.mount
for details), while the .automount
unit file specifies what to mount automatically on boot (man systemd.automount
).
Important! Note that the name of the file must map to the actual filesystem mount target, that is mnt-winshare.(auto)mount
refers to /mnt/winshare
. E.g. to mount in /home/user/myfolder
the file names must be home-user-myfolder.(auto)mount
.
The content of the files is the following:
# cat /etc/systemd/system/mnt-winshare.mount
[Unit]
Description=Remote Win Mount
[Mount]
What=//192.168.1.1/users/self/shared
Where=/mnt/winshare
Type=cifs
Options=user=myuser,password=secret123
[Install]
WantedBy=multi-user.target
and
# cat /etc/systemd/system/mnt-winshare.automount
[Unit]
Description=Automount Remote Win Mount
[Automount]
Where=/mnt/winshare
[Install]
WantedBy=multi-user.target
The former file goes in /etc/systemd/system/mnt-winshare.mount
while the latter /etc/systemd/system/mnt-winshare.automount
.
Then, reload the units to ensure everything is to its latest version:
# systemctl daemon-reload
At this point, we should be able to manually mount and unmount the remote folder using systemctl
:
# systemctl start mnt-winshare.mount
# systemctl stop mnt-winshare.mount
This will not, however, automatically mount the system at startup. To do so, just enable the automount
# systemctl enable mnt-winshare.automount
And this should be it!
There's a slight difference between this method and /etc/fstab that's worth noting. Writing this here in case it helps others.
In fstab you need to escape and convert special characters to ascii, so if the share has a space in the name you need to include
\040
if the share has an & in the name you need to use
\046
. This is not the case for the systemd method. In theWhere=
just put the literal string with special characters.Another shoutout is how to manage paths on the local directory with a hyphen in the name, given the naming convention of the systemd .mount file replaces slashes with hyphens.
In this instance you want to use the ascii code for hyphen in the filename which is
\x2d
So the path:
/mnt/my-windows-share
Becomes:
/etc/systemd/system/mnt-my\x2dwindows\x2dshare.mount
And a subsequent
.automount
file to matchYou can either put the file in quotes when creating it, or double escape it
vi /etc/systemd/system/mnt-my\\x2dwindows\\x2dshare.mount
orvi "/etc/systemd/system/mnt-my\x2dwindows\x2dshare.mount"