How to Mount NFS Shares using Automounter in CentOS/RHEL

Mounting NFS Shares with the Automounter

The automounter is a service (autofs) that automatically mounts NFS shares “on-demand,” and will automatically unmount NFS shares when they are no longer being used.

Automounter Benefits

  • Users do not need to have root privileges to run the mount and umountcommands.
  • NFS shares configured in the automounter are available to all users on the machine, subject to access permissions.
  • NFS shares are not permanently connected like entries in /etc/fstab, freeing network and system resources.
  • The automounter is configured on the client side; no server-side configuration is required.
  • The automounter uses the same options as the mount command, including security options.
  • The automounter supports both direct and indirect mount-point mapping, for flexibility in mount-point locations.
  • autofs creates and removes indirect mount points, eliminating manual management.
  • NFS is the default automounter network file system, but other network file systems can be automatically mounted.
  • autofs is a service that is managed like other system services.

Create an automount

Configuring an automount is a multiple step process:

1. Install the autofs package.

[[email protected] ~]$ sudo yum install autofs

This package contains everything needed to use the automounter for NFS shares.

2. Add a master map file to /etc/auto.master.d. This file identifies the base directory used for mount points and identifies the mapping file used for creating the automounts.

[[email protected] ~]$ sudo vim /etc/auto.master.d/demo.autofs

The name of the master map file is arbitrary (although typically meaningful), but it must have an extension of .autofs for the subsystem to recognize it. You can place multiple entries in a single master map file; alternatively, you can create multiple master map files each with its own entries grouped logically.

Add the master map entry, in this case, for indirectly mapped mounts:

/shares  /etc/auto.demo

This entry uses the /shares directory as the base for indirect automounts. The /etc/ auto.demo file contains the mount details. Use an absolute file name. The auto.demo file needs to be created before starting the autofs service.

3. Create the mapping files. Each mapping file identifies the mount point, mount options, and source location to mount for a set of automounts.

[[email protected] ~]$ sudo vim /etc/auto.demo

The mapping file-naming convention is /etc/, where name reflects the content of the map.

work  -rw,sync  serverb:/shares/work

The format of an entry is mount point, mount options, and source location. This example shows a basic indirect mapping entry. Direct maps and indirect maps using wildcards are covered later in this post.

- Known as the key in the man pages, the mount point is created and removed automatically by the autofs service. In this case, the fully qualified mount point is /shares/work (see the master map file). The /shares directory and the /shares/work directories are created and removed as needed by the autofs service.

In this example, the local mount point mirrors the server’s directory structure, however this is not required; the local mount point can be named anything. The autofs service does not enforce a specific naming structure on the client.

- Mount options start with a dash character (-) and are comma-separated with no white space. Mount options available to a manual mounting of a file system are available when automounting. In this example, the automounter mounts the share with read/write access (rw option), and the server is synchronized immediately during write operations (sync option).

Useful automounter-specific options include -fstype= and -strict. Use fstype to specify the file system type, for example, nfs4 or xfs, and use strict to treat errors when mounting file systems as fatal.

- The source location for NFS shares follows the host:/pathname pattern; in this example, serverb:/shares/work. For this automount to succeed, the NFS server, serverb, must export the directory with read/write access and the user requesting access must have standard Linux file permissions on the directory. If serverb exports the directory with read/only access, then the client will get read/only access even though it requested read/ write access.

4. Start and enable the automounter service. Use systemctl to start and enable the autofs service.

[[email protected] ~]$ sudo systemctl enable --now autofs
Created symlink /etc/systemd/system/ → / usr/lib/systemd/system/autofs.service.

Direct Maps

Direct maps are used to map an NFS share to an existing absolute path mount point. To use directly mapped mount points, the master map file might appear as follows:

/-  /etc/

All direct map entries use /- as the base directory. In this case, the mapping file that contains the mount details is /etc/ The content for the /etc/ file might appear as follows:

/mnt/docs  -rw,sync  serverb:/shares/docs

The mount point (or key) is always an absolute path. The rest of the mapping file uses the same structure. In this example the /mnt directory exists, and it is not managed by autofs. The full directory /mnt/docs will be created and removed automatically by the autofs service.

Indirect Wildcard Maps

When an NFS server exports multiple subdirectories within a directory, then the automounter can be configured to access any one of those subdirectories using a single mapping entry.

Continuing the previous example, if serverb:/shares exports two or more subdirectories and they are accessible using the same mount options, then the content for the /etc/auto.demo file might appear as follows:

*  -rw,sync  serverb:/shares/&

The mount point (or key) is an asterisk character (*), and the subdirectory on the source location is an ampersand character (&). Everything else in the entry is the same.

When a user attempts to access /shares/work, the key * (which is work in this example) replaces the ampersand in the source location and serverb:/shares/work is mounted. As with the indirect example, the work directory is created and removed automatically by autofs.