Skip to main content
In this lesson, you’ll learn how to optimize Linux system performance by mounting filesystems only when they’re accessed. This technique, known as on-demand mounting, reduces unnecessary network traffic—especially for remote filesystems like NFS—and keeps your server load light.

Why On-Demand Mounting?

  • Defers mounting until a path is accessed
  • Saves network bandwidth for NFS and CIFS shares
  • Automatically unmounts after inactivity
  • Simplifies management of rarely used mount points
Consider a rarely used directory such as /backups. With on-demand mounting, nothing is mounted at boot or during idle periods. As soon as an application or user reads or writes /backups, the OS mounts the remote share automatically:
The image illustrates a diagram of "On Demand Mounting," showing multiple "/backups/" folders connected to a central fileserver.

Prerequisites

  • A Linux distribution with dnf or yum (RHEL, CentOS, Fedora)
  • Root or sudo privileges
  • Basic knowledge of NFS or other network filesystems

1. Install and Enable AutoFS

The most common tool for on-demand mounts is autofs. Install and start the service:
If you’re using a different package manager such as yum or apt, adjust the install command accordingly.

2. Set Up a Simple NFS Server

To demonstrate, we’ll configure a basic NFS export on the local machine.
  1. Install NFS utilities:
  2. Start and enable the NFS server:
  3. Add an export in /etc/exports:
  4. Reload the server to apply changes:

3. Configure AutoFS Maps

AutoFS uses a master map and one or more map files to define mounts.

3.1 /etc/auto.master

Open the master configuration:
Add at the end:
  • /shares: Parent directory that AutoFS will create
  • /etc/auto.shares: Map file listing the mounts
  • —timeout=300: Unmount after 300 seconds of inactivity

3.2 /etc/auto.shares

Edit the map file to define individual mounts:
Reload AutoFS:

4. Test the On-Demand Mount

  1. Verify that /shares is empty:
  2. Access the share to trigger the mount:
  3. After 5 minutes of inactivity, AutoFS will unmount the share automatically.

5. Direct Mount Points (No Common Parent)

If you prefer absolute mount paths instead of a shared parent:
  1. Update /etc/auto.master:
    Replace the /shares entry with:
  2. Modify /etc/auto.shares:
  3. Reload AutoFS:
Now you can access directly:

Next Steps

You’ve successfully configured on-demand mounting with AutoFS and NFS. In the next lesson, we’ll explore advanced unmounting controls and troubleshooting techniques.

Watch Video