Configure autofs

Configure autofs

CIS126RH | RHEL System Administration 1
Mesa Community College

autofs — the automounter — mounts filesystems on demand the moment they are accessed and unmounts them automatically after a configurable idle timeout. This eliminates the need to keep NFS shares permanently mounted when they may only be needed occasionally, prevents boot failures when NFS servers are temporarily unavailable, and scales to large environments where hundreds of home directories would be impractical to list in /etc/fstab. autofs configuration is a distinct RHCSA exam objective and is tested with specific scenario tasks.

Learning Objectives

  1. Explain how autofs works — Describe the autofs on-demand mounting model, the role of the kernel automounter, and why autofs is preferred over fstab for certain use cases
  2. Install and configure the autofs service — Install the autofs package, understand the master map and map file structure, and enable the service
  3. Configure indirect maps — Write master map and indirect map file entries to automount NFS shares as subdirectories of a base directory
  4. Configure the wildcard home directory pattern — Use the * and & substitution in a map file to automount any user's home directory on demand

How autofs Works

autofs intercepts filesystem access at directories it manages and mounts the target on demand — the user never sees a difference from a permanently mounted filesystem, except that the mount only exists while in use.

  • autofs registers itself with the kernel as a filesystem handler for configured directories
  • When a process accesses /shares/data, the kernel asks autofs to handle it
  • autofs looks up data in its map file, finds the NFS target, and mounts it
  • The process sees the mounted directory contents — no mount command required
  • After a configurable idle timeout (default 300 seconds), autofs unmounts the share
  • If the NFS server is unavailable, autofs returns "No such file or directory" rather than hanging
autofs vs /etc/fstab

Use /etc/fstab when a share must always be mounted and the server is reliable. Use autofs when: mounts are needed only occasionally, the server may be temporarily unavailable, or many mounts (e.g. user home directories) would make fstab unmanageable.

Installing autofs

The autofs package provides the daemon, configuration files, and systemd service unit.

# Install the autofs package
$ sudo dnf install -y autofs

# NFS support also requires nfs-utils
$ sudo dnf install -y nfs-utils

# Enable and start the autofs service
$ sudo systemctl enable --now autofs

# Confirm autofs is running
$ systemctl status autofs
● autofs.service - Automounts filesystems on demand
     Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled)
     Active: active (running) since ...

# View the autofs configuration files installed
$ rpm -ql autofs | grep etc
/etc/auto.master
/etc/auto.misc
/etc/auto.net
/etc/auto.smb
/etc/autofs.conf
/etc/autofs_ldap_auth.conf
Reload vs restart after config changes

After editing map files, use sudo systemctl reload autofs — reload re-reads all map files without interrupting existing mounts. Use restart only if the daemon itself needs to be restarted.

The autofs Configuration Model

autofs uses two levels of configuration: the master map defines which directories autofs manages; map files define what gets mounted at each key within those directories.

# /etc/auto.master — the master map
# Format: BASE_DIR  MAP_FILE  [OPTIONS]
/misc     /etc/auto.misc
/mnt      /etc/auto.mnt     --timeout=60
/home     /etc/auto.home    --timeout=300
/-        /etc/auto.direct

# A map file — /etc/auto.mnt
# Format: KEY  [-OPTIONS]  LOCATION
data     -rw,sync,hard  servera:/exports/data
logs     -ro            servera:/exports/logs
backup   -rw            192.168.1.20:/backup
Master map field Meaning
Base directoryThe directory autofs manages (must exist or autofs creates it)
Map filePath to the file listing individual mounts under the base directory
--timeout=NSeconds of inactivity before auto-unmounting (default 300)

The Master Map: /etc/auto.master

The master map is the starting point for all autofs configuration. Every autofs-managed directory is registered here.

# View the default /etc/auto.master on RHEL 9
$ cat /etc/auto.master
#
# This is the master map file for autofs.
# See auto.master(5) for details.
#
+dir:/etc/auto.master.d
+auto.master
/misc  /etc/auto.misc
/net   -hosts

# Add a new section to manage /shares with map file /etc/auto.shares
/shares  /etc/auto.shares  --timeout=300

# Alternative: use the drop-in directory (survives package updates)
$ sudo tee /etc/auto.master.d/shares.autofs <<'EOF'
/shares  /etc/auto.shares  --timeout=300
EOF

# Reload autofs to apply the new master map entry
$ sudo systemctl reload autofs
Drop-in directory: /etc/auto.master.d/

Files ending in .autofs in /etc/auto.master.d/ are automatically included in the master map. This is the preferred location for site-specific entries — they survive autofs package updates that might reset /etc/auto.master.

