On Demand CIFS Network Shares On Your Pi with autofs

Why autofs?

autofs is great for mounting shares from networks that aren’t always available – particularly at boot time if you have to wait for a network device to initialise or a VPN to establish a connection.

Installation
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install autofs
Configuration

autofs is configured by editing the Master Map File located at /etc/auto.master. Here is the default file. Add the following line, replacing <YOUR USERNAME> with your username:

/home/<YOUR USERNAME>/shares /home/<YOUR USERNAME>/.auto.shares

Then create the corresponding files in your home directory. The .auto.shares file must not be executable.

mkdir ~/shares
chmod 700 ~/shares
mkdir ~/.credentials
chmod 700 ~/.credentials
touch ~/.auto.shares
chmod 600 .auto.shares
nano ~/.auto.shares

Then create and edit your cifs share, here is an example:

myshare -fstype=cifs,credentials=/home/<YOUR USERNAME>/.credentials/.example.com.credentials,uid=<YOUR USERNAME>,gid=<YOUR USERNAME>,dir_mode=0770,file_mode=0770,noperm ://example.com/myshare

Then add your credentials required to access the network share in the file referenced above:

nano ~/.credentials/example.com.credentials

Add your credentials like this:

username=<YOUR NETWORK USERNAME>
password=<YOUR NETWORK PASSWORD>

Reload your service.

sudo service autofs reload

You should now be able to cd into your share directory which will be mounted on demand.

Issues

If you experience any issues shut down the daemon and manually run automount.

sudo service autofs stop
sudo automount -f -v

Now you will see any errors logged to the console.


Leave a Reply

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