CentOS/RedHat 7/8 -Automounting CIFS File System

When we manual mount the NFS and SMB file system, it will not available persistently. The mount point will be lost once you reboot the system. To mount the remote file system (NFS and SMB) persistently, you can configure the system automatically mount the remote file system edit /etc/fstab file.

As an alternative to use /etc/fstab, you can configure automount to mount the share automatically. Automount can be used for SMB as well as NFS mount. Automount is implemented by the autofs servie that take care of mounting a share when an attempt is made or on demand, and it will automatically unmount the share when no longer in used. One of the benefit of automounter, user do not need to have root privileges to run the mount and umount command.

Configuring an automount is a multiple step process.

First step, you need to install the autofs package by enter following command

yum install -y autofs

Second step, you need to edit the master configuration file /etc/auto.master or add a master map file (file must have an extension of .autofs) to /etc/auto.master.d directory. This configuration files will define the base directory used for mount point and identify the mapping file used for creating the automounts.

In this example, i going to add a master map file

vim /etc/auto.master.d/avms3.autofs

Syntax of master map file

Mount Point Map Files

Mount Point – Location or folder where to mount the autofs file system

Map File – Name of the map source to use for mounting

Indirect Map

An indirect map uses a substitution value of a key to establish the association between a mount point on the client and a directory on the server. Substitution value is the pathname to create virtual directory

/cifs /etc/auto.master.d/indirect_auto.cifs

note: /cfs directory will auto create once autofs service is start

Now you have to add/edit the mapping file that you specified on above step. Each mapping file consist of the mount point, mount options, and the source location to mount

vi /etc/auto.master.d/indirect_auto.cifs

Sample configuration of mapping file

data1 -fstype=cifs,rw,credentials=/etc/auto.master.d/sdrc.txt ://30.30.9.245/demosdplt

data1 -> mount point – will auto create and remove by autofs service

-fstype -> is the file system type, example: nfs, cifs,xfs and etc

credentials -> specific the credential file location. Note: in this example, i will put cifs username and password in this credential file.

Example of credentials file (/etc/auto.master.d/sdrc.txt)

username=user

password=abc123

Source share location -> in this example is the cifs share path

Indirect Wildcard map

If you have a directory with subdirectories, automounter can be configured to access any one of the those directories using a single mapping file.

Sample configuration of mapping file

* -fstype=cifs,rw,credentials=/etc/auto.master.d/sdrc.txt ://30.30.9.245/demosdplt/&

* -> asterisk character is the mount point

& -> subdirectory if the source location

Direct Map

Direct maps are used to map the share to an existing absolute path mount point

/- /etc/auto.master.d/direct_auto.cifs

Sample configuration of mapping file

/avms3 -fstype=cifs,rw,credentials=/etc/auto.master.d/sdrc.txt ://30.30.9.245/demosdplt

avms3 -> absolute path. If avms3 is an existing directory, it will mount on it. Else, you have to create the directory to allow autofs to mount share location

-fstype -> is the file system type, example: nfs, cifs,xfs and etc

credentials -> specific the credential file location. Note: in this example, i will put cifs username and password in this credential file.

Example of credentials file (/etc/auto.master.d/sdrc.txt)

username=user

password=abc123

Source share location -> in this example is the cifs share path

Last Steps – Start and enable the automounter service.

systemctl start autofs

systemctl enable –now autofs

Test AutoFS

A) Indirect map

i. Verify cifs directory is auto created by autofs

ii. verify the share folder is link to data1

B) Direct map

i. Verify /avms3 directory is automount cifs share

Debugging AutoFS

Enable the debug log by adding LOGGING=”debug” in /etc/sysconfig/autofs

Debugging log will show at /var/log/messages

Leave a Reply

Your email address will not be published. Required fields are marked *