Indirect Map Configuration

An indirect map defines a base directory managed by autofs. Each key in the map file becomes a subdirectory that triggers an automatic mount.

# Step 1: Create the base directory
$ sudo mkdir -p /shares

# Step 2: Add to /etc/auto.master (or drop-in)
$ sudo vim /etc/auto.master
# Append this line:
/shares  /etc/auto.shares  --timeout=300

# Step 3: Create the map file /etc/auto.shares
$ sudo vim /etc/auto.shares
# Format: KEY  [-OPTIONS]  SERVER:/EXPORT
data    -rw,sync,hard,intr  servera:/exports/data
logs    -ro                 servera:/exports/logs

# Step 4: Reload autofs
$ sudo systemctl reload autofs

# Step 5: Test — accessing /shares/data triggers the mount
$ ls /shares/data
$ mount | grep shares
servera:/exports/data on /shares/data type nfs4 (rw,...)
RHCSA pattern: indirect map in 5 steps

mkdir base → edit auto.master → create map file → reload autofs → test with ls. The ls triggers the mount; do not use mount manually.

Map File Format in Detail

Each line in a map file has three parts. Options are optional but the dash prefix is required when options are specified.

# Map file format:
# KEY   [-OPTIONS]   LOCATION

# Example entries
data    -rw,sync,hard           servera:/exports/data
logs    -ro                     servera:/exports/logs
usb     -fstype=vfat,uid=1000   :/dev/sdb1
iso     -fstype=iso9660,ro      :/dev/cdrom
*       -rw,sync,hard           servera:/home/&
Field Description Example
KEYThe subdirectory name that triggers the mount. * matches any key.data, *
OPTIONSMount options prefixed with a dash. Comma-separated. Optional.-rw,sync,hard
LOCATIONNFS: SERVER:/path. Local: :/dev/device. With & for wildcard substitution.servera:/exports/data
The dash before options is mandatory

Options in map files must be prefixed with a dash: -rw,sync. Without the dash, autofs cannot parse the line and silently ignores it — the mount will not work but no error is reported in the standard output.

Direct Map Configuration

A direct map mounts NFS shares at explicit absolute paths anywhere in the filesystem tree — not as subdirectories of a base directory.

# /etc/auto.master — direct map uses /- as the base directory
/-  /etc/auto.direct

# /etc/auto.direct — absolute paths as keys
# Format: ABSOLUTE_PATH  [-OPTIONS]  LOCATION
/data         -rw,sync  servera:/exports/data
/var/reports  -ro       servera:/exports/reports
/opt/apps     -ro       servera:/exports/apps

# The directories must exist before autofs can manage them
$ sudo mkdir -p /data /var/reports /opt/apps
$ sudo systemctl reload autofs

# Test
$ ls /data    # triggers mount of servera:/exports/data at /data
Direct vs indirect: when to use each

Indirect: multiple mounts under a common parent directory — e.g. /shares/data, /shares/logs.
Direct: mounts at specific arbitrary paths — e.g. /data, /var/reports.

Wildcard Home Directory Automounting

The most important autofs exam pattern: mount any user's home directory on demand using a single map file entry with the * wildcard and & substitution.

# Scenario: NFS server exports /home/USERNAME for each user
# Goal: /home/student1 automounts as servera:/home/student1 on demand

# Step 1: Add to /etc/auto.master
/home  /etc/auto.home  --timeout=300

# Step 2: Create /etc/auto.home
$ sudo vim /etc/auto.home
# * = matches any username; & = substitutes matched value
*  -rw,sync,hard  servera:/home/&

# Step 3: Reload autofs
$ sudo systemctl reload autofs

# Step 4: Test — access a user's home directory
$ ls /home/student1     # mounts servera:/home/student1
$ ls /home/operator1    # mounts servera:/home/operator1
$ mount | grep home
servera:/home/student1 on /home/student1 type nfs4 (rw,...)

The * and & Substitution

Understanding exactly how * and & interact is essential for writing correct wildcard map entries.

# Map file entry
*  -rw,sync  servera:/home/&

# When /home/alice is accessed:
#  *  matches "alice"
#  &  substitutes to "alice"
#  autofs mounts: servera:/home/alice  →  /home/alice

# When /home/ldapuser3 is accessed:
#  *  matches "ldapuser3"
#  &  substitutes to "ldapuser3"
#  autofs mounts: servera:/home/ldapuser3  →  /home/ldapuser3

# The & can appear anywhere in the location path:
*  -ro  servera:/exports/users/&/data
# /home/alice → servera:/exports/users/alice/data

