autofs is a nice traditional way to mount the nfs directories on-demand on the nfs clients. was originally used to mount the user home directories to the clients to make them sort of roaming user profiles, but this doesnt stop us from using it to configure nfs mounts in other ways. using autofs saves unnecessary resource allocation to mount the resources on a client machine, which may never be accessed in that session. it also saves the open handles on the nfs server, which is another plus.
ps: the examples below have been done on a centos 8.3 gnu/linux machine. adjust the path locations accordingly as per your flavor of gnu/linux. rest of the options should be good.
dnf -y install autofs
define the mounts in the master configuration file named auto.master in /etc
explanation with example
nfs server ip: 188.8.131.52
nfs export: /nfs-mount-00
client ip: 184.108.40.206
client directory where we need to mount the nfs share: /nfs-mount-client
step 1: add the map file path in auto.master
the text i added to the auto.master has been bolded for easy reading below
[root@centos-c ~]# cat /etc/auto.master
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
/nfs-mount-client /etc/auto.nfs-mount-client --ghost
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
# If you have fedfs set up and the related binaries, either
# built as part of autofs or installed from another package,
# uncomment this line to use the fedfs program map to access
# your fedfs mounts.
#/nfs4 /usr/sbin/fedfs-map-nfs4 nobind
# Include central master map if it can be found using
# nsswitch sources.
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
step 2: define the map file (added above)
[root@centos-c ~]# cat /etc/auto.nfs-mount-client
data -fstype=nfs 220.127.116.11:/nfs-mount-00
step 3: reload the autofs daemon
systemctl reload autofs.service
that it! now when you browse to the mounted directory on the nfs client, the nfs will be mounted automatically.
Good things to know
- the default settings in autofs do not show the mounted directories. this might give you the illusion that the directory is not mounted at all even when you are in the mount point! you need to remember the name of the directory to cd into that. if you dont want to remember all this, then you may use the –ghost option in the map file options defined in the auto.master configuration file, as shown in the example above. i prefer that and like to see the mounted directories.
- another way would be to turn on the browse_mode in the autofs.conf file. it is the same as adding the –ghost option manually.
- if you’re using a systemd based linux, you might not be interested in autofs at all! yeah, use the systemd automount instead. it is built in to the system already and no more remembering the autofs mapping and stuff. no installing the additional autofs package either.