Automatically mounting a Samba share at boot on Debian

First you’ll need somewhere safe to put the username & password. We’ll store them in /etc/sambapasswords, and make that only readable by root.

sudo mkdir /etc/sambapasswords
sudo chmod 700 /etc/sambapasswords

Now create a file for the server itself:

sudo vim /etc/sambapasswords/myserver

Enter the credentials in this format:


(Note: The maximum password length is 16 characters. I spent ages trying to debug it when I was using a randomly generated 20 character password!)

And make sure the file itself is secure, just in case:

sudo chmod 600 /etc/sambapasswords/myserver

Create the directory where it should be mounted:

sudo mkdir /mnt/myserverfiles

Now edit /etc/fstab:

sudo vim /etc/fstab

And add the following line:

//myserver/path/to/files /mnt/myserverfiles smbfs credentials=/etc/sambapasswords/myserver 0 0

Finally tell it to mount the newly added share now (so you don’t have to reboot to test it / start using it):

sudo mount -a