How to Use autofs for On‑Demand Mounts in Linux
This guide explains why static entries in /etc/fstab can waste resources, introduces the autofs daemon for dynamic mounting, and provides step‑by‑step commands to install, configure, and test autofs so that filesystems are mounted only when accessed.
Static mounting via /etc/fstab automatically mounts all listed remote resources at boot, which can overload network bandwidth and server hardware when many shares are defined but seldom used. Manually running mount each time avoids the waste but is inconvenient.
What autofs Does
The autofs daemon watches for access to a path that is not yet mounted; when a user attempts to open a file or change directory, autofs automatically mounts the required filesystem, saving bandwidth and hardware resources.
Install autofs
[root@localhost ~]# yum install autofs
Loaded plugins: langpacks, product-id, subscription-manager
......
Running transaction
Installing : hesiod-3.2.1-3.el7.x86_64 1/2
Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2
Verifying : hesiod-3.2.1-3.el7.x86_64 1/2
Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2
Installed:
autofs.x86_64 1:5.0.7-40.el7
Dependency Installed:
hesiod.x86_64 0:3.2.1-3.el7
Complete!Configure the Master Map
To keep the master map concise, use the mount‑point sub‑map format. The mount point is the directory under which autofs will look for further maps.
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
/media /etc/iso.misc
/misc /etc/auto.misc
# NOTE: mounts done from a hosts map will be mounted with the "nosuid" and "nodev" options unless the "suid" and "dev" options are explicitly given.
/net -hosts
# Include /etc/auto.master.d/*.autofs
+dir:/etc/auto.master.d
# Include central master map if it can be found using nsswitch sources.
# Note that if there are entries for /net or /misc (as above) in the included master map any keys that are the same will not be seen as the first read key seen takes precedence.
+auto.masterCreate a Sub‑map for the CD-ROM
Define a sub‑map (any name ending with .misc) that describes each device under the chosen mount directory.
# /etc/iso.misc
iso -fstype=iso9660,ro,nosuid,nodev :/dev/cdromStart and Enable autofs
[root@localhost ~]# systemctl start autofs
[root@localhost ~]# systemctl enable autofs
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'Test the Dynamic Mount
Before accessing the CD, df -h shows no entry for /dev/cdrom. After navigating to /media/iso, autofs mounts the device automatically.
# df -h (before)
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
... (other filesystems)
# cd /media
# ls
# cd iso
# ls -l
total 812
dr-xr-xr-x. 4 root root 2048 May 7 2017 addons
... (list of CD contents)
# df -h (after)
Filesystem Size Used Avail Use% Mounted on
/dev/cdrom 3.5G 3.5G 0 100% /media/iso
... (other filesystems)Thus, autofs provides on‑demand mounting, eliminating unnecessary resource consumption while keeping the workflow seamless for users.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
