2 min read

Creating credential files for automatic Samba mounts in /etc/fstab

It’s handy to have credentials for each Samba server so you don’t have public passwords in /etc/fstab for the Samba file systems you want to mount on boot. Here’s how I do it:

  1. Create a directory /etc/samba/credentials

  2. Create a file /etc/samba/credentials/myserver

  3. In the myserver file, put the credentials for that server:

    username=myusername
    password=mypassword
    
    

    Note: spaces are important here - don’t use “ = ”, use ”=”.

  4. chown -R root.root /etc/samba/credentials

  5. chmod 700 /etc/samba/credentials

  6. chmod 600 /etc/samba/credentials/myserver

Once this is done, you can add lines for each mount of that server to your /etc/fstab:

//myserver/music        /music  smbfs   credentials=/etc/samba/credentials/myserver 0 0
//myserver/shared       /shared smbfs   credentials=/etc/samba/credentials/myserver 0 0

Note that the file in /etc/samba/credentials doesn’t have to be named the same thing as the server name - it just makes it easier.

This also survives the Ubuntu upgrade process, so you just have to update /etc/fstab if you upgrade to a new release, and can keep the same credentials files.