Skip to content
Visual explainer 2h 30m to learn 4 steps

NFS clients & autofs

Mount NFS exports manually, persistently and on demand with autofs.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 22 of 42

In short

NFS lets a client mount directories exported by a file server over the network. You can mount them manually, persistently in fstab, or on demand with autofs, which mounts a share when someone accesses it and unmounts it after it sits idle. Automounted home directories are a classic RHCSA task.

Explained simply

NFS, the Network File System, lets one server share a folder over the network. Other machines mount that folder, and for their users it looks just like a local directory. They can list, open and save files, and every client sees the same data.

The server shares a directory, which is called an export. The client mounts it with a normal mount command, giving the server's name and the exported path, for example nfs.example.com:/exports/projects.

Mounting a share at every boot through fstab works, but it has downsides: boot waits if the server is down, and hundreds of clients keep connections open all the time. autofs solves this by mounting a share only when someone actually opens the folder, and unmounting it again after it has been idle for a while.

Companies use this for shared project folders and especially for home directories: a user logs in on any machine and their home folder appears, mounted from the file server.

Press Play on a diagram to walk through it one step at a time.

SequenceOn-demand mount with autofs
User
autofs
NFS server
  1. 1cd /shares/projects
    User to autofs: cd /shares/projects
  2. 2mount nfs.example.com:/exports/projects
    autofs to NFS server: mount nfs.example.com:/exports/projects
  3. 3Export mounted
    NFS server to autofs: Export mounted
  4. 4Directory available, cd succeeds
    autofs to User: Directory available, cd succeeds
  5. 5Unmount after the idle timeout
    autofs to NFS server: Unmount after the idle timeout
CompareThree ways to mount an NFS share
Manual mount
  • mount -t nfs server:/exp /mnt
  • Good for a quick test
  • Gone after reboot
fstab
  • Mounted at every boot
  • Use _netdev and maybe nofail
  • Always connected, even when unused
autofs
  • Mounted on first access
  • Unmounted after idle timeout
  • Wildcards for many home dirs

In depth

NFS versions and what they need

Modern Linux clients use NFS version 4, usually 4.2. NFSv4 talks over a single TCP port, 2049, which makes firewalls simple. Older NFSv3 also needs rpcbind on port 111 and extra helper services such as mountd on changing ports.

That matters for showmount -e. It asks the server's mountd, an NFSv3 helper, for the export list. On a server that only allows NFSv4, or a firewall that only opens 2049, showmount fails even though mounting works. In that case mount the server's root, as in mount nfs.example.com:/ /mnt, and browse: NFSv4 presents all exports under one pseudo root.

On the client you need the nfs-utils package on RHEL or nfs-common on Ubuntu. You can force a version with -o vers=4.2, and nfsstat -m or findmnt shows which version and options a mount actually got.

  • NFSv4: TCP 2049 only
  • NFSv3: also rpcbind 111 and mountd
  • nfsstat -m shows per-mount options

Mount options that change behaviour

hard is the default: if the server stops answering, programs that touch the mount wait and keep retrying until it returns. Data stays safe, but processes can hang in state D. soft gives up after the retries and returns an error, which can corrupt files an app was writing, so use it only for read-mostly data. timeo sets the wait before a retry, in tenths of a second.

rw and ro set the client side, but the server's export options win. A common one is root_squash, on by default: root on the client is treated as the anonymous user nobody on the server, so sudo cannot bypass permissions on the share. User IDs must match across machines, because NFS by default checks permissions by number, not by name.

In fstab add _netdev so systemd waits for the network, and nofail if boot should continue without the share.

  • server:/exports/data /data nfs _netdev,nofail 0 0
  • hard: wait and retry (default)
  • root_squash: client root becomes nobody

autofs maps: master, indirect and direct

autofs reads the master map, /etc/auto.master, which includes every file in /etc/auto.master.d/ ending in .autofs. Each master line names a mount point and a map file.

An indirect map has a base directory such as /shares. Each key in the map file is a subdirectory name: the line projects -rw,sync nfs.example.com:/exports/projects makes /shares/projects appear on access. autofs owns the base directory, so do not create subfolders in it yourself. The wildcard key * with & in the location means use the same name, which is perfect for home directories.

A direct map uses the special key /- in the master map. Its map file lists full paths, for example /mnt/docs -ro nfs.example.com:/exports/docs. Use it when shares must appear at fixed, unrelated paths. After editing maps, run systemctl reload autofs. By default a share is unmounted after 300 seconds idle; --timeout=600 on the master line changes it.

MapHow autofs config files fit together
/etc/auto.master
  • +dir:/etc/auto.master.d
    • Includes every *.autofs file
  • shares.autofs: /shares /etc/auto.shares
    • Indirect map
    • projects -rw nfs:/exports/projects
    • Appears as /shares/projects
  • home.autofs: /home/guests /etc/auto.guests
    • Wildcard map
    • * -rw nfs:/home/guests/&
  • direct.autofs: /- /etc/auto.direct
    • Direct map
    • /mnt/docs -ro nfs:/exports/docs

Debugging an automount that does not appear

