Auto-mount network shares (cifs, sshfs, nfs) on-demand using autofs



At work I usually have to connect to several servers. Some are Windows Servers, some are Linux Servers. On my local Linux machines (running Kubuntu 17.10 at the time writing this) I usually used /etc/fstab entries. However, the fstab way does not mount on boot and always needs manual re-mounting. I was told that there have been times in which automatic mounting during boot using fstab was working but I never managed to get it working although I tried several mount options like _netdev and others. Since I often have to re-mount the network shares (whenever there was a network disconnect), an option to automatically re-connect and mount the folders on boot was highly sought for.


Recently I came across autofs. It basically mounts the network shares on-demand (when the folder is accessed) for a desired time. If there was no activity during that time the shares are unmounted again. This gives a practical feeling of networks shares which are mounted at boot.

After installation of autofs using your favorite package manager, you will see an /etc/auto.master file. This file holds the main settings.

Mine looks as follows:

# /etc/auto.master
/- /etc/auto.all --timeout=60 --ghost

The first option /- sets the root directory as an universal mount point. Other attempts as suggested in various examples1 2 did not work for me.

Next, you need to edit the specified file from above (here /etc/auto.all). It will hold the specific mount instructions for each protocol. You can either use multiple entries in /etc/auto.master with subsequent multiple /etc/auto.* files or use a single file for various protocols (e.g. cifs, sshfs, nfs).

I prefer to use a single file that looks as follows:

/mnt \
/server1 -fstype=cifs,rw,credentials=/etc/.smbcredentials.txt,uid=1000,file_mode=0775,dir_mode=0775,users ://<server ip>/<share> \
/server2 -fstype=fuse,rw,allow_other,uid=1000,users :sshfs\#<user>@<server ip>\:/<share>


Be aware that only the direct mount point (here /mnt) exists permanently. The on-demand folders (here e.g. /mnt/server1) will be created during the mount process and should not exist before.

  1. ↩︎

  2. ↩︎