
In this article, we will review the top 50 Linux commands every Linux Sysadmin should know. Junior-level sysadmins and Linux enthusiasts are familiar with all
Learn how to install and configure AutoFS on RHEL 9 to automate the mounting of NFS and SMB shares. This step-by-step guide covers installation, configuration, troubleshooting, and advanced features for efficient file system management.
In the world of Linux, automating network file system (NFS) mounts is essential for optimizing workflows, improving performance, and reducing the risk of manual errors. One powerful tool that can help system administrators accomplish this task is AutoFS. With AutoFS, mounting and unmounting network filesystems is automated, enabling on-demand access to shared resources. In this guide, we will walk you through the process of installing and configuring AutoFS on RHEL 9.
AutoFS (Automount File System) is a Linux service that automates the mounting and unmounting of filesystems on demand. When a user or application tries to access a directory that is not currently mounted, AutoFS will automatically mount the corresponding filesystem. This improves system performance by reducing the number of unnecessary mounted file systems and helps avoid manual configuration errors.
In RHEL 9, AutoFS works seamlessly with various types of network file systems, including NFS, SMB, and even local file systems. The tool reduces the administrative burden and ensures that file systems are available as needed.
Here are several reasons why you might want to use AutoFS on RHEL 9:
|
|
|
|
Before you proceed with the installation and configuration, ensure that:
|
|
|
|
AutoFS is not installed by default in RHEL 9, so you’ll need to install it manually. Here’s how:
|
sudo dnf install autofs -y
|
systemctl status autofs
|
sudo systemctl enable --now autofs
AutoFS uses configuration files to determine how file systems are mounted and unmounted. The key configuration files include:
|
|
|
Let’s now explore how to configure AutoFS for various scenarios.
🔄 Define Mount Points in /etc/auto.master |
The auto.master file is the central configuration file for AutoFS. Here, you will define the mount points and the corresponding map files that will be used by AutoFS. For instance, if you’re mounting NFS shares, you might want to define a directory to mount these shares dynamically.
Open the auto.master
file:
sudo vim /etc/auto.master
Add an entry for the mount point and specify the corresponding map file (for example, auto.nfs
):
# /etc/auto.master
/mnt/nfs /etc/auto.nfs
This indicates that the NFS shares will be mounted under /mnt/nfs
and the auto.nfs
file will define the specifics of how these shares are mounted.
🔄 Create the Map File (e.g., /etc/auto.nfs) |
Now, you need to define the NFS share locations and options in the map file (auto.nfs
).
sudo vim /etc/auto.nfs
Add lines to define the NFS share. For example:
# /etc/auto.nfs
share1 -fstype=nfs,rw,soft 192.168.1.100:/export/share1
share2 -fstype=nfs,rw,soft 192.168.1.101:/export/share2
This configuration tells autofs
to mount the NFS shares only when they are accessed. Specifically:
|
|
🔄 Restart AutoFS |
After making changes to the configuration files, restart the AutoFS service to apply them:
sudo systemctl restart autofs
🔄 Verify the Mounts |
Once the configuration is complete and AutoFS has been restarted, you can verify the mounting by accessing the directories:
ls /mnt/nfs/share1
ls /mnt/nfs/share2
AutoFS should mount the NFS shares on demand when you try to access these directories.
AutoFS also works with SMB (Server Message Block) shares. To configure AutoFS for SMB, follow similar steps as for NFS.
🔄 Define SMB Mount Points in /etc/auto.master |
In auto.master
, you might add a line like:
# /etc/auto.master
/mnt/smb /etc/auto.smb
🔄 Create the SMB Map File ( |
In this file, define your SMB share mappings. Example:
# /etc/auto.smb
share1 -fstype=cifs,credentials=/etc/samba/credentials ://192.168.1.100/share1
share2 -fstype=cifs,credentials=/etc/samba/credentials ://192.168.1.101/share2
In this example, the credentials file will hold the necessary credentials for accessing the SMB shares.
🔹Explanation |
|
|
|
|
🔄 Update the |
Edit /etc/auto.master
(or /etc/auto.master.d/*.autofs
if using systemd style) and add:
/mnt/smb /etc/auto.smb --timeout=60
This tells autofs
to monitor /mnt/smb
and use /etc/auto.smb
for the share definitions. Shares will be mounted on-demand when accessed under /mnt/smb/share1
, /mnt/smb/share2
, etc.
🔄 Create the Credentials File |
To avoid embedding your SMB credentials in the map file directly, create a separate, secure credentials file:
sudo vim /etc/samba/credentials
Add the following content:
username=your_username
password=your_password
Optional: If the SMB server is using a workgroup or domain, add:
domain=WORKGROUP
🔄 Secure the Credentials File |
Ensure only root can read the credentials file:
sudo chmod 600 /etc/samba/credentials
🔄 Restart AutoFS |
After updating the configuration files, restart the autofs
service:
sudo systemctl restart autofs
🔄 Verify the Mounts |
Try accessing the SMB shares. AutoFS should automatically mount them when accessed:
ls /mnt/smb/share1
ls /mnt/smb/share2
You can also verify active mounts with:
mount | grep cifs
Or:
df -h | grep smb
🔹Recap |
Step | Description |
1 | Define SMB shares in /etc/auto.smb |
2 | Link it in /etc/auto.master |
3 | Create /etc/samba/credentials securely |
4 | Restart autofs service |
5 | Access shares via /mnt/smb/share1 , etc. |
🔄 Mounting with Timeouts |
AutoFS allows you to specify a timeout period after which an idle file system is unmounted. This is useful for conserving system resources. For example, you can add the timeout option in the map file:
share1 -fstype=nfs,rw,soft,timeout=60 192.168.1.100:/export/share1
This unmounts the NFS share after 60 seconds of inactivity.
🔄 Using LDAP for AutoFS Configuration |
AutoFS can be configured to use LDAP for centralized management of mount points. You can store the AutoFS maps in an LDAP server to provide network-wide access and management.
Here are a few common troubleshooting steps:
|
journalctl -u autofs
|
|
Configuring AutoFS on RHEL 9 is an efficient way to manage file systems, particularly network file systems such as NFS and SMB. By automating the mounting and unmounting processes, AutoFS saves time, enhances system performance, and reduces manual errors. Following this guide, you can easily install and configure AutoFS to manage your file systems with minimal manual intervention.
Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.
In this article, we will review the top 50 Linux commands every Linux Sysadmin should know. Junior-level sysadmins and Linux enthusiasts are familiar with all
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
NFS or Network File System is a commonly known method for network file sharing on Linux hosts. We will create and export an NFS server.