Skip to content

NFS mount using Redhat autofs

  •  
  • 4 min read
  • 8,288 reads

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

Questions about your own setup? A dbi services consultant can look at your case.

Talk to an expert (opens in a new tab)
# 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

Let’s work together

Ready to operate at
the next level?

Tell us about your challenge. We’ll bring the depth, the expertise, and the Swiss precision.

Joan Frey

Joan Frey

Joan Frey has more than 8 years of experience in the IT sector, including 5 years as a database and Linux consultant. He is specialized in PostgreSQL high availability (Patroni, etcd, HAProxy, Keepalived) and also manages MariaDB and MySQL environments. His knowledge includes Linux (RHEL, SLES), Windows Server, backup and restore strategies (pgBackRest, mariabackup), Ansible automation, as well as development in Python, Go, TypeScript and SQL. Since 2021, he has been working at dbi services, where he installs, supports and documents database infrastructures for customers, often under SLA contracts, and coaches their internal teams. He also contributes to internal tools such as DMK (Database Management Kit), the Resource Planning ERP and the Yak cloud tool. Before joining dbi services, he was an Analyst Developer Intern at Euro Information Développement (France) in Strasbourg. His branch related experience includes banking, fintech, payment services, telecommunications, aviation, watchmaking, public administration, energy, insurance, pharmaceuticals and public transport.View Author posts

28 posts since February 2022

Tags:

Most read in Operating System

The Operating System posts our readers open most: the all-time favourites, and what is trending right now.

Leave a Comment

Your email address will not be published. Required fields are marked *