Mounting CIFS Shares on CentOS 7 - rharmonson/richtech GitHub Wiki

Mounting CIFS Shares on CentOS 7

Package

$ sudo yum install cifs-utils

Results

================================================================================
 Package                 Arch        Version                 Repository    Size
================================================================================
Installing:
 cifs-utils              x86_64      6.2-7.el7               base          84 k
Installing for dependencies:
 cups-libs               x86_64      1:1.6.3-22.el7          base         355 k
 keyutils                x86_64      1.5.8-3.el7             base          54 k
 libldb                  x86_64      1.1.25-1.el7_2          updates      125 k
 libtalloc               x86_64      2.1.5-1.el7_2           updates       34 k
 libtdb                  x86_64      1.3.8-1.el7_2           updates       45 k
 libtevent               x86_64      0.9.26-1.el7_2.1        updates       33 k
 libwbclient             x86_64      4.2.10-7.el7_2          updates       97 k
 pytalloc                x86_64      2.1.5-1.el7_2           updates       14 k
 samba-client-libs       x86_64      4.2.10-7.el7_2          updates      4.3 M
 samba-common            noarch      4.2.10-7.el7_2          updates      273 k
 samba-common-libs       x86_64      4.2.10-7.el7_2          updates      157 k
 samba-common-tools      x86_64      4.2.10-7.el7_2          updates      445 k
 samba-libs              x86_64      4.2.10-7.el7_2          updates      261 k

Transaction Summary
================================================================================
Install  1 Package (+13 Dependent packages)

Total download size: 6.2 M
Installed size: 21 M

mount.cifs

sudo mount.cifs //hostname-or-ip_address/sharename /mnt/testmnt -o ro,guest

fstab

Does not mount at boot even if using _netdev option. See section below titled systemd. //hostname-or-ip_address/music /mnt/testmnt cifs ro,guest 0 0

systemd

Use a mount or automount unit. Must use a naming convention for the unit file that corresponds to the file system, i.e. /mnt/dir/dir2 = mnt-dir-dir2.mount.

$ cat /usr/lib/systemd/system/mnt-nas.mount
[Unit]
  Description=NAS Mount Script
  Requires=network.target
  After=network.service

[Mount]
  What=//192.168.2.1/sharename
  Where=/mnt/mntdir
  Options=guest,ro
  Type=cifs

[Install]
  WantedBy=multi-user.target

Mounting with a username and password with read-write privileges.

$ sudo cat /usr/lib/systemd/system/mnt-media.mount 
[Unit]
  Description=Media Mount Script
  Requires=network.target
  After=network.service

[Mount]
  What=//192.168.2.1/media
  Where=/mnt/media
  Options=username=User1,password=Password1,rw
  Type=cifs

[Install]
  WantedBy=multi-user.target

Instead of storing the user credentials in the systemd unit file, replace the username and password options with credentials and create a credential file.

[Mount]
  What=//192.168.2.1/media
  Where=/mnt/media
  Options=credentials=/root/.smbcredentials,rw
  Type=cifs

then

touch /root/.smbcredentials
chmod 600 /root/.smbcredentials
vi /root/.smbcredentials

Enter

username=User1
password=Password1

Do not forget to enable the new systemd unit.

systemctl enable mnt-media.mount

Update systemd unit after edits.

systemctl daemon-reload