Systemd: (Auto-) Mount cifs shares

Hi,

systemd can mount cifs filesystems at boot or on demand like autofs.

First cifs mount at boot time. Determine the share and the mount point.

For example:

Share : \\192.168.56.1\mp$
Local mountpoint: /mnt/mp
User : yourCifsUser
Windows Domain : YourDomain (this is optional, also in the unit file and only necessary if you use a Domain Login)


Create the mount point

root@debdev:~# mkdir /mnt/mp

Create the systemd definition file. The Folder for custom systemd unit files is /etc/systemd/system.

Create a new file mnt-mp.mount in the diretory /etc/systemd/system. The filename must contain the mountpointname where the slashes are replaced with “minus”. Filename for /mnt/mp => mnt-mp.mount

If this does not match you will get an error like:

root@debdev:~# journalctl |grep storage
storage-cifs.mount's Where= setting doesn't match unit name. Refusing.

This is a working unit


[Unit]
  Description=cifs mount script
  Requires=network-online.target
  After=network-online.service

[Mount]
  What=//192.168.56.1/mp$
  Where=/mnt/mp
  Options=username=yourCifsUser,password=Secretpassword,workgroup=YourDomain,rw
  Type=cifs

[Install]
  WantedBy=multi-user.target

Enable the previous defined config and check if error occurs.

systemctl enable mnt-mp.mount
systemctl status mnt-mp.mount

Update: From security reasons newer Kernel releases does not allow to connect to CIFS Share that only supports SMB1. Error Message: No dialect specified on mount. Default has changed to a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS (SMB1).

To mount such shares you have to explict set the SMB protocol version to 1 by add the option vers=1.0 to the Options line in the mount unit. For example:
Options=username=yourCifsUser,password=Secretpassword,workgroup=YourDomain,rw,vers=1.0

To mount and unmount your share start or stop the unit.

root@debdev:~# systemctl start mnt-mp.mount
root@debdev:~# mount|grep 192
//192.168.56.1/mp$ on /mnt/mp type cifs (rw,relatime,vers=1.0,cache=strict,username=yourCifsUser,.....
root@debdev:~# systemctl stop mnt-mp.mount

If you make changes to your unit file while its still active, call

systemctl daemon-reload

to reload it.

It’s also possible to mount your share just on demand. Like an automounter.

Define your mount unit mnt-mp.mount like above but do not enable it via systemctl.

Create an automount unit /etc/systemd/system/mnt-mp.automount. The automount unit starts the mount unit (mnt-mp.mount) on demand.


[Unit]
  Description=cifs mount script
  Requires=network-online.target
  After=network-online.service

[Automount]
  Where=/mnt/mp
  TimeoutIdleSec=10

[Install]
  WantedBy=multi-user.target

Enable the automount unit

systemctl enable mnt-mp.automount

This will create an autofs entry in the mount tab

root@debdev:~# mount|grep systemd
systemd-1 on /mnt/mp type autofs (rw,relatime,fd=27,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)


root@debdev:~# systemctl status mnt-mp.automount
● mnt-mp.automount - cifs mount script
Loaded: loaded (/etc/systemd/system/mnt-mp.automount; enabled)
Active: active (running) since Fr 2016-03-18 12:21:37 CET; 9min ago
Where: /mnt/mp

 

and browse to the mountpoint and see what happens:

root@debdev:~ # ls -l /mnt/mp/Andreas/Adoring_Human_Flesh/
-rwxr-xr-x 1 root root 9361449 Mai 2 2015 ZOOM0041.MP3
-rwxr-xr-x 1 root root 6881906 Mai 2 2015 ZOOM0045.MP3
-rwxr-xr-x 1 root root 5959680 Mai 2 2015 ZOOM0046.MP3
-rwxr-xr-x 1 root root 5880058 Mai 2 2015 ZOOM0051.MP3

Michael

Advertisment to support michlstechblog.info

Add a comment »9 comments to this article

  1. Great article!. Very helpful. Thanks

    Reply

  2. I’m having a slight problem with this, the shares are being mounted fine. But only the root user can read/write to them. Can’t seem to get it to work for user accounts on my system for some reason.

    Reply

    • Don’t worry figured it out. Before mounting the shares I had to chown the folders in /mnt first. I assumed when they were mounted they would just take the permissions from whatever was actually mounted. But I guess it doesn’t work that way with SMB shares for some reason.

      Reply

  3. thank you, very helpful and useful 🙂 In fact I thought, that the dollar symbol in the share name is a special symbol that needs to be there. After removing it from my share name it works perfectly!

    Reply

    • Hi daniquiel,

      the $ sign at the end of the share name makes the share “hidden”. Such shares cannot be found by the net view \\192.168.56.1 command and you must know the name if you want to connect.

      Michael

      Reply

  4. Thanks for the helpful post! When I use this approach, the mounted CIFS share is read write, but attempting to write to the share results in “root” being the owner of the new files. Any idea why?Or how to fix?

    Reply

    • Hi Roark,

      your CIFS Share is provided by Samba? Then I think is an Samba issue. Can you post the Samba config?

      Michael

      Reply

  5. Hi Michael,

    Please disregard my last post (I had an error in my [Unit] variables for cifs mount –

    Aug 08 12:20:01 msi-u100 kernel: CIFS VFS: bad security option: ntlm 0 0

    Fixed; and working like a boss ^_^

    Reply

    • You’re welcome 🙂

      Reply

Time limit is exhausted. Please reload CAPTCHA.

Original Theme by Schiy · Powered by WordPress