Linux, Windows, Virtualization, OpenSource & Blogging

How to Configure Autofs on CentOS 7 / Ubuntu 16.04 / Fedora 27/26

6

Autofs is an automounter utility that helps you to automatically mount NFS share when a file or directory is requested on the NFS mount point. Auto-mounts are mounted only when they are accessed and are unmounted after a period of inactivity.

In the last tutorial we used /etc/fstab to mount a filesystem automatically across the system reboot, but here we will use autofs to mount the same NFS share.

AutoFS very useful in case if the client server has to mount many mounts at one time, overall boot up time will be increased; with the autofs we can speed up the bootup time.

When the static mount (/etc/fstab) is used, regardless of how infrequently a user accesses the NFS mounted file system, the system must dedicate resources to keep the mounted file system in place. Autofs solves the problem of keeping the filesystem mounted that are not frequently accessed by the users by unmounting it after a period of inactivity. Because of this, automounting NFS/Samba shares conserves bandwidth and offers better overall performance compared to static mounts via fstab.

Autofs can also be used to mount other file systems including AFS, SMBFS, CIFS, and local file systems.

This guide helps you to install and configure Autofs on CentOS 7 / Fedora 27 / 26 / Ubuntu 16.04 / 14.04. The same steps will apply to previous versions of CentOS, Fedora, and Ubuntu.

Environment

Here are my demo nodes detail, I have both NFS and Client server in the same distribution.

NFS Server Hostname: server.itzgeek.local (CentOS 7 / Fedora 27 / Ubuntu 16.04)

NFS Server IP Address: 192.168.12.5/24

Share Path: /nfsfileshare

NFS Client Hostname: client.itzgeek.local (CentOS 7 / Fedora 27 / Ubuntu 16.04)

NFS Client IP Address: 192.168.12.7/24

Autofs mount path: /autofs/nfsfileshare

Other HowTo’s that you would require to setup autofs.

Install Autofs

Let’s install autofs package, and it is available in the base repository. Install it using the following command in terminal.

### CentOS 7 / Fedora 26 ###

# yum -y install autofs

### Ubuntu 16.04 / 14.04 ###

$ sudo apt-get install autofs

Configure Autofs

The main configuration file for Autofs is the /etc/auto.master file,  referred as the master map. The master map file contains list of a mount and the location of its map.

Sample output of /etc/auto.master, you can see the mount and its map shown in “Green“.

#
# 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).
#
/misc   /etc/auto.misc
#
# 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.
#
/net    -hosts
#
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
#
+dir:/etc/auto.master.d
#
# 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
# precedence.
#
+auto.master

Edit /etc/auto.master

As per our scenario, we are going to mount NFS share on /autofs/nfsfileshare using Autofs. The following step creates a mount point at /autofs and configures it according to the settings specified in /etc/auto.nfs (which we will create in the next step).

### CentOS 7 / Fedora 26 ###

# vi /etc/auto.master

### Ubuntu 16.04 / 14.04 ###

$ sudo nano /etc/auto.master

Add the following line at the end of /etc/auto.master file. The default timeout for autofs is 300 seconds (5 minutes). After five minutes of inactivity, a filesystem that is mounted will be automatically unmounted.

Note: It is optional to define the timeout in /etc/auto.master file.

/autofs   /etc/auto.nfs --timeout=60

Create /etc/auto.nfs

Now, we will create the file which contains our automounter map.

### CentOS 7 / Fedora 26 ###

# vi /etc/auto.nfs

### Ubuntu 16.04 / 14.04 ##

$ sudo nano /etc/auto.nfs

This file should contain a separate line for each NFS share. The format for a line is {mount point} [{mount options}] {location}. The mount points specified here will be relative to the mount point given in /etc/auto.master.

nfsfileshare        -fstype=nfs4,rw,soft,intr        192.168.12.5:/nfsfileshare

Where,

nfsfileshare : Mount Point

-fstype=nfs4,rw,soft,intr : Mount options

192.168.12.5:/nfsfileshare : Share location

This creates a new mount point at /autofs/nfsfileshare/ and mounts the NFS “/nfsfileshare” directory exported by “192.168.12.5

Restart the autofs service and enable it to start at system startup.

### CentOS 7 / Fedora 26 ###

# systemctl restart autofs
# systemctl enable autofs

### Ubuntu 16.04 / 14.04 ##

$ sudo service autofs restart
$ sudo update-rc.d autofs defaults

Testing Autofs

Now, try accessing “/autofs/nfsfileshare” directory.

Note: Make sure you unmount the static mounts and remove the entry from /etc/fstab file.

$ ll  /autofs/nfsfileshare

total 0
drwxrwxrwx 4 root root 33 Sep 17 17:47 ./
drwxr-xr-x 3 root root  0 Sep 19 01:23 ../
drwxr-xr-x 2 root root  6 Sep 17 17:39 1/
drwxrwxr-x 2 raj  raj   6 Sep 17 17:45 2/
-rw-rw-r-- 1 raj  raj   0 Sep 17 17:47 test

You would see the content of NFS shared directory. Also, use “df” command to see the mounted file systems, you could see the NFS share that is mounted automatically.

Note: Below is the sample out of “df” command (Ubuntu 16.04).

$ df -hT

Filesystem                 Type      Size  Used Avail Use% Mounted on
/dev/sda1                  ext4       98G  3.7G   89G   4% /
none                       tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
udev                       devtmpfs  478M  4.0K  478M   1% /dev
tmpfs                      tmpfs      98M  1.5M   97M   2% /run
none                       tmpfs     5.0M     0  5.0M   0% /run/lock
none                       tmpfs     489M  144K  489M   1% /run/shm
none                       tmpfs     100M   32K  100M   1% /run/user
192.168.12.5:/nfsfileshare nfs4       50G  858M   50G   2% /autofs/nfsfileshare

To test the unmount, do not perform any action on the mounted file system for 5 minutes (In my case 1 minute as i set –timeout=60). The autofs will automatically unmount the filesystem once the inactivity time has passed.

Note: Please do not even try to use “df” command to check the mounted filesystems as “df” command may access the mounted filesystems which will cause a delay in unmount.

Debugging and Troubleshooting

CentOS 7 / Fedora 27

Note: Make sure you have LOGGING=”debug” in “/etc/sysconfig/autofs

# cat /var/log/messages

Sep 19 13:14:20 client automount[1891]: expire_proc_indirect: expire /autofs/nfsfileshare
Sep 19 13:14:20 client automount[1891]: handle_packet: type = 4
Sep 19 13:14:20 client automount[1891]: handle_packet_expire_indirect: token 4, name nfsfileshare
Sep 19 13:14:20 client automount[1891]: expiring path /autofs/nfsfileshare
Sep 19 13:14:20 client automount[1891]: umount_multi: path /autofs/nfsfileshare incl 1
Sep 19 13:14:20 client automount[1891]: umount_subtree_mounts: unmounting dir = /autofs/nfsfileshare

If you see the output marked in “Green“, autofs unmounted the NFS share after the timeout.

Ubuntu 16.04/14.04

$ sudo service autofs stop

$ sudo automount -f -v

Try to access the share in another terminal, and go through the output of the above command on the first terminal.

Starting automounter version 5.0.7, master map /etc/auto.master
using kernel protocol version 5.02
lookup(dir): dir map /etc/auto.master.d missing or not readable
lookup(file): failed to read included master map dir:/etc/auto.master.d
lookup(file): failed to read included master map auto.master
mounted indirect on /autofs with timeout 60, freq 15 seconds
attempting to mount entry /autofs/nfsfileshare
 mounted /autofs/nfsfileshare
1 remaining in /autofs
1 remaining in /autofs
1 remaining in /autofs
1 remaining in /autofs
expiring path /autofs/nfsfileshare
 expired /autofs/nfsfileshare

If you see the output marked in “Green“, autofs automatically mounted the filesystem on your access and unmounted it after the timeout.

That’s All. You have successfully mounted NFS mount via autofs.

You might also like
  • Iain

    You should probably make a note to install nfs-common first for those connecting for the first time.

  • is autos good for a share that you want to always be mounted? including if the share goes offline having it automatically remounted when it comes back online?

    • Raj

      Autofs is used for auto mounting shared filesystem when it has been accessed. It never do remount on due to offline. There is no option in linux to remount when share comes online.

      • Quan

        You can run crontab to remount in period lol

        • Raj

          Yes, ofcourse you can do that. I mean there is no inbuilt option.

How to Configure Autofs on CentOS 7 / Ubuntu 16.04 / Fedora 27/26

6