Setting up AutoFS in Linux on RHEL7 or CentOS7

autofs in Linux

Configuring autofs in Linux is a straightforward task. This article will guide you through the process of setting up and enabling the autofs service.

Table of Contents

Introduction

As part of this process, we will discuss the difference between static and dynamic mount points and use a previous article “Create and Export an NFS server in Linux” as a reference and for demonstrative purposes.

Young woman in a data center

Image by Christina Morillo from Pexels

Pre-Installation Checks

This tutorial assumes you have a RHEL7 or CentOS7 machine running with root or sudo privileges to become root. Also, it helps to already have an NFS server and client setup with perhaps one or two exported shares.

Checklist items

Ensure that you have the following setup (below) before proceeding to the autofs configuration section.
    1. Check if the nfs-utils package is installed on both the NFS server and client instances: rpm -qa | grep nfs-utils . If nfs-utils is not installed, run the following command to install it: yum -y install nfs-utils

    2. Check if the nfs.service is started and enabled to autostart on reboot (only on the server instance, not required for the client instance):  sudo systemctl status nfs.service to check if NFS service is running. If not,  run the following to start it: sudo systemctl start nfs.service. The following command will ensure the NFS service is enabled to autostart on reboot:  sudo systemctl enable nfs.service

    3. Check if the right ports are allowed for NFS on your local firewall (firewalld): sudo firewall-cmd --list-all if not, run the following commands (below) to allow the right firewall ports for NFS.

				
					$ sudo firewall-cmd --permanent --add-service=rpc-bind
$ sudo firewall-cmd --permanent --add-service=mountd
$ sudo firewall-cmd --permanent --add-service=nfs
$ sudo firewall-cmd --permanent --add-port=2049/tcp
$ sudo firewall-cmd --permanent --add-port=2049/udp
$ sudo firewall-cmd --reload
				
			

If you’re having difficulties with any of the instructions on the checklist (above), revisit the “Create and Export an NFS server in Linux” article for further detailed assistance.

Static vs. Dynamic NFS mounts

Simply put, static NFS shares (as the term suggests) are always available. Typically, entries are made in the /etc/fstab file to ensure the NFS share is mounted automatically after rebooting a machine. However, this is not considered best practice if bandwidth conservation and optimum performance are your organizational goals.

This is why dynamic NFS mounts are the industry standard. It allows NFS shares to be mounted automatically on an as-needed basis. The mount point or NFS share expires after a set period of time of not being accessed. This not only conserves bandwidth, it also improves overall performance when compared to static NFS mounts.

Setting Up AutoFS Mounts

We are now ready to proceed with the autofs configuration. We will begin with the server-side and end with the client-side instance.

Server-side configuration

SSH into your server instance. VM1 is our server instance for this demonstration. Then, less or cat the contents of the /etc/exports file (shown in the image below). This is where we expect to find the NFS shares currently being exported to our client instance.

AutoFS in Linux - Display /etc/exports file

Photo by admingeek from Infotechys

As you can see (above) the /nfs_share mount is being exported from this server instance to the client instance. For this demonstration, we will create a new share called /nfs_auto_share and export that share to the client instance.

Creating an NFS share using SSM

We will use the SSM utility to create our new nfs share. The new logical volume called lv_nfs_auto_share will be 10GB in size. Run the following command (below):

				
					[admin@vm1 ~]$ sudo ssm create -s 10GB -n lv_nfs_auto_share --fstype xfs -p vg00 /dev/vda2 /nfs_auto_share
[sudo] password for admin: 
Logical volume "lv_nfs_auto_share" created.
 Logical volume "lv_nfs_auto_share" created.
meta-data=/dev/vg00/lv_nfs_auto_share isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Directory '/nfs_auto_share' does not exist! Create (Y/n/q) ? Y
				
			

In the image shown below, we can see the output of the df command and verify that our new share has been mounted. Next, we will ensure the /nfs_auto_share mount point persists on our server instance.

AutoFS in Linux - New NFS share

Photo by admingeek from Infotechys

Using your preferred text editor, open the /etc/fstab file and add the following entry (circled in the image below) to the file. Then, save and exit the file.

AutoFS in Linux - New fstab entry

Photo by admingeek from Infotechys

Finally, we will complete the server-side configuration, by once again opening the /etc/exports file and adding the share as a new entry to the file.

				
					[admin@vm1 ~]$ cat /etc/exports
/nfs_auto_share 192.168.1.197(rw,sync)
				
			

Finally, we will export the new share to our client instance.

				
					[admin@vm1 ~]$ sudo exportfs -va
[sudo] password for admin:
exporting 192.168.1.197:/nfs_auto_share
				
			

Client-side configuration

SSH into your client instance. VM2 is our client instance for this demonstration. In the image below, the output of the df command shows our static mount point /nfs_share and does not yet show the /nfs_auto_share mount we’ve just exported. The /nfs_share is static because it has an entry in our /etc/fstab file and remains persistent and visible even though we currently have no use for it.
AutoFS in Linux - df output on client instance

Photo by admingeek from Infotechys

Now let’s configure AutoFS on our client instance to enable the /nfs_auto_share.

Pre-configuration Checklist

Before we begin, let’s ensure the AutoFS service is installed, enabled, and running on our client instance.

Starting and Enabling AutoFS

First, let’s run the following command (below) to check if the AutoFS service is running on our machine. If AutoFS is not installed, simply install it using yum: sudo yum -y install autofs

				
					[admin@vm2 ~]$ sudo systemctl status autofs
● autofs.service - Automounts filesystems on demand
Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled; vendor preset: disabled)
Active: inactive (dead)
				
			

As you can see, AutoFS is not running on our machine and we need to start it. While we’re at it, let’s enable it as well so it persists on our machine. We can use the semi-colon ; or double ampersand && to run multiple commands at once on the command-line (CLI).

				
					[admin@vm2 ~]$ sudo systemctl start autofs ; sudo systemctl enable autofs
				
			

Now that AutoFS is running and enabled on our client instance, we can proceed to the next step and begin the configuration process to automount the /nfs_auto_share filesystem.

				
					[admin@vm2 ~]$ sudo systemctl status autofs
● autofs.service - Automounts filesystems on demand
Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-02-21 13:26:56 EST; 4s ago
Main PID: 2725 (automount)
CGroup: /system.slice/autofs.service
└─2725 /usr/sbin/automount --systemd-service --dont-check-daemon

Feb 21 13:26:56 vm2.dev.infotechys.com systemd[1]: Starting Automounts filesystems on demand...
Feb 21 13:26:56 vm2.dev.infotechys.com systemd[1]: Started Automounts filesystems on demand.
				
			

Automount the NFS share

We are going to create two separate files as part of this procedure. One file (nfs_auto_share.autofs) will reside under the /etc/auto.master.d/ sub-directory and the other file (auto.nfs_auto_share) will reside under /etc.

Using your preferred text editor, cd into /etc/auto.master.d and create a file called nfs_auto_share.autofs.

Add the following entry (below) to the file. Then, save and exit the file.

				
					/- /etc/auto.nfs_auto_share
				
			

The entry (above) points the AutoFS service to the location of the file (/etc/auto.nfs_auto_share) where the parameters of the /nfs_auto_share filesystem or mount point is defined.

Next, cd into /etc and create a file called auto.nfs_auto_share. Add the following entry (below) to that file.

				
					/nfs_auto_share -rw,intr 192.168.1.196:/nfs_auto_share
				
			

The entry (above) defines the new share (nfs_auto_share) we want dynamically mounted on our client machine. We’ve also specified that we want it mounted with read and write (rw) permissions from our NFS server instance (NFS Server IP Address is 192.168.1.196). The interrupt (intr) mount option is a default setting and is used when we do not expect to damage critical data by manually interrupting an NFS request.

Finally, restart the AutoFS service so it can read the newly added configuration files.

				
					[admin@vm2 etc]$ sudo systemctl restart autofs && sudo systemctl status autofs
● autofs.service - Automounts filesystems on demand
Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-02-21 14:15:25 EST; 3s ago
Main PID: 2882 (automount)
CGroup: /system.slice/autofs.service
└─2882 /usr/sbin/automount --systemd-service --dont-check-daemon
Feb 21 14:15:25 vm2.dev.infotechys.com systemd[1]: Starting Automounts filesystems on demand...
Feb 21 14:15:25 vm2.dev.infotechys.com systemd[1]: Started Automounts filesystems on demand.
				
			

Verifying or Testing the NFS share

You’ll notice that even after restarting the AutoFS service, the new /nfs_auto_share is still not visible (shown in the image below).

AutoFS in Linux - df output on client instance

Photo by admingeek from Infotechys

This is to be expected as we are not currently using the share. However, we can verify that it exists by listing it: ls -l /nfs_auto_share . We can also cd into it and create a test file: touch test1.txt. Executing any one of these options will activate the /nfs_auto_share and make it visible using the df command (shown in the image below).

NOTE: If you cannot create a file under /nfs_auto_share, it is because of your ownership/permission settings. SSH into your server instance and change the permission setting using the following command (below). This will allow world read, write, and execute permissions on /nfs_auto_share.

				
					


[admin@vm1 ~]$ sudo chmod -R 1777 /nfs_auto_share
				
			

Now, a listing of the /nfs_auto_share directory shows its contents (below).

AutoFS in Linux - nfs_auto_share df output

Photo by admingeek from Infotechys

Conclusion

Congratulations! You’ve successfully completed AutoFS configuration on your Linux machine. As part of this tutorial, we reviewed the difference between static and dynamic NFS mounts and configured AutoFS to mount a new share–created and exported from our NFS server instance.

Was this article helpful to you? If so, let us know in the comment section below. We’d love to hear from you!

Related Posts

Leave a Reply

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