First of all, why would you use autofs over nfs mount in the fstab ?

  • Shares are accessed automatically and transparently when a user tries to access files or directories under the designated mount point of the remote filesystem to be mounted.
  • Shorter booting time because mounting isn’t done at boot time.
  • Less permanently active mount points.
  • No booting issue if the shares are inaccessible.

1. Installation

1.1 Create the VMs

Use one VM as the NFS server and the other one as the NFS client.

I’m currently using two VM setup with Redhat 8.5 and two network adapter : NAT and Host-Only.

1.2 Setup NFS

1.2.1 For both servers

Install NFS

# yum install -y nfs-utils

Enable and start rpcbind

# systemctl enable --now rpcbind

Check if it’s working

# systemctl status rpcbind

Allow NFS services in the firewall

# firewall-cmd --add-service={nfs,rpc-bind,mountd} --permanent
success
# firewall-cmd --reload
success

1.2.2 NFS server

Enable and start nfs-server

[root@nfs-server ~]# systemctl enable --now nfs-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.

Check if it’s working

[root@nfs-server ~]# systemctl status nfs-server.service

Create NFS shared directory

[root@nfs-server ~]# mkdir /nfs-export

Create a file into the directory

[root@nfs-server ~]# touch /nfs-export/testfile.txt
[root@nfs-server ~]# ll /nfs-export/
total 0-rw-r--r--. 1 root root 0 May 17 15:07 testfile.txt

Add the directory that you want to access from the nfs-client to the /etc/exports file and the client’s IP address (or Hostname if there is a dns or if it’s registered in /etc/hosts) :

• in /etc/hosts

192.168.56.102 nfs-client

• in /etc/exports

/nfs-export nfs-client(rw,sync,no_root_squash)

Apply all the exports in /etc/exports and check what is exported

[root@nfs-server ~]# exportfs -a
[root@nfs-server ~]# exportfs
/nfs-export nfs-client

1.2.3 NFS client

Verify that the NFS shared directory exported from nfs-server is seen by the client

[root@nfs-client ~]# showmount -e 192.168.56.103
Export list for 192.168.56.103:
/nfs-export nfs-client

Create a mount point

[root@nfs-client ~]# mkdir /mount-nfs

Mount the NFS shared directory manually to test if it works

[root@nfs-client ~]# mount -t nfs 192.168.56.103:/nfs-export /mount-nfs/
[root@nfs-client ~]# ls /mount-nfs/
testfile.txt

Unmount it

[root@nfs-client ~]# umount /mount-nfs
[root@nfs-client ~]# ls /mount-nfs/
[root@nfs-client ~]#

1.3 Mounting NFS shared directory with autofs

Install autofs

[root@nfs-client ~]# yum install -y autofs

1.3.1 Direct mapping

Create the master map file. It’s located in /etc/auto.master.d/ and Its name has to be like whatever.autofs. It will reference a map file.

[root@nfs-client ~]# vim /etc/auto.master.d/dbi.autofs
/- /etc/auto.dbi

Create the map file

[root@nfs-client ~]# vim /etc/auto.dbi
/mount-nfs -rw,sync 192.168.56.103:/nfs-export

Enable and start autofs

[root@nfs-client ~]# systemctl enable --now autofs
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.

Check if the NFS shared file is mounted

[root@nfs-client ~]# ll /mount-nfs/
total 0
-rw-r--r--. 1 root root 0 May 17 15:07 testfile.txt

1.3.2 Indirect mapping

Create the master map file. It’s located in /etc/auto.master.d/ and Its name has to be like whatever.autofs. It will reference a map file.

[root@nfs-client ~]# vim /etc/auto.master.d/dbi.autofs
/mount-nfs /etc/auto.dbi

Create the map file (here we are mapping the folder of /nfs-export/)

[root@nfs-client ~]# vim /etc/auto.dbi
* -rw,sync 192.168.56.103:/nfs-export/&

Enable and start autofs

[root@nfs-client ~]# systemctl enable --now autofs
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.

Check if the NFS shared files are mounted  (Shared directory has to be discovered manually with full path before you can see it)

[root@nfs-client ~]# ll /mount-nfs/
total 0

[root@nfs-client ~]# ll /mount-nfs/nfs-1
total 0
-rw-r--r--. 1 root root 0 Jun 21 08:29 nfs1.txt

[root@nfs-client ~]# ll /mount-nfs/nfs-2
total 0
-rw-r--r--. 1 root root 0 Jun 21 08:29 nfs2.txt

[root@nfs-client ~]# ll /mount-nfs/
total 0
drwxr-xr-x. 2 root root 22 Jun 21 08:29 nfs-1
drwxr-xr-x. 2 root root 22 Jun 21 08:47 nfs-2

[root@nfs-client ~]# ll /mount-nfs/*
/mount-nfs/nfs-1:
total 0
-rw-r--r--. 1 root root 0 Jun 21 08:29 nfs1.txt

/mount-nfs/nfs-2:
total 0
-rw-r--r--. 1 root root 0 Jun 21 08:29 nfs2.txt

2. Conclusion

Hope this guide help, either for you personal purpose or for your EX200 Red Hat Certified System Administrator Exam.

3. Annexes

Information from RHCSA preparation pdf

Other useful guides and information :

https://learn.redhat.com/t5/Platform-Linux/Difference-between-Direct-and-Indirect-Automount/td-p/20869