Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 228Network · Storage

How to mount network folders on demand with autofs

Fifty NFS shares in /etc/fstab slow down every boot, and when the NFS server is down, the client hangs. autofs mounts a share only when someone opens its path, and unmounts it again after some idle minutes. A wildcard map can even give every user their own home folder from the file server with one line. This how-to sets up an indirect map, a wildcard map for homes, and tests it.

  • 5 Lessons
  • 10 Commands

$ run as a normal user# run as root (or with sudo)

228.1

The quick answer

/etc/auto.master.d/data.autofs
/data  /etc/auto.data  --timeout=300
/etc/auto.data
projects  -rw,soft,vers=4.2  nfs01.example.com:/exports/projects
archive   -ro,vers=4.2       nfs01.example.com:/exports/archive
dnf install -y autofs nfs-utils
systemctl enable --now autofs
ls /data/projects
findmnt /data/projects

228.2

Step by step

  1. Install autofs and the NFS client (Debian/Ubuntu: autofs nfs-common).
  2. Master map (first file) — "everything under /data is managed by the map /etc/auto.data, unmount after 300 s idle". Do not create /data/projects yourself; autofs creates the folders.
  3. Map file (second file) — one line per share: key (folder name under /data), mount options, source. Check the exports first: showmount -e nfs01.example.com (NFSv3) or try a manual mount.
  4. Start and trigger — ls /data/projects mounts it; ls /data alone shows nothing until a key is used (add --browse in the master map to show all keys).
  5. Home folders with a wildcard:
/etc/auto.master.d/home.autofs
/home/remote  /etc/auto.home
/etc/auto.home
*  -rw,vers=4.2  nfs01.example.com:/exports/home/&

* matches any name, & is replaced by it: /home/remote/alice mounts …/home/alice.

  1. See the active maps — automount -m prints what autofs has loaded. After editing maps: systemctl reload autofs.
  2. Debug — journalctl -u autofs -f while you ls the path; stop the service and run automount -f -v in the foreground for verbose output.

228.3

Check that it worked

ls /data/projects lists the files; findmnt /data/projects shows nfs4 from nfs01.example.com:/exports/projects; after 5 idle minutes findmnt shows it unmounted again; ls /home/remote/alice mounts alice's home.

228.4

Common problems

ProblemCauseFix
No such file or directory on the keyMap not loaded, typo in the key, or master file not ending in .autofs.automount -m; systemctl reload autofs.
access denied by serverExport does not allow this client.Fix /etc/exports on the server (exportfs -v).
Folder exists but is emptyYou created it by hand under the autofs mount point.Remove your own folders; let autofs create them.
Hangs when the server is downHard mount.Use soft (careful with writes) and short timeouts; monitor the server.
Permission denied as root on NFSRoot squash on the server.Normal security; work as the owning user.
Mounts never go awaySomething keeps them busy (shell in the folder).lsof +f -- /data/projects; shorter --timeout.

228.5

Go further

1What is the advantage of autofs over fstab for NFS?

Shares are mounted only when used and unmounted when idle, so boots are fast and a down server does not block the system.

2What do * and & mean in an autofs map?

* matches any folder name that is opened, and & is replaced by that name in the source path.

All commands in this chapter