I recently had to mount a samba share on Ubuntu 20.04 server on a Raspberry Pi 4, and found that piecing things together from examples was a bit of a pain

Installing the required packages

Installing the required packages is easy, as they are available in the default installed repositories in Ubuntu and most other Linux distributions

sudo apt install smbclient cifs-utils

Editing fstab

To automatically mount the share on boot, the following line must be added to the end of /etc/fstab, with placeholders replaced:

//<samba_server_ip>/<share_name> /mnt/<share_name> cifs vers=3.0,credentials=/home/<user>/.smb,uid=<user_id>,gid=<group_id>,x-systemd.automount,noperm,noatime

With example values:

//192.168.0.42/storage /mnt/mystorage cifs vers=3.0,credentials=/home/myuser/.smb,uid=1000,gid=1000,x-systemd.automount,noperm,noatime

Flags used:

  • vers=3.0 specifies the version of the samba (and SMB) protocol to use
  • uid and gid specify the user and group to use when mounting the share
  • noperm instructs the samba client to skip permission checks on client-side, as that is expected to be done by the server
  • noatime instructs the samba client to not update the access time of accessed files, to avoid needless writes
  • x-systemd.automount tells systemd to mount the share on first use instead of straight away on boot, so network can be established properly first

Samba credentials

Instead of putting the credentials in the fstab file, which every user can read, you can put them in a file that only your user can read. For example, this file could be placed in /home/<user>/.smb with the following content:

user=<samba_username>
password=<samba_password>
domain=WORKGROUP

Mounting the share

To mount the share without rebooting, you can run the following command:

sudo mount -a

You can then try listing the contents of the share with ls -l /mnt/<share_name>