Connect shares on a as-need basis using autofs

autofs is a program for automatically mounting directories on an as-needed basis. Auto-mounts are mounted only as they are accessed, and are unmounted after a period of inactivity. Mounting a remote share through fstab remains mounted unless you unmount it. Automounting NFS/Samba shares conserves bandwidth and offers better overall performance compared to static mounts through fstab. Furthermore fstab might cause problems if the remote share becomes unavailable, resulting in stale mounts. For example, the file server you are connecting to might crash or the network might go down.

Install autofs

To start, first install autofs through.

For Ubuntu

sudo apt-get install autofs

For Red Hat, Fedora and CentOS

sudo yum install autofs

Select or create a mount point

Create a mount point or choose an existing folder (such as /mnt or /media) to act as the mount point.

A mount point is where you want your shared folder to appear. Generally on Windows based machines, they would create a new drive such as a D drive or Z drive.

To create a mount point, simply create a new folder where you wish your mounts to be e.g. /mymount, /home/calvin/networkmounts, etc.

Mount point configuration

You will need to define mount points to indicate how to connect to your network shares.

Create a new file in /etc/. I like to name this after my mount point e.g. if I chose /mnt I would name it auto.mnt but if I chose /mymount I would use auto.mymount.

Edit the master autofs configuration file at the bottom

sudo nano /etc/auto.master

Add the mountpoint to the bottom

/mnt   /etc/auto.mnt

Now create the auto.mnt file

sudo nano /etc/auto.mnt

There are several types of mounts and each differs in their configuration.

SMB

Add the following line to your auto.mnt file

folder   server:/

Where 'folder' indicates the folder to be created under /mnt to host your shares. e.g.

qnapfs  192.168.0.2://

NFS

Add the following line to your auto.mnt file

folder -fstype=nfsv4  server:/

Where 'folder' indicates the folder to be created under /mnt to host your shares. e.g.

qnapfs -fstype=nfsv4 192.168.0.2://

The client needs the same changes to /etc/default/nfs-common to connect to an NFSv4 server. In /etc/default/nfs-common we set:

NEED_IDMAPD=yes
NEED_GSSD=no # no is default

CIFS

Add the following line to your auto.mnt file.

folder -fstype=cifs,rw,noperm,username=myuser,password=mypass ://example.com/shrname

If you are using a domain, specify it like so:

folder -fstype=cifs,rw,noperm,username=myuser,domain=domain.com,password=mypass ://example.com/shrname

Check your share

Reboot and attempt to access your share.

ls /mnt/folder
cd /mnt/folder

If it does not work, you will have to debug it

  1. Stop the autofs daemon
sudo service autofs stop
  1. Run automount in the foreground with verbose information
sudo automount -f -v
  1. From another terminal, try to mount your file-systems by changing directories into the mountpoint.
  2. Check the output from the first terminal for clues as to why the mount failed or was not attempted.

Sources