How to Mount NFS Filesystems with autofs on Ubuntu 20.04

laptop 4662049 640

This brief tutorial shows students and new users how to mount NFS filesystems with autofs automounting tool on Ubuntu 20.04 | 18.04.

Linux allows you to mount filesystems statically in the /etc/fstab file. However, with the approach, the mount point is always mapped even when you’re not using the mapped resource.

To improve automounting of NFS/Samba shares, a tool called autofs was created. This allows automounting of NFS/Samba shares to conserve bandwidth and offer better overall performance compared to mounting statically.

autofs is a program for automatically mounting directories on an as-needed basis. This is good in that it will only mount resource when needed which improves systems’ performance.

To get started with autofs, follow the steps below:

Install autofs on Ubuntu

Before you can start using autofs, make sure it’s installed in Ubuntu. If not, run the commands below to install it.

sudo apt update
sudo apt-get install autofs

Now that autofs is installed, continue below to use it.

autofs uses a configuration file as the framework for completing tasks. It looks up a map file located at /etc/auto.master by default.

The lines in auto.master describes a mount and the location of its map.

auto.master file field:

  • Mount point
  • Location of map file
  • Map optional

Auto mounting NFS shares

For this tutorial, we’re going to be using autofs to mount a directory or folder via NFS on our remote server.

The first thing we’ll do is enter a mount point for our NFS share on our local server at /mynfs and configures it according to the settings specified in /etc/auto.nfs.

Run the commands below to open the auto.master file

sudo nano /etc/auto.master

Then add the line below at the end of the file to define the /mynfs share on the local host.

/mynfs   /etc/auto.nfs

Save the file and exit.

Next, run the commands below to create the file which contains our automounter map we specified above.

sudo nano /etc/auto.nfs

This file should contain a separate line for each NFS share if you’re going to be defining multiple NFS shares.

The format for a line is {mount point} [{mount options}] {location}.

So to mount a remote server to our /mynfs path on the local host, add this line in the /etc/auto.nfs file.

/mynfs   -fstype=nfs4   ubuntu2004:/remote_path

If your NFS shares use NFSv4, you need to tell autofs about that.

When you’re done editing the files, run the commands below to reload autofs configurations.

sudo service autofs reload

After reloading, check the service status.

sudo service autofs status

It should display similar line as shown below:

● autofs.service - Automounts filesystems on demand
     Loaded: loaded (/lib/systemd/system/autofs.service; enabled; vendor preset>
     Active: active (running) since Sun 2021-01-17 13:30:49 CST; 20min ago
       Docs: man:autofs(8)
    Process: 5569 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUC>
   Main PID: 3856 (automount)
      Tasks: 4 (limit: 4654)
     Memory: 1.4M
     CGroup: /system.slice/autofs.service
             └─3856 /usr/sbin/automount --pid-file /var/run/autofs.pid

Jan 17 13:30:49 ubuntu2004 systemd[1]: Starting Automounts filesystems on deman>

That should do it.

Conclusion:

This post showed you how to use autofs to mount NFS shares on Ubuntu for better improvement over static mount.

If you find any error above, please use the form below to report.

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

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