A Quick Primer on Autofs
As mentioned in my last blog post, I setup an autofs share to mount my NAS for backups. Since I’ve always used fstab in the past to mount this, and it’s quite unreliable for cifs shares, and some internet articles go into way more detail than necessary on setting up autofs, here’s a very quick overview on setting up samba / CIFS shares with autofs on Raspberry Pi OS (or any other Debian / Ubuntu based system). I tested this on Raspberry Pi OS (formerly Rasbian) Buster.
Install Autofs⌗
First you need the package. Often a good idea to reboot after doing things with apt.
sudo apt-get install autofs
sudo shutdown -r now
Add a samba shares file to the master autofs file⌗
Edit the autofs master file:
sudo nano /etc/auto.master
Add a line pointing to a (yet non existant) samba config file
/- /etc/auto.smb.shares --timeout 15 browse
Brekaing down this command:
- /- indicates that the following file should be mounted relative to / (i.e. all paths in the file are absolute, if you put /mnt here, then everything in auto.smb.shares would be mounted below /mnt)
- /etc/auto.smb.shares is the file we will create to list our SMB shares
- timeout is how long to keep the shares mounted
- browse seems to work well
Add a new samba shares file and add a share to it⌗
Edit the samba shares file we pointed to earlier
sudo nano /etc/auto.smb.shares
Add your new mount point
/mnt/data -fstype=cifs,rw,username=<user>,password=<pass>,credentials=<file>,noperm,dir_mode=0777,file_mode=0777 ://server/share
Not all of these options should be used all the time.
- /mnt/data - the path on the local system to mount to
- -fstype=cifs - always needed for samba shares
- rw - enable read/write access to the share
- username=<user> - put the username here or use a credentials file
- password=<password> - put the password here or use a dredentials file
- credentials=<file> - Alternative to putting the uname/password here, point to a file that has the credentials
- noperm - ignore permissions for local users accessing the share
- dir_mode / file_mode - default UNIX permissions for directories and files on the Windows share
- ://server/share - you can put in an IP or hostname, and the share can be multiple folders deep
If you created a credentials file, it needs to look like this:
username=<user>
password=<pass>
Make sure the mount point exists
sudo mkdir /mnt/data
Restart autofs so it finds the new config entries⌗
sudo systemctl restart autofs
Try to access the share⌗
ls /mnt/data
That’s It! Have fun. If you’re having trouble, make sure you rebooted after installing everything, otherwise you might not have the cifs kernel module loaded.