Autofs service provides an alternate way to mount nfs shares. Automount daemon is used to automatically mount file systems on demand i.e. when they are being accessed. not only will it mount automatically but it can automatically unmount when not in use for a particular predefined timeout value.
Yes, you can argue that it is much easier to edit /etc/fstab than go through all that hassle to configure autofs, but it has its perks:
Imagine a scenario where a nfs share is supposed to be mounted at boot but it is not accessible; this will lead to delayed start up time or in worse case, the OS won't boot up at all.
Another advantage is that booting time is significantly reduced because no mounting is done at boot time. Therefore, autofs is the way to go if your server is frequently powered on/off and nfs share is accessed occasionally.
Also, failed mount requests can be reduced by assigning an alternate mount point as the source of a filesystem, so if there is a failure at one end, the mount point still works.
Configuring autofs...
The main configuration file for autofs is /etc/auto.master (aka the master map). Master map lists mount points handled by autofs on the system, and their corresponding configuration files.
Each entry in auto.master has three fields: the mount point, location of the map file, and the third field is optional and may contain information such as a timeout value as shown below:
Value | Description |
---|---|
mount-point | The mount point, /misc or /nfs, for example. |
map-name | The name of a map source which contains a list of mount points. /etc/auto.nfs |
options | Specify options such as timeout etc. |
In the following example I create auto.master file in /etc and add auto.nfs file as source to mount on /nfs. I’ve also set the timeout to 20s, which means any file system mounted on /nfs under with nfs.auto configuration will be unmounted after 20s inactivity.
# mkdir /nfs
# vi /etc/auto.master
/nfs /etc/auto.nfs --timeout=20
auto.nfs contains our actual configuration and would look like this:
# vi /etc/auto.nfs
sharefiles1 -fstype=nfs server1.domain.net:/work
sharefiles2 -fstype=nfs server2.domain.net:/mydocs
#systemctl reload autofs
# cd /nfs/sharefiles1
The directories from nfs sources should be mounted in two directories called sharefiles1 and sharefiles2 under /nfs.