How To Configure Autofs (Automount) In Linux
In this article, we are going to learn How To Configure Autofs (Automount) In Linux. Autofs also referred to as Automount is a nice feature in Linux used to mount the filesystems automatically on user’s demand. There are two ways available in Linux by which we can mount the file system i.e. /etc/fstab and another one is Autofs. /etc/fstab is used to mount the filesystems automatically at when the system boots up and Autofs is also doing the same thing.
Difference Between /etc/fstab and Autofs (AutoMount)
You might think that if both are doing the same thing then why to use Autofs (Automount) instead of /etc/fstab. and what is the difference between /etc/fstab and Autofs? Here I am going to explain to you what is the exact difference between /etc/fstab and Autofs.
As we know that /etc/fstab is used for permanent mounting of file systems but it will be useful only if you have fewer mount points connected to your /etc/fstab file but if you are working on a large organization and have so many mount points linked to your /etc/fstab file then in that case your overall system’s performance will be get affected.
But Autofs mounts the file systems on user’s demand. By default the mount point’s configured in Autofs is in the unmounted state till the user access the mount point, once a user tries to access the mount point it will mount automatically and if the user doesn’t use the mount point for some time then it will automatically go to unmount state.
For example, if we have Two NFS (Network File System) exported share drives and configured in Autofs to automatically mount in a directory named /myautoshares for users. In that case till the user didn’t access the mountpoint it will be on unmounted state and you unable to see any mounted filesystem in /myautoshares directory, once the user access the mount point it will automatically get mounted and will be in mount state till user uses that mount point. once the user stops using that mount point it will unmount automatically. Here we are using Autofs to mount the NFS exported shares automatically.
Note: We can set time for after what time the filesystem unmount automatically once the user stops using the mount point.
Follow the below steps to configure Autofs
Step: 1 Requires Packages
Before we start the configuration of Autofs we have to install required packages. The main package we have to install is autofs.xxx.xxx.rpm. you can check if the packages are already installed or not by using the below command.
# rpm -qa | grep -i autofs
Install the autofs packages by using the yum command as shown below.
# yum -y install autofs # Install Autofs Package Loaded plugins: fastestmirror, langpacks base | 3.6 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 updates/7/x86_64/primary_db | 2.2 MB 00:08 Loading mirror speeds from cached hostfile * base: centos.myfahim.com * extras: centos.myfahim.com * updates: centosc6.centos.org Resolving Dependencies --> Running transaction check ---> Package autofs.x86_64 1:5.0.7-56.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: autofs x86_64 1:5.0.7-56.el7 base 782 k Transaction Summary ================================================================================ Install 1 Package Total size: 782 k Installed size: 4.9 M Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Warning: RPMDB altered outside of yum. ** Found 1 pre-existing rpmdb problem(s), 'yum check' output follows: ipa-client-4.1.0-18.el7.centos.0.1.x86_64 has missing requires of autofs Installing : 1:autofs-5.0.7-56.el7.x86_64 1/1 Verifying : 1:autofs-5.0.7-56.el7.x86_64 1/1 Installed: autofs.x86_64 1:5.0.7-56.el7 Complete!
As we can see the autofs package installed successfully, we can confirm the autofs packages installation status by using the below command.
# rpm -qa | grep -i autofs autofs-5.0.7-56.el7.x86_64
Step : 2 Configure /etc/auto.master
Here we are going to use NFS exported shares with Autofs to automatically mount it. To check the NFS exports just run the below command.
# showmount -e 192.168.0.104 Export list for 192.168.0.104: /packages * /database *
As we can see above we have two NFS exports i.e. /package and /database. Now create a directory for Autofs to mount the filesystems.
[root@oracledb ~]# mkdir /myautoshares/
The main configuration files of Autofs is /etc/auto.master where we can link the file in which we are going to mention all NFS mount points. The syntax to enter in /etc/auto.master file is shown below :
<Mount-Point> <File where we mention the Filesystems> <Time after which the filesystem will Unmount>
Note: It is not mandatory to mention timeout syntax in /etc/auto.master configuration file but It is recommended to do so.
The configuration part is Highlighted in Blue Color.
[root@oracledb ~]# nano /etc/auto.master # # 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 ### My AutoShares ### /myautoshares /etc/auto.sharedfs --timeout=35
- /myautoshares – This is the directory where Autofs will mount the filesystems
- /etc/auto.sharedfs – In this file, we have to mention the file systems which will automatically mounted by Autofs
- –timeout=35 – This is states that if the user will don’t use the mount point for 35 Seconds, then it will be unmounted automatically.
Step : 3 Create and Configure /etc/auto.sharedfs
Now we have to create /etc/auto.sharedfs file which we have mentioned in /etc/auto.master file to mention the file systems.
Just copy the /etc/auto.misc file as /etc/auto.sharedfs as shown below. /etc/auto.misc file is the sample file which is created automatically after autofs package installation.
[root@oracledb ~]# cp /etc/auto.misc /etc/auto.sharedfs
Now just edit the /etc/auto.sharedfs file and mention the filesystems to be a mount, the Syntax is shown below :
<Name of the Share> <File System> <NFS Share Path/Drive Path/Syntax>
Note: Configuration Part is Highlighted in Blue Color.
[root@oracledb ~]# nano /etc/auto.sharedfs # # This is an automounter map and it has the following format # key [ -mount-options-separated-by-comma ] location # Details may be found in the autofs(5) manpage cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom # the following entries are samples to pique your imagination #linux -ro,soft,intr ftp.example.org:/pub/linux #boot -fstype=ext2 :/dev/hda1 #floppy -fstype=auto :/dev/fd0 #floppy -fstype=ext2 :/dev/fd0 #e2floppy -fstype=ext2 :/dev/fd0 #jaz -fstype=ext2 :/dev/sdc1 #removable -fstype=ext2 :/dev/hdd packages -fstype=nfs 192.168.0.104:/packages database -fstype=nfs 192.168.0.104:/database
We are done with the configuration part, now let’s go ahead and start the autofs service using the below command.
[root@oracledb ~]# systemctl start autofs.service
Now it’s time for testing. So let’s list our Autofs mount point i.e. /myautoshares using ls -l command.
[root@oracledb ~]# ls -l /myautoshares/ total 0
As we can see above currently there is no active mounts are available because as I explained earlier till the user doesn’t access the mount it will be in the unmount state.
Now let’s go ahead and access our first NFS export i.e. /packages by using below command.
[root@oracledb ~]# cd /myautoshares/packages/ [root@oracledb packages]# ls file1.txt file2.txt file3.txt file4.txt file5.txt
As we can see above we able to access the NFS mount successfully. Now let’s again list our mount point i.e. /myautoshares and I am sure it will definitely show the NFS mount points shared by Autofs. Refer to the Output below.
[root@oracledb ~]# ls -l /myautoshares/ total 0 drwxrwxrwx. 2 root root 86 Feb 1 22:45 packages
Now let’s access our second NFS export i.e. /database.
[root@oracledb ~]# cd /myautoshares/database [root@oracledb database]# ls test1.txt test2.txt test3.txt test4.txt test5.txt
As we can see in the output below we able to access both NFS Exported shares.
[root@oracledb database]# ls -l /myautoshares/ total 0 drwxrwxrwx. 2 root root 86 Feb 1 21:42 database drwxrwxrwx. 2 root root 86 Feb 1 22:45 packages
Now if the user doesn’t use access to this NFS shares for 35 Seconds it will be automatically unmounted.
That’s all, In this article, we have explained How To Configure Autofs (Automount) In Linux. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.