Automount NFS share in Linux using autofs

Channel: Linux
Abstract: /dbstuff /etc/auto.nfsdb --timeout=180 Save & exit the file. Create the mount point. [email protected]db_backup is a mount point. -fstype=nfs is t

Autofs is a service in Linux like operating system which automatically mounts the file system and remote shares when it is accessed. Main advantage of autofs is that you don’t need to mount file system at all time, file system is only mounted when it is in demand.

Autofs service reads two files Master map file ( /etc/auto.master ) and a map file like /etc/auto.misc or /etc/auto.xxxx.

In ‘/etc/auto.master’ file we have three different fields :

/<Mount-Point>     <Map-file>     <Timeout-Value>

In map file (/etc/auto.misc or /etc/auto.xxxx) also we have three different fields:

<Mount-Point>    <Mount-Options>        <Location_of_File System>

In this article we will mount the NFS share using autofs. NFS share ‘/db_backup‘ is exported from Fedora NFS Server (192.168.1.21). We are going to mount this nfs share on CentOS 7 & Ubuntu Linux using autofs.

Steps to mount nfs share using Autofs in CentOS 7 Step:1 Install autofs package.

Install the autofs package using below yum command if it is not installed.

[[email protected] ~]# rpm -q autofs
package autofs is not installed
[[email protected] ~]# yum install autofs
Step:2 Edit the Master map file (/etc/auto.master )

Add the following line .

[[email protected] ~]# vi /etc/auto.master
/dbstuff  /etc/auto.nfsdb  --timeout=180

Note : Mount point ‘/dbstuff’‘ must exist on your system. If not then create a directory ‘mkdir /dbstuff‘. NFS Share will automatically umount after 180 seconds or 3 minutes if don’t perform any action on the share.

Step:2 Create a map file ‘/etc/auto.nfsdb’

Create a map file and add the following line.

[[email protected] ~]# vi /etc/auto.nfsdb
db_backup  -fstype=nfs,rw,soft,intr  192.168.1.21:/db_backup

Save and exit the file.

Where :

  • db_backup is a mount point.
  • -fstype=nfs is the file system type & ‘rw,soft,intr’ are mount options.
  • ‘192.168.1.21:/db_backup’ is nfs share location.
Step:3 Start the auotfs service.
[[email protected] ~]# systemctl start autofs.service
[[email protected] ~]# systemctl enable autofs.service
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'
[[email protected] ~]#
Step:3 Now try to access the mount point.

Mount point of nfs share will be ‘/dbstuff/db_backup’. When we try access the mount point then autofs service will mount nfs share automatically.

Steps to mount NFS share using autofs in Ubuntu Linux. Step:1 Install the autofs package using apt-get command.
[email protected]:~$ sudo apt-get install autofs
Step:2 Edit the Master Map file ‘/etc/auto.master’

Add the following line in the master map file.

[email protected]:~$ sudo vi /etc/auto.master
/dbstuff   /etc/auto.nfsdb   --timeout=180

Save & exit the file.

Create the mount point.

[email protected]:~$ sudo mkdir /dbstuff
[email protected]:~$
Step:2 Create a map file ‘/etc/auto.nfsdb’.

Add the following line in the map file.

[email protected]:~$ sudo vi /etc/auto.nfsdb
db_backup   -fstype=nfs4,rw,soft,intr   192.168.1.21:/db_backup
Step:3 Start the autofs service.
[email protected]:~$ sudo /etc/init.d/autofs start
Step:4 Try to access the mount point.

Ref From: linuxtechi

Related articles