Autofs also referred to as automount is a feature in Linux like systems that automatically mount filesystems on demand. In Linux, we mount filesystems using two ways: in /etc/fstab
and using austofs. Using /etc/fstab
has to be updated everything a new mount point is configured and it ensures persistent mounting after system reboots.
What is the difference between /etc/fstab
and Autofs? It seems as if both /etc/fstab
and Autofs and doing automatic mounting and so why would one prefer autofs to /etc/fstab
?
/etc/fstab
is used to ensure persistent mounting of filesystems. However, this works effectively only with few mount points. The more the mount points linked to /etc/fstab, the less the effective it becomes. In such a case, autofs comes handy to ensure that filesystems are mounted only when needed. The mount points configured in autofs are by default in an unmounted stated. They will only be automatically mounted when a user tries to access the filesystem.
Advantages of autofs
Autofs has quite a number of advantages over /etc/fstab
as stated below:
- Filesystems are not mounted on boot thus saving boot time.
- Users do not need to know the server root password to keep mounting and unmounting file systems.
- Network traffic is reduced since filesystems are only mounted on demand.
Autofs reads from two files: master map (/etc/auto.master
) and a map file (/etc/auto.xxxx
).
/etc/auto.master
has the following fields:
/<Mount-Point>Â Â Â <Map-file> Â <Timeout-Value>
Map file (/etc/auto.misc
or /etc/auto.xxxx
) also we have three different fields:
<Mount-Point> Â Â <Mount-Options>Â Â Â Â <Location_of_File System>
Configuring Autofs in CentOS | Ubuntu | Debian
In my example, I have an nfs share, /backup that has been exported from nfs server with IP 192.168.50.2. We are going to mount it on /nfs_share on CentOS and Ubuntu/Debian client servers using autofs. We first need to install autofs on CentOS Linux and Ubuntu/Debian as below:
On CentOS, run the below command:
sudo yum -y install autofs
On Ubuntu/ Debian, install autofs as below:
sudo apt update
sudo apt-get -y install austofs
Once autofs is installed, the following procedure applies to CentOS, Ubuntu and Debian client machines.
Configure master and map files
Use your preferred editor to open and edit the master file as below:
$ sudo vim /etc/auto.master
/nfs_share /etc/auto.nfsdb --timeout=180
Where /nfs_share
is the mount point on our client. Autofs checks from the master file to determine the defined mount points. Create a map file /etc/auto.nfsdb
and add the contents as shown:
$ sudo vim /etc/auto.nfsdb
backup -fstype=nfs4,rw,soft,intr 192.168.50.2:/backup
The first field here is a subdirectory created dynamically by automount and should not actually exist on the nfs client. In this case, /backup exported from the nfs server will be mounted on /nfs_share/backup
on the clients.
Now start and enable autofs service as below:
$ sudo /etc/init.d/autofs start
Starting autofs (via systemctl): autofs.service.
$ sudo systemctl enable autofs.service
Now confirm that you can access the exported directory from the client. Remember that before you access the directory, it will be in unmounted state but it will be mounted immediately you make a request to use the filesystem. Create some test files on the nfs server shared directory and check whether you can see them on the client. For my case, I have created test2, test3, test4
$ cd /nfs_share/backup
$ ls
test2 test3 test4
This shows that our autofs is working as expected.
Direct and Indirect Autofs Mapping
With autofs, you can either use direct or indirect mapping.
- Direct mapping: absolute path mounting
- Indirect mapping: relative path mounting
For example, I have /mnt/backup
exported from an nfs server, 192.168.50.2. I want to mount it to /mnt/nfs_share
on the client.
Direct Mapping
If I am using direct mounting, these are how my configurations will look like:
The master file for direct mapping:
$ sudo vim /etc/auto.master
/- /etc/auto.nfsdb
The map file:
$ sudo vim /etc/auto.nfsdb
/mnt/nfs_share -fstype=nfs 192.168.50.2:/mnt/backup
Indirect Mapping
For indirect mapping, the master file:
$ sudo vim /etc/auto.master
/mnt /etc/auto.nfsdb
And the map file:
$ sudo vim /etc/auto.nfsdb
nfs_share -fstype=nfs 192.168.50.2:/mnt/backup
That’s it. You have successfully configured autofs mounting on CentOS | Ubuntu | Debian. Enjoy your usage and check below more interesting Linux Guides: