×
Auto mount filesystem using AutoFS
Blog

Auto mount filesystem using AutoFS

Autofs is used to automatically mount any filesystem on demand as and when you access them and not only it will mount automatically but it can automatically unmount any filesystem when not in used for a particular predefined timeout value.

One drawback to using /etc/fstab is that, regardless of how infrequently a user accesses the NFS or any other mounted file system, the system must dedicate resources to keep the mounted file system in place. This is not a problem with one or two mounts, but when the system is maintaining mounts to many systems at one time, overall system performance can be affected. An alternative to /etc/fstab is to use the kernel-based automount utility.

Installation:

On Ubuntu/Debian

root@node-4:~# apt-get install autofs

On RHEL/CentOS

root@node-4:~# yum install autofs

Configuration:

Config files:

/etc/auto.master --> Master config file
/etc/auto.misc   --> Map file

Setup:

Create a directory  to mount AutoFS filesystems

root@node-4:~# mkdir /automnt

Add below entries In /etc/auto.master file

Syntax:

<mount point> <map file> <options>

root@node-4:~# cat /etc/auto.master
/automnt /etc/auto.misc --timeout=30

Here --timeout is optional.  The default timeout is 300 seconds and the file /etc/auto.misc is a default map file. You can also use  custom map file instead.

Now add below entries in /etc/auto.misc file.

Syntax:
<mount-point> [<options>] <location>

root@node-4:~# cat /etc/auto.misc
zaheer -fstype=nfs 192.168.2.45:/mynas

Here zaheer is mount point that will be mounted automatically.   192.168.2.45 is NFS server and 192.168.2.45:/mynas is location of the NFS export.

Now restart the autofs service to take effect the changes done.

root@node-4:~# service autofs restart

Testing Autofs:

root@node-4:~# cd /automnt/zaheer

Now check by executing df -h

root@node-4:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 283M 4.0K 283M 1% /dev
tmpfs 59M 292K 59M 1% /run
/dev/sda1 9.9G 1.5G 7.9G 16% /
none 100M 0 100M 0% /run/user
192.168.2.45:/mynas 9.8G 2.1G 7.2G 23% /automnt/zaheer

After 20 seconds of inactivity, it will be unmounted automatically.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.