Autofs and samba shares

Since we still have one windows computer at home (my wife’s laptop), my file server runs samba. Some days, I am fiddling with the file server and connections may get lost. Therefore having static mounts is inconvenient. Fortunately, during the Red Hat Certified Systems Administrator (RHCSA) course, I was pointed towards a nifty feature called the automounter and autofs. To use autofs with samba, we need the packages autofs and cifs-utils to be installed. After installing those, the first step is creating the credentials file for the samba share. Since we do not want everyone to know our passwords, we create a file called /etc/smbmount.cred with owner root and permissions set to 400.

user@host:~$ ls -al /etc/smbmount.cred
-r-------- 1 root root 0 MarĀ  5 16:45 smbmount.cred
user@host:~$ cat /etc/smbmount.cred
cat /etc/smbmount.cred: Permission denied
root@host:~$ cat /etc/smbmount.cred
username=$yourfileserverusername 
password=$yourfileserverpassword

Create a directory /etc/auto.master.d/, the contents of this directory are included by default in the autofs configuration. Therefore create a file called share.autofs in /etc/auto.master.d/

root@host:~$: echo "/mnt /etc/auto.share" > /etc/auto.master.d/share.autofs
root@host:~$: chmod 440 /etc/auto.master.d/share.autofs

This file is a reference to the autofs configuration file for each specific directory. In this case, there is only one directory to we need to create the file /etc/auto.share:

root@host:~$: echo "share -fstype=cifs,rw,credentials=/etc/smbmount.cred ://fileserver/fileshare" > /etc/auto.share

Once this is completed, we can restart autofs and the mount should work.

root@host:~$: systemctl restart autofs.service
root@host:~$: ls /mnt/share
fileshare-dir1 fileshare-dir2 fileshare-dir3 fileshare-dir4 lost+found temp

Of course, for obvious reasons, I have sanitized the output of some commands but this configuration is tested and works on Debian Jessie (8) with a Debian Jessie file server.