With an indirect map, ls /shares often shows nothing, which confuses people. autofs creates the subfolder only when you access it by name, so try ls /shares/projects instead. To make keys visible before mounting, add the browse option on the master line or set browse_mode = yes in /etc/autofs.conf (it only works for keys that are not wildcards).

If access still fails, check the basics in order: is autofs running (systemctl status autofs), can you mount the share by hand with mount -t nfs, and does the master map name the right file. journalctl -u autofs shows lookup errors. For detail, stop the service and run automount -f -v in the foreground, then access the path from another terminal to see each step.

  • ls /shares/projects, not just ls /shares
  • automount -f -v runs in the foreground with logging
  • mount | grep autofs lists the autofs trigger points

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Discover exports with showmount~40m
  2. 2
    Mount an NFS share manually~40m
  3. 3
    Persist NFS mounts in fstab~40m
  4. 4
    Configure autofs direct and indirect maps~40m

Key points

  • showmount -e server lists exports, if the server allows it.
  • In fstab use _netdev so the mount waits for the network.
  • autofs master map: /etc/auto.master.d/NAME.autofs points a base directory at a map file.
  • Indirect map keys are subdirectories; the wildcard * with & mounts any matching name.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo dnf install -y nfs-utils autofs
showmount -e nfs.example.com
sudo mount -t nfs nfs.example.com:/exports/projects /mnt
echo "/shares /etc/auto.shares" | sudo tee /etc/auto.master.d/shares.autofs
echo "* -rw,sync nfs.example.com:/exports/&" | sudo tee /etc/auto.shares
sudo systemctl enable --now autofs
ls /shares/projects && mount | grep shares

A manual mount, then a wildcard indirect autofs map.

Automount home directories for remote users

Users remote1, remote2 and so on have home folders exported by nfs.lab.example.com under /homes. They should appear at /home/remote/USERNAME on demand. Step 1: install autofs. Step 2: create a master map file pointing /home/remote to a map. Step 3: write a wildcard map where * is the key and & repeats it in the server path. Step 4: enable autofs and test by accessing one user's folder by name.

bash
sudo dnf install -y autofs nfs-utils
echo '/home/remote /etc/auto.remote --timeout=600' | sudo tee /etc/auto.master.d/remote.autofs
echo '* -rw,sync nfs.lab.example.com:/homes/&' | sudo tee /etc/auto.remote
sudo systemctl enable --now autofs
ls /home/remote/remote1
findmnt -t nfs4

showmount fails, but the share is there

showmount -e nfs.example.com returns an RPC error, so a colleague thinks the server is down. Step 1: test port 2049, which NFSv4 needs. Step 2: mount the server's NFSv4 pseudo root and list it to discover the exports. Step 3: mount the export you need directly. The server is fine; it just does not run the NFSv3 helper services that showmount relies on.

bash
showmount -e nfs.example.com
# clnt_create: RPC: Unable to receive
timeout 3 bash -c '</dev/tcp/nfs.example.com/2049' && echo 2049 open
sudo mount -t nfs -o vers=4.2 nfs.example.com:/ /mnt
ls /mnt
sudo umount /mnt
sudo mount -t nfs nfs.example.com:/exports/projects /srv/projects

Quick reference

  • showmount -e server lists exports (needs NFSv3 helpers on the server)
  • sudo mount -t nfs -o vers=4.2 server:/exports/data /mnt mounts a share by hand
  • fstab: server:/exports/data /data nfs _netdev,nofail 0 0
  • Indirect map: master line /shares /etc/auto.shares, map line key options server:/path
  • Direct map: master line /- /etc/auto.direct, map line /full/path options server:/path
  • Wildcard home dirs: * -rw,sync server:/home/&
  • sudo systemctl reload autofs after editing maps; idle timeout default is 300 seconds
  • nfsstat -m or findmnt -t nfs4 shows NFS mounts with their real options

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which TCP port does an NFSv4 client need to reach on the server?

Question 2

2In an indirect autofs map, what does & mean in * -rw server:/homes/&?

Question 3

3ls /shares shows an empty folder even though autofs is running. Why?

Question 4

4Root on an NFS client cannot read a root-owned file on the share. What is the likely reason?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Mount nfs.example.com:/exports/projects at /mnt/projects by hand, check which NFS version was negotiated, then unmount it.

  2. Exercise 2medium

    Make the share mount at boot at /srv/projects, without blocking boot if the server is unreachable, and test it without rebooting.

  3. Exercise 3hard

    Configure autofs so /mnt/docs (read-only from server:/exports/docs) and /shares/ANYNAME (read-write from server:/exports/ANYNAME) both mount on demand.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between NFS and Samba?
Both share folders over the network. NFS is the native choice between Linux and Unix machines; Samba speaks SMB, the protocol Windows uses.
Why does my program hang when the NFS server goes down?
NFS mounts are hard by default, so the client waits and retries until the server returns, to protect your data. The process continues once the server is back.
Should I use fstab or autofs for NFS?
Use fstab for a share a server always needs, such as application data. Use autofs for shares used now and then, or for many per-user folders like home directories.
Do I have to create the folders autofs mounts on?
For indirect maps, no: autofs creates the subfolders under the base directory itself. For direct maps, autofs creates the full paths as needed too.

Go deeper

ShareXLinkedInWhatsApp