Table of Contents

About

Drives that are not essential to the operating system should preferably be mounted dynamically rather than using the good old /etc/fstab. In order to do this, there are multiple avenues to explore - most notably, the systemd way of mounting filesystems from fstab directly. However, there are situations where you would like to browse and automatically mount shares over the network on demand. For that, we have autofs which is a kernel-level filesystem module that is able to automatically mount devices and network shares on demand once a certain folder path is accessed.

Requirements

In order to use autofs, on Debian you would have to install the package autofs by issuing:

aptitude install autofs

and, in case you plan to mount samba network shares automatically, the smbclient has to be installed as well:

aptitude install smbclient cifs-utils

Creating Credentials for Samba Servers

In order to access Samba shares, most likely you will need some credentials, which can be set-up by creating files in /etc/creds named after the server you are connecting to. For example, if we would like to connect to the server deepfreeze.int, we would create the file /etc/creds/deepfreeze.int with the following contents:

username=login
password=passphrase
domain=WORKGROUP

where username, password and domain should be set accordingly for the Samba server on deepfreeze.int.

Setting-Up autofs to Mount a Network Samba Share

To mount network samba shares on demand using autofs, create the folder /etc/auto.master.d/:

makedir -p /etc/auto.master.d

and then place a file there called smb.autofs with the following contents:

/media/cifs  /etc/auto.smb --timeout=300 --ghost

This line configures a cifs-mounted shared directory at /media/cifs, that, when accessed, will call /etc/auto.smb. The mount timeout will be $300s = 5m$ and the directory will be ghosted such that if there is a disconnect the directory will still be left there.

The /etc/auto.smb file is provided by the autofs package. In case you have Apple Time Capsules on the network, it is a good idea to edit /etc/auto.smb and add the sec=ntlm option to the option list:

opts="-fstype=cifs,sec=ntlm"

Testing

After restarting autofs by issuing:

/etc/init.d/autofs restart

you should be able to access cifs shares. Following the previous example, we could issue:

cd /media/cifs/deepfreeze.int/Data

and the share should be mounted automatically. Furthermore, if we leave the directory, after a while the share should be unmounted.