Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Mounting Samba (CIFS) Share In Linux

This tutorial will show you how to mount a samba/cifs share using either a single BASH command for a temporary mount, or permanently by adding an entry to /etc/fstab.

BASH Command

If you wish to mount a share temporarily, then use a command/script like below:

USERNAME="programster"
PASSWORD="putYourPasswordHere"
USER_ID=1000
GROUP_ID=$USER_ID
SAMBA_SERVER_IP="192.168.1.2"
NAME_OF_SHARE="projects"
MOUNT_PATH="/mnt/projects"

sudo mount -t cifs \
  -o username=$USERNAME,password=$PASSWORD,uid=$USER_ID,gid=$GROUP_ID \
  //$SAMBA_SERVER_IP/$NAME_OF_SHARE \
  $MOUNT_PATH

The $NAME_OF_SHARE might not be the same as the name of the folder on the server that is acting as the share. This will often be the cause of a lot of pain. The name is whatever is specified in your Samba config file, or the "name of share" when selecting "share this folder" in windows. If you find that you need to provide the domain, then add it to the -o options like so: -o username=$USERNAME,domain=$DOMAIN,password=$PASSWORD,uid=$USER_ID,gid=$GROUP_ID

Read-Only Access Issue - Specify UID/GID

When I first looked into performing the mounting, I didn't specify the uid or gid parameters and only had read access (no write). After specifying the UID and GID of my user on my local machine

To find out your user ID, just run:

echo $UID

If you want to find the ID of another user, use:

id -u $USERNAME

Using Fstab

Create Credentials File

The first thing we need to do is create a locked down file that will contain the credentials for the samba share:

touch $HOME/.smbcredentials
chmod 0600 $HOME/.smbcredentials

Fill it with the relevant settings:

username=myUsername
password=myPassword
domain=domain_or_workgroupname

Domain may not be required.

Update fstab

Update your fstab

sudo editor /etc/fstab

Add a line similar to below (you will need to swap out the details as appropriate, such as the IP address and mount point etc)

//192.168.0.1/share-name    /where/to/mount    cifs    credentials=/home/username/.smbcredentials,uid=shareUserId,gid=shareGroupId,_netdev,x-systemd.automount,x-systemd.idle-timeout=60s     0     0
  • The _netdev mount option specifies that this is a network device, and thus needs to wait for the network to be available before trying to mount when booting the machine.

Debugging

If you get the message similar to below:

mount: /where/to/mount: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.

... then all you need to do is install the cifs-utils package:

sudo apt update \
  && sudo apt install cifs-utils -y

References

Last updated: 27th October 2023
First published: 17th October 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch