File System Management with autofs: Setup Guide #RHCSA | Linux (3) #LinuxAdmin

Karthi SKarthi S
3 min read

In this blog post, we’ll delve into autofs, a dynamic filesystem mounting service in Red Hat Linux. This service automates the mounting and unmounting of file systems on demand, optimizing system performance and simplifying administration.

What is autofs?

Autofs is a service that automates the mounting and unmounting of file systems. Unlike traditional methods that require static entries in the /etc/fstab file, autofs dynamically mounts file systems as needed. This ensures that network shares or external devices are only mounted when required, thus enhancing performance and reducing administrative overhead.

Lesson Setup: Configuring the NFS Server

To follow along with this guide, you'll need two systems: Server One (the client) and Server Two (the NFS server). If you've set up NFS before, you can skip this part.

Step 1: Install NFS Packages on Server Two

First, install the necessary NFS packages:

sudo dnf install -y nfs-utils

Step 2: Start NFS and RPC Bind Services

Enable and start the NFS server and RPC Bind services:

sudo systemctl enable nfs-server rpcbind --now

Step 3: Create a Shared Directory

Create the directory that will be shared:

sudo mkdir -p /nfs/exports/my_share

Step 4: Set Directory Permissions

Set the permissions to allow full access for this exercise:

sudo chmod 777 /nfs/exports/my_share

Step 5: Export the Directory

Configure NFS to export the directory:

echo "/nfs/exports/my_share *(rw)" | sudo tee /etc/exports

Apply the export configuration:

sudo exportfs -ra

Step 6: Configure the Firewall

Allow NFS services through the firewall:

sudo firewall-cmd --add-service=nfs --permanent
sudo firewall-cmd --add-service=rpc-bind --permanent
sudo firewall-cmd --add-service=mountd --permanent
sudo firewall-cmd --reload

Configuring autofs

Step 1: Install the autofs Package

On Server One, install the autofs package:

sudo dnf install -y autofs

Step 2: Configure the auto.master File

The auto.master file defines mount points and their corresponding map files. Add an entry to this file to specify where autofs will look for the map file:

echo '/nfs/autofs /etc/auto.my_share' | sudo tee -a /etc/auto.master

Step 3: Create the autofs Map File

Create the map file auto.my_share that specifies the NFS share location. Replace <server 2 IP> with the actual IP address of Server Two:

echo 'my_share <server 2 IP>:/nfs/exports/my_share' | sudo tee -a /etc/auto.my_share

This configuration will mount the NFS share to /nfs/autofs/my_share when accessed.

Step 4: Start the autofs Service

Enable and start the autofs service:

sudo systemctl enable autofs --now

Testing autofs Configuration

With autofs configured, it’s time to test if it works correctly.

Step 1: Verify Initial Mount Status

Check that the NFS share is not mounted initially:

df -h | grep '/nfs/autofs/my_share'

No output should be returned, indicating that the share is not currently mounted.

Step 2: Access the Mounted Directory

Navigate to the mount point to trigger autofs:

cd /nfs/autofs/my_share

Check if the remote share is now mounted:

df -h | grep '/nfs/autofs/my_share'

The output should show:

<server 2 IP>:/nfs/exports/my_share   20G  12G  8.0G  60% /nfs/autofs/my_share

Step 3: Verify Files in the Directory

List the files in the mounted directory to ensure proper access:

ls -lh

You should see a list of files like:

-rw-r--r--. 1 user1 user1 120K Sep 15 15:45 PROJECT_PLAN.pdf
-rw-r--r--. 1 user1 user1 3.2K Sep 15 15:47 TASKS.txt
-rw-r--r--. 1 user1 user1 850K Sep 15 15:50 DATA_REPORT.xls
-rw-r--r--. 1 user1 user1 45K Sep 15 15:55 MEETING_NOTES.docx

Step 4: Remove a File

Confirm you can delete files from the share:

rm -v TASKS.txt

The output will show:

removed 'TASKS.txt'

Conclusion

Autofs simplifies file system management by dynamically handling mounts and unmounts. Configuring autofs and NFS helps you efficiently manage file systems and optimize system performance. This setup is ideal for environments where file systems are accessed intermittently or need dynamic management. With these steps, you should have a robust configuration ready for practical use in your RHCSA environment.

0
Subscribe to my newsletter

Read articles from Karthi S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Karthi S
Karthi S

Evolving around Devops and multi-cloud. Cloud - AWS/Azure Container & orchestration -> Docker & Kubernetes Automating -> golang/shell scripting Certified AZ900 | AZ104 | RHSCA