Mounting a Samba Share Under Debian

In this article we’ll permanently mount a Samba share on a Debian based system, this process will be similar for other Linux distributions too. It is assumed that you have a share somewhere else already configured and ready to go.

Start by installing the required utilities, this will install a few other utilities as well.

sudo apt install cifs-utils

Now create a folder to act as the mount point for the shared drive. Where you put these is up to you, but I generally put mine under the /mnt directory. Strictly speaking /mnt is for temporarily mounted filesystems but the filesystem hierarchy standard doesn’t really provide a good location for permanently mounted shares that don’t have an obvious location elsewhere (although /srv looks like a possible candidate). For Docker installs I’ve take to creating a root directory called /data and a subdirectory /data/mnt, I find this works well enough.

sudo mkdir /mnt/music 

Create a credentials file in your home directory that will contain the login details needed to mount the remote share. Root will be mounting the share so place the file in root’s home directory. I name the file with the server name first which is helpful if there are multiple sets of credentials stored.

sudo nano /root/.fileserver_smbcredentials 

The credentials file should contain just two lines:

username=samba_username
password=samba_password

Now test mount the share by issuing the command below.

You may or may not need all the options that are specified here. The uid and gid flags tell the mount system to set the user and group for the mount. In my case I need to write to the share as a particular user and group since that is what my file server is expecting. I needed to add the lxc-users group to the machine I was creating the mount on and then add the user to the group. If you don’t add the uid and gid settings you’ll find the you can only write to the share as root.

The dir_mode and file_mode settings tell the mount system what the default permissions should be, I want files and directories writeable by the whole group. You could probably also use the noperm option but that removes all permissions checks. I also needed to use the fully qualified name of the file server, I must have forgotten to set up search domains.

sudo mount -t cifs -o rw,vers=3.0,credentials=/root/.fileserver_smbcredentials,dir_mode=0775,file_mode=0775,uid=1000,gid=9999 //fileserver.example.co.uk/music /mnt/music

If that works you can unmount the path again with:

sudo umount /mnt/music

If the manual mounting works you can add the same settings to /etc/fstab to make the mount permanent. Note that the order of the settings is somewhat different in the fstab file and the entry shown should all appear on a single line.

sudo nano /etc/fstab 
//fileserver.example.co.uk/music /mnt/music cifs rw,vers=3.0,credentials=/root/.fileserver_smbcredentials,dir_mode=0775,file_mode=0775,uid=1000,gid=9999

When you’re done save the file and reboot the machine. If the settings are all correct you should find the share mounted after the restart.

References