File Systems, NFS and AutofsLesson 3 of 4
Mounting on demand with autofs
Mount NFS shares only when someone uses them, with indirect, direct and wildcard maps, and unmount them again when idle.
autofs/etc/auto.master/etc/auto.master.dsystemctl reload autofsautomount -mfindmntuse_nfs_home_dirs
After this lesson you can
- Explain how autofs differs from an fstab mount and when to prefer it.
- Write a master map entry and a map file for indirect, direct and wildcard (home directory) mounts.
- Enable the service, trigger a mount, verify it, and diagnose a map that does nothing.
The idea
An /etc/fstab NFS line mounts a share at boot and keeps it mounted whether anyone uses it or not. With hundreds of shares or an unreliable server, that is wasteful and fragile. autofs mounts a share the moment someone accesses its directory and unmounts it after it has been idle for a while.
The service is autofs (package autofs; NFS support from nfs-utils). Its configuration has two layers:
- The master map says where autofs manages a directory and which map file describes it.
- A map file says, for each name under that directory, what to mount.
/mnt/remote /etc/auto.remote --timeout=60
share -rw,sync node1:/srv/share
Together: accessing /mnt/remote/share mounts node1:/srv/share. autofs creates the share directory itself; it does not exist until accessed, and ls /mnt/remote may even look empty. That is normal, not a fault.
Three kinds of map
| Kind | Master entry | Map line | Result |
|---|---|---|---|
| Indirect | /mnt/remote /etc/auto.remote | share -rw node1:/srv/share | Mounts under the parent: /mnt/remote/share |
| Direct | /- /etc/auto.direct | /data/shared -rw node1:/srv/share | Mounts at an exact path |
| Wildcard | /rhome /etc/auto.rhome | * -rw node1:/home/remoteusers/& | One line serves every user: /rhome/NAME mounts /home/remoteusers/NAME |
In the wildcard line, * matches whatever name was requested and & substitutes that same name. This is the standard way to provide home directories for many users.
Rather than editing /etc/auto.master, put a file ending in .autofs into /etc/auto.master.d/; it is read as part of the master map. That keeps each task in its own small file.
Service behaviour
- Start and enable:
systemctl enable --now autofs. - Master map changes (new file, new timeout) need
systemctl reload autofs. - Map file changes take effect on the next access, no reload.
- Do not also mount the same path from fstab, and do not put your own files in the autofs directory: the contents are hidden while it is managed.
--timeout=SECONDSis how long the mount stays idle before unmounting. The default is 300.
For users logging in with an NFS home directory, SELinux must allow it: setsebool -P use_nfs_home_dirs on. Without it, login succeeds and the home directory access is denied.
Try it
sudo dnf install -y autofs nfs-utils
echo '/mnt/remote /etc/auto.remote --timeout=60' | sudo tee /etc/auto.master.d/remote.autofs
echo 'share -rw,sync node1:/srv/share' | sudo tee /etc/auto.remote
sudo systemctl enable --now autofs
/mnt/remote /etc/auto.remote --timeout=60
share -rw,sync node1:/srv/share
Created symlink '/etc/systemd/system/multi-user.target.wants/autofs.service' → '/usr/lib/systemd/system/autofs.service'.
findmnt -t nfs4
ls /mnt/remote/share
findmnt -t nfs4
hello.txt
TARGET SOURCE FSTYPE OPTIONS
/mnt/remote/share node1:/srv/share nfs4 rw,relatime,vers=4.2,...
The first findmnt printed nothing: no mount existed until the ls. After an idle minute, it disappears again.
sudo automount -m | head -8
autofs dump map information
===========================
global options: none configured
Mount point: /mnt/remote
source(s):
instance type(s): file
map: /etc/auto.remote
share | -rw,sync node1:/srv/share
This shows exactly what autofs parsed. If your entry is missing here, the master file was not read: check its name ends in .autofs and that you reloaded.
Home directories with one line
echo '/rhome /etc/auto.rhome' | sudo tee /etc/auto.master.d/rhome.autofs
echo '* -rw,sync node1:/home/remoteusers/&' | sudo tee /etc/auto.rhome
sudo setsebool -P use_nfs_home_dirs on
sudo systemctl reload autofs
ls /rhome/remoteuser1
Documents notes.txt
Diagnosing
If the directory stays empty and nothing mounts:
systemctl is-active autofs
sudo automount -m | grep -A3 'Mount point'
sudo journalctl -u autofs -n 10 --no-pager
getent hosts node1
showmount -e node1
For a detailed trace, stop the service and run the daemon in the foreground: sudo systemctl stop autofs; sudo automount -f -v, reproduce in a second terminal, then Ctrl+C and start the service again.
Going further
Map options accept anything mount does, such as -fstype=nfs4,rw,sec=sys. +auto.master includes NIS or LDAP maps, which is how autofs is usually deployed in larger sites. automount --dumpmaps is another way to inspect the configuration. The ghost option makes keys visible before they are mounted, and /etc/autofs.conf holds global defaults such as timeout. Local file systems and CD-ROMs can be handled the same way (-fstype=iso9660,ro :/dev/sr0).
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
An indirect map for one share
Make
node1:/srv/share(from the previous lesson) appear at/mnt/remote/shareon the client, mounted automatically on first access. Use a master drop-in file/etc/auto.master.d/remote.autofs, a map file/etc/auto.remote, read-write, and an idle timeout of 60 seconds. Enableautofsat boot.Check your result
ls /mnt/remoteshows nothing at first. Afterls /mnt/remote/sharethe filehello.txtis listed andfindmnt /mnt/remote/shareshowsnfs4. After 60+ idle seconds the mount is gone.Hint
Master:
/mnt/remote /etc/auto.remote --timeout=60. Map:share -rw,sync node1:/srv/share. Installautofs,systemctl enable --now autofs.Solution
sudo dnf install -y autofs nfs-utils echo '/mnt/remote /etc/auto.remote --timeout=60' | sudo tee /etc/auto.master.d/remote.autofs echo 'share -rw,sync node1:/srv/share' | sudo tee /etc/auto.remote sudo systemctl enable --now autofs ls /mnt/remote ls /mnt/remote/share findmnt /mnt/remote/share - Practice
Home directories for remote users
The server exports
/home/remoteusersand each userremoteuser1,remoteuser2, ... has a directory there. On the client, make/rhome/remoteuser1mountnode1:/home/remoteusers/remoteuser1automatically, and the same for any other user, with one map line. Allow SELinux to use NFS home directories.Check your result
ls /rhome/remoteuser1mounts and lists the files.ls /rhome/remoteuser2also works without any extra line.getsebool use_nfs_home_dirsprintson.Hint
Wildcard map:
* -rw,sync node1:/home/remoteusers/&. The*matches the directory name asked for,&repeats it.setsebool -P use_nfs_home_dirs on.Solution
echo '/rhome /etc/auto.rhome' | sudo tee /etc/auto.master.d/rhome.autofs echo '* -rw,sync node1:/home/remoteusers/&' | sudo tee /etc/auto.rhome sudo setsebool -P use_nfs_home_dirs on sudo systemctl reload autofs ls /rhome/remoteuser1 getsebool use_nfs_home_dirs - Practice
A direct map at an exact path
Mount
node1:/srv/shareat exactly/data/shared(not under a parent directory) with a direct map. Verify that/dataitself is not otherwise managed by autofs.Check your result
ls /data/sharedtriggers the mount;findmnt /data/sharedshowsnfs4. The master entry uses/-as the mount point.Hint
Master:
/- /etc/auto.direct. Map:/data/shared -rw,sync node1:/srv/share. The map key is the full path.Solution
echo '/- /etc/auto.direct' | sudo tee /etc/auto.master.d/direct.autofs echo '/data/shared -rw,sync node1:/srv/share' | sudo tee /etc/auto.direct sudo systemctl reload autofs ls /data/shared findmnt /data/shared - Practice
When is a reload needed?
Change the map file
/etc/auto.remoteby adding a second keydocspointing to another export, without restarting anything, and check it works. Then change the master map timeout and show which change needs a reload.Check your result
A new key in a map file works on the next access. A change to the master map (or a new master file) only takes effect after
systemctl reload autofs.Hint
Map files are re-read on access; master maps are read at start and reload.
Solution
echo 'docs -rw,sync node1:/srv/docs' | sudo tee -a /etc/auto.remote ls /mnt/remote/docs sudo sed -i 's/--timeout=60/--timeout=120/' /etc/auto.master.d/remote.autofs sudo systemctl reload autofs automount -m | head - Challenge
The directory is empty and nothing mounts
Given a map whose server name is misspelled, diagnose and repair. List the checks in order: service running, master map loaded, map syntax, name resolution, export reachable, journal messages. Then repair.
Check your result
systemctl status autofsis active;automount -mshows the mount point and map;journalctl -u autofssaysmount(nfs): ... failedorlookup(file); after fixing the name,ls /mnt/remote/shareworks.Hint
Run the automounter in the foreground for detail:
sudo systemctl stop autofs; sudo automount -f -v.Solution
systemctl is-active autofs sudo automount -m sudo journalctl -u autofs -n 20 --no-pager getent hosts node1 showmount -e node1 sudo sed -i 's/nod1/node1/' /etc/auto.remote ls /mnt/remote/share