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

22 thoughts on “Systemd: (Auto-) Mount cifs shares”

  1. 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.

    1. 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.

  2. 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!

    1. 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

  3. 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?

  4. 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 ^_^

  5. Hi Michael,
    I’m trying to mount my QNAP NAS on my raspberry pi. I created the .mount file like you showed but if fails with a status 32. However, if I use the same Where and What information in fstab and run mount -a, everything works. Also, I have to use the IP address of the NAS, I can’t use the UNC name like //13740NAS/Shared

    Thoughts?
    Thanks

  6. Hi,
    great howto! Thanks.
    I observed a strange system behavior: The first time I navigate to such an automounted directory, it’s in ro mode. The sub folders are not! The second time I navigate into it, it’s in rw. Any idea why that happens?
    I use the following options:
    .mount: Options=uid=1000,gid=1000,credentials=/home/me/.smbcredentials,noauto,nofail
    No options in .automount.
    Thanks,
    Michael

  7. Hello Michael,

    Thanks for the works. Similar to what Paul complained about above, when I use the following in command line it works

    “`
    pi@System:~ $ sudo mount //10.0.0.1/’volume(sda1)’/share/HA /mnt/ha -t cifs -o username=me,password=password,vers=1.0,rw
    “`

    But I have the following in a “mnt-ha.mount” file

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

    [Mount]
    What=//10.0.0.1/’volume(sda1)’/share/HA
    Where=/mnt/ha
    Options=username=me,password=pass,vers=1.0,rw
    Type=cifs

    [Install]
    WantedBy=multi-user.target
    “`
    If I run `sudo systemctl start mnt-ha.mount`

    I get

    “`
    Job for mnt-ha.mount failed.
    See “systemctl status mnt-ha.mount” and “journalctl -xe” for details.
    “`

    Then I run `sudo systemctl status mnt-ha.mount`

    I get the following

    “`
    ● mnt-ha.mount – cifs mount script for HA
    Loaded: loaded (/etc/systemd/system/mnt-ha.mount; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Sat 2019-01-12 07:53:03 GMT; 1min 58s ago
    Where: /mnt/ha
    What: //10.0.0.1/’volume(sda1)’/share/HA
    Process: 17410 ExecMount=/bin/mount //10.0.0.1/’volume(sda1)’/share/HA /mnt/ha -t cifs -o username=me,password=pass,vers=1.0,rw (code=exited, status=32)
    CPU: 24ms

    Jan 12 07:53:03 System systemd[1]: Mounting cifs mount script for HA…
    Jan 12 07:53:03 System systemd[1]: mnt-ha.mount: Mount process exited, code=exited status=32
    Jan 12 07:53:03 System systemd[1]: Failed to mount cifs mount script for HA.
    Jan 12 07:53:03 System systemd[1]: mnt-ha.mount: Unit entered failed state.
    “`

    Any ideas please?

  8. Hi,
    thank you for this helpful howTo – thanky to this I got it working.
    Is there any possibility to avoid a credentials-file? My username, password, uid and gid are the same on the client (ubuntu) and on the smb/cifs-server (synology disk station).
    Thank you very much.

  9. On Ubuntu 18:04 this works for me.
    1. other network service
    2. added uid/gid. Otherwise the mount is readonly
    3. no password on the nas, so could be anything.

    [Unit]
    Description=cifs mount script
    Requires=NetworkManager.service
    After=NetworkManager.service

    [Mount]
    What=//192.168.1.121/NasD1
    Where=/mnt/nas1
    Options=password=test,username=guest,rw,vers=2.0,uid=1000,gid=1000
    Type=cifs

    [Install]
    WantedBy=multi-user.target

  10. Thanks for the tutorial, works fine for me if I only have one user.
    One small improvement:
    With Ubuntu 20.04 the mounts are shown twice in the file explorer (Files 3.36.3). If you add the option x-gvfs-hide to the mount options in the .mount file, its displayed only once. Perfect, in single user environment.

    So how would I do that for two users accessing the same directory with different uid.
    If I simply create two mount files and two automount files with different local mount directories (each accessible only by one user) the mounting and access works.
    However, in the file explorer each user has the inaccessible directories of the other user displayed too (both with the same name).

    If I make the mount and automount user units, then mounting does not work (Operation not permitted).

  11. Hello, the resume is:

    in /etc/fstab add:

    //192.168.0.xxx/folder/folder2/folder3 /mnt/yourlocalfolder cifs x-systemd.automount,_netdev,noserverino,noperm,credentials=/etc/win-credentials,file_mode=0755,dir_mode=0755 0 0

    save and create /etc/win-credentials with:

    username=share-user
    password=share-password
    domain=WORKGROUP

    run:

    systemctl daemon-reload

    and

    systemctl list-unit-files –type=automount

    (at this point, you can see yout autogenerated automount unit)

    start automount:

    systemctl start mnt-yourlocalfolder.automount

    see if it´s ok:

    mount | grep systemd

    return like this:

    systemd-1 on /mnt/yourlocalshare type autofs (rw,relatime,fd=36,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=2358619)

    on the first access, systemd will mount according to /etc/fstab .. after the first acces, you can run:

    mount | grep yourlocalshare | grep cifs

    //192.168.0.xxx/folder/folder2/folder3 on /mnt/yourlocalshare type cifs (rw,relatime,vers=3.1.1,cache=strict,username=xxxxx,domain=WORKGROUP,uid=0,noforceuid,gid=0,noforcegid,addr=192.168.0.xxx,file_mode=0755,dir_mode=0755,soft,nounix,mapposix,noperm,rsize=4194304,wsize=4194304,bsize=1048576,echo_interval=60,actimeo=1,x-systemd.automount,_netdev)

    That´s all !!

  12. Hi,

    Thank you for the tutorial ! Great job !
    I am having a problem: if the user does not use the shares It hangs and even ls -l \share works. I have to restart the service manually.

    My mount file:

    [Unit]
    Description=cifs mount script
    Requires=network-online.target
    After=network-online.service
    [Mount]
    What=///Data
    Where=/shares/share_name
    Options=username=,password=,rw,uid=89775,gid=506,dir_mode=0750,
    Type=cifs
    [Install]
    WantedBy=multi-user.target

    My automount file:

    [Unit]
    Description=cifs automount script
    Requires=network-online.target
    After=network-online.service
    [Automount]
    Where=/shares/share_name
    TimeoutIdleSec=10
    [Install]
    WantedBy=multi-user.target

Leave a Reply