Logo Microtux

MicroTux

Automount cifs met systemd

26-06-2016

We have a system running with a working samba share.

How do you make an automount?

Assumptions:

The samba server is called servix
You are root on your system
In the example the username is ron
The address of servix is known on your system.
Otherwise, create it in the file /etc/hosts

vi /etc/hosts

type there: [ip address of servix]       servix
e.g. 192.168.1.150    service

To request a list of available shares on the server:
type in a terminal: smbclient -L servix -U%

Start a terminal:
We create a folder share in /home/ron/

cd /home/ron

mkdir /home/ron/share

Create a file .credentials in the root folder

cd /root

vi .credentials
username=ron
password=mysecretpassword
Save the above file and exit the editor
chmod 600 .credentials

Now we are going to create 2 files in the /etc/systemd/system folder

File named: home-ron-share.automount

cd /etc/systemd/system

vi home-ron-share.automount

[Unit]

DefaultDependicies=no

After=remote-fs-pre.target

Wants=remote-fs-pre.target

Conflicts=umount.target

Before=umount.target

[Automount]

Where=/home/ron/share

DirectoryMode=0775

TimeIdleSec=0

[Install]

WantedBy=remote-fs.target

Save the above file and exit the editor
File named: home-ron-share.mount

vi home-ron-share.mount

[Unit]

Description=cifs mount script

Requires=network-online.target

After=network-online.service

[Mount]

What=//servix/share

Where=/home/ron/share

Options=credentials=/root/.credentials,workgroup=ARMORICA,rw

Type=cifs

[Install]

WantedBy=multi-user.target

Save the above file and exit the editor

chmod 644 home-ron-share.*

For other mounts, adjust the share name for the desired share name, and for another user, of course, adjust the user name.

The folder must have been created there! (You can of course enter any place in your system where you want to mount the folder under Where.)

Activate the systemd service:

systemctl enable home-ron-share.automount

A symlink is created.

Start the service: systemctl starts home-ron-share.automount

Owner of the files: root:root , Filemode: 644

If you have any problems, check the log file! Possibly a firewall problem, SELinux or server not in hosts file!