# Without & — every key mounts the SAME path (unusual)
*  -ro  servera:/exports/shared
# /home/ANYUSER → servera:/exports/shared (same for everyone)
* matches; & substitutes

* is the pattern that matches any key value in the map. & is the substitution that inserts the matched value into the server path. Both are needed together for per-user home directories.

autofs Timeout and Global Configuration

The autofs daemon reads its global settings from /etc/autofs.conf. Per-map timeouts are set in the master map.

# View/edit the global autofs configuration
$ cat /etc/autofs.conf
[autofs]
timeout = 300          # default idle timeout in seconds
negative_timeout = 60  # how long to cache a failed lookup
browse_mode = no       # show managed dirs even when not mounted
logging = none         # logging level: none, verbose, debug

# Set per-map timeout in the master map (overrides global)
/home  /etc/auto.home  --timeout=600  # 10 minutes for home dirs
/shares /etc/auto.shares --timeout=60  # 1 minute for shared data

# Check how long a mount has been idle
$ automount --pid-file /run/autofs.pid -f

# View autofs debug output
$ sudo journalctl -u autofs -f
browse_mode shows directories before mounting

Setting browse_mode = yes in /etc/autofs.conf makes autofs-managed directories visible in directory listings even before they are accessed. Useful when users need to see available shares without clicking through to access them.

Verifying autofs Operation

Confirming that autofs is correctly configured requires checking the service, triggering a mount, and reading the result.

# Confirm autofs service is running
$ systemctl status autofs

# List what autofs is currently managing
$ automount --version

# Trigger a mount by listing a managed directory
$ ls /shares/data

# Confirm the mount is active
$ mount | grep autofs
autofs on /shares type autofs (rw,relatime,fd=...)
servera:/exports/data on /shares/data type nfs4 (rw,...)

# Confirm with df -hT
$ df -hT /shares/data
Filesystem             Type  Size  Used Avail Use% Mounted on
servera:/exports/data  nfs4   10G  3.2G  6.8G  32% /shares/data

# Watch autofs log for mount/unmount events
$ sudo journalctl -u autofs --since "5 minutes ago"

Troubleshooting autofs

When autofs does not mount a share as expected, a systematic diagnosis finds the cause quickly.

# Check autofs service status and recent log entries
$ systemctl status autofs
$ sudo journalctl -u autofs --since "10 minutes ago"

# Verify the master map is correctly parsed
$ sudo automount --dumpmaps
map /etc/auto.master has 2 entries:
  /shares   /etc/auto.shares
  /home     /etc/auto.home

# Check if the NFS server is reachable
$ showmount -e servera

# Test the NFS mount manually to isolate autofs from NFS issues
$ sudo mount -t nfs servera:/exports/data /mnt/test
$ sudo umount /mnt/test

# Enable verbose autofs logging temporarily
$ sudo vim /etc/autofs.conf
# Change: logging = none  to: logging = verbose
$ sudo systemctl restart autofs
Symptom Likely cause
Directory not visible under base pathautofs not running, or master map entry missing/typo
ls returns "No such file or directory"Map file entry missing, NFS server unreachable, or options have no leading dash
Mount hangs indefinitelyNFS server reachable but not responding — check firewall and NFS service on server
Changes not taking effectautofs not reloaded after config change

autofs Quick Reference

