Chapter 19: Network file systems and autofs

Permanent NFS mounts and autofs

Two ways to make a share available automatically. An /etc/fstab entry mounts it at boot and keeps it mounted, which is simple but ties the boot to the server being there. autofs mounts it the first time somebody uses the path and unmounts it again when it is idle, which is lighter and more forgiving. This lesson shows both and when to use which.

The fstab way

An NFS line in /etc/fstab: Select a field.

[root@servera ~]# echo "serverb:/srv/shared  /mnt/shared  nfs  defaults,_netdev  0 0" >> /etc/fstab
[root@servera ~]# systemctl daemon-reload
[root@servera ~]# mount -a
[root@servera ~]# df -hT /mnt/shared | tail -1
serverb:/srv/shared nfs4   20G  1.7G   18G   9% /mnt/shared

The same routine as for disks (previous chapters): back up fstab first, daemon-reload, test with mount -a before rebooting. The cost of this method: at boot, the system tries to mount the share, and if the server is down, the machine can wait or drop into emergency mode. Add nofail when the share is optional, or x-systemd.automount, which turns the entry into an on-demand mount.

The autofs way

autofs is a service that watches directories and mounts things there on demand.

How autofs works: Step through an on-demand mount.

Install and enable it, then describe the mount tree in two places:

[root@servera ~]# dnf install -y autofs
[root@servera ~]# cat /etc/auto.master.d/shared.autofs
/mnt/auto  /etc/auto.shared  --timeout=20
[root@servera ~]# cat /etc/auto.shared
data  -rw,sync  serverb:/srv/shared
[root@servera ~]# systemctl enable --now autofs
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.

Now watch it work:

[root@servera ~]# mount | grep -c /mnt/auto/data
0
[root@servera ~]# ls /mnt/auto/data
alice  hello.txt
[root@servera ~]# findmnt -t autofs,nfs4 -o TARGET,SOURCE,FSTYPE | tail -2
/mnt/auto              /etc/auto.shared    autofs
└─/mnt/auto/data         serverb:/srv/shared nfs4
[root@servera ~]# sleep 30; mount | grep -c /mnt/auto/data
0

Before the ls there was nothing mounted at /mnt/auto/data. The ls triggered the mount; thirty seconds later (the timeout is 20) it was gone again. Note that /mnt/auto itself looks empty until a key is used: an indirect map creates its subdirectories on demand.

After editing a master or map file, run systemctl reload autofs. If a path does not appear, journalctl -u autofs and the error shown by ls tell you more (a wrong server path gives "No such file or directory").

Which one?

fstabautofs
Always mountedYes, from bootOnly while in use
Server down at bootCan delay or break the boot (unless nofail)No effect until someone uses the path
Many mounts (home directories)One line eachOne wildcard line
SetupOne linePackage, master map, map, service

Try it

Check your understanding