Task Command or configuration
Install autofssudo dnf install -y autofs nfs-utils
Enable and start autofssudo systemctl enable --now autofs
Reload after config changessudo systemctl reload autofs
Master map file/etc/auto.master
Master map drop-in directory/etc/auto.master.d/*.autofs
Master map entry (indirect)/BASE /etc/auto.MAPFILE --timeout=300
Master map entry (direct)/- /etc/auto.direct
Map file entry formatKEY -OPTIONS SERVER:/EXPORT
Wildcard home directory entry* -rw,sync,hard SERVER:/home/&
Trigger a mount (access)ls /BASE/KEY
Verify mount is activemount | grep autofs or df -hT /BASE/KEY
Dump all autofs mapssudo automount --dumpmaps
View autofs logsudo journalctl -u autofs

Common Mistakes

Mistake What goes wrong Correct approach
Missing the leading dash before map file options autofs cannot parse the line — mount silently fails with "No such file or directory" KEY -rw,sync SERVER:/path — the dash before options is mandatory
Editing map files but not reloading autofs Changes have no effect — autofs still uses the old in-memory configuration sudo systemctl reload autofs after every map file change
Forgetting the & in the wildcard location All keys mount the same server path instead of user-specific paths * -rw SERVER:/home/& — & substitutes the matched key
Base directory does not exist before autofs reload autofs fails to register the map — entries under the base path are inaccessible Create the base directory first: sudo mkdir -p /BASE
Trying to manually mount within an autofs-managed directory Manual mounts conflict with autofs management — unpredictable behaviour Let autofs manage its directories — do not run mount manually inside them
Configuring autofs and fstab for the same mount point Both try to manage the same path — boot errors or mount conflicts Use either autofs or fstab for a given path, never both

Knowledge Check

Answer these before moving to the next slide.

  1. What are the two configuration files needed to configure a new autofs indirect mount, and what does each one contain?
  2. Write the complete configuration (master map entry and map file entry) to automount fileserver:/exports/reports at /reports/monthly when accessed, with read-only access.
  3. Write the /etc/auto.master entry and the /etc/auto.home map file entry to automount any user's home directory from nfsserver:/home/USERNAME at /home/USERNAME.
  4. What does the & symbol do in an autofs map file, and why is it needed in the wildcard home directory pattern?
  5. You edit /etc/auto.shares to add a new NFS share. What command must you run for the change to take effect without interrupting existing mounts?
  6. An autofs share fails to mount — the directory appears empty when accessed. Write two commands to begin diagnosing the problem.

Knowledge Check — Answers

  1. Two files: /etc/auto.master (the master map) contains the base directory, the path to the map file, and global options like timeout. /etc/auto.MAPFILE (the map file) contains individual entries with the key, mount options, and NFS server path.
  2. In /etc/auto.master: /reports /etc/auto.reports --timeout=300
    Create /etc/auto.reports with: monthly -ro fileserver:/exports/reports
    Create the base dir: sudo mkdir -p /reports, then reload autofs.
  3. In /etc/auto.master: /home /etc/auto.home --timeout=300
    In /etc/auto.home: * -rw,sync,hard nfsserver:/home/&
  4. & (ampersand) substitutes the value matched by the * wildcard into the location field. Without it, every user would mount the same fixed server path. With it, accessing /home/alice builds the path nfsserver:/home/alice and accessing /home/bob builds nfsserver:/home/bob — each user gets their own home directory.
  5. sudo systemctl reload autofs — reload re-reads all map files immediately without stopping the daemon or interrupting any existing active mounts.
  6. Any two of: sudo journalctl -u autofs --since "10 minutes ago" (check service log), sudo automount --dumpmaps (verify the map entry is parsed), showmount -e SERVERNAME (verify the NFS export exists), systemctl status autofs (confirm the service is running).

Key Takeaways

  1. autofs mounts on demand and unmounts automatically. Install autofs and nfs-utils. Enable with systemctl enable --now autofs. Reload — not restart — after config changes: systemctl reload autofs.
  2. Two files configure every indirect mount: master map + map file. Master map entry: /BASE /etc/auto.MAPFILE --timeout=300. Map file entry: KEY -OPTIONS SERVER:/export. The base directory must exist. The leading dash on options is mandatory.
  3. The wildcard pattern handles all home directories with one entry. Master map: /home /etc/auto.home. Map file: * -rw,sync,hard SERVER:/home/&. * matches any username; & substitutes it into the path.
  4. Trigger mounts with ls; verify with mount | grep autofs. Never run mount manually inside autofs-managed directories. Use automount --dumpmaps to confirm map files are parsed correctly. Check journalctl -u autofs for errors.

Graded Lab

  • Install autofs and nfs-utils. Enable and start the autofs service. Confirm it is running with systemctl status autofs.
  • Configure an indirect map: add /shares /etc/auto.shares --timeout=60 to /etc/auto.master. Create /etc/auto.shares with one entry mounting an NFS export from your lab server at /shares/labdata.
  • Create the /shares base directory, reload autofs, and test by running ls /shares/labdata. Confirm the mount with mount | grep shares and df -hT /shares/labdata.
  • Wait 60 seconds (the timeout set in step 2) and run mount | grep shares again to confirm the share was automatically unmounted. Then access it again to confirm it remounts on demand.
  • Configure autofs to handle home directories: add /home /etc/auto.home to /etc/auto.master. Create /etc/auto.home with the wildcard entry. Reload autofs and test by accessing a home directory that exists on the NFS server.
  • Run sudo automount --dumpmaps to verify both map entries are parsed. Run sudo journalctl -u autofs --since today to review mount and unmount events from the session.
RHCSA Objective

"Configure autofs." Know the two-file model (auto.master + map file), the leading dash for options, the wildcard * and & pattern for home directories, and systemctl reload autofs after every change.