So, I was working on a local project that required me to mount a persistent SMB network file share in Ubuntu. I'd completely forgotten any commands needed for this (I've not done a great deal of network files sharing in Linux) so I did the usual research workflow - Google. I found some great info and forum posts and found the info I was looking for (or so I thought). I had the commands and I was off.
However, when I'd done all the configuration, the network share would only mount as read only for me. I tried all sorts to resolve, checking the user rights on the network drive, checking the creds I'd used to connect. I even created another user account on the file server but every time the share was mounted as read only. I did notice that the share was mounted under the "root" user but I wasn't able to change the permissions, even as "root" - these are controlled by the file server!
I resorted back to Google and then found the answer - the command I was using was incomplete, there were a few more bits I needed to add. This wasn't that easy to find so I though I'd do a quick blog to have it all in one place:
System used for the demo: Ubuntu 22.04 (Jammy) Desktop VM, 2Gb Ram, 2 Cores, 25GB VHDD
First of all, do your homework:
On your File Server or NAS, Create or locate the share you want mounted - make a note!
Create Credentials for the share - I always use new credentials of each new system I share to - Make a note!
Decide where you're going to mount the drive, a popular place is /mnt/<sharename>
In my case, for this blog example the details were:
NAS share address: //10.1.1.52/Documents/Test
Credentials: username: rich / password: test123
Location to mount: /home/rich/share
You now need to install some software to enable Linux to communicate with the SMB share:
Make sure your repositories are up to date:
sudo apt update
Install cifs-utils:
sudo apt install cifs-utils
mkdir /home/rich/share
Create the credential file (and note the location):
sudo gedit .smbcredentials
Enter the username and password:
username=test
password=test123
Save the file and close the editor.
Change the permissions for the file:
chmod 600 .smbcredentials
Edit the /etc/fstab file:
This is where Ubuntu will take the information to mount the drive each time you boot / reboot the system.
sudo nano /etc/fstab
Scroll down to a new line with the cursor keys and enter:
//<serveraddress>/<sharename>/(folder structure if required) /<directory to mount in>/<directory> cifs credentials=/<full path to file>/.smbcredentials, rw, uid=<your system username>, iocharset=utf8 0 0
so my line looks like this:
//10.1.1.52/Documents/Test /home/rich/share cifs credentials=/home/rich/.smbcredentials,rw, uid=rich,iocharset=utf8 0 0
CTRL + X then Y and then Enter to save and close the file.
The moment of truth:
sudo mount -a
If all goes well you should see the mount appear on the task bar, the left hand menu of Files and the files in the shared folder:
Right clicking in the free space of the folder and selecting Properties shows the folder location and the Free Space (as 236.7GB) which is a lot more than the VM's hard drive. Now you can read and write to and from the folder as required.
Summing up:
In the code we put in the fstab file, the "rw" tells it the permissions we need (read/write); providing the "uid" tells it what user to mount the share for; using the .smbcredentials file after changing the permissions means that clear text creds are not in files readable by other users, keeping them more secure.
If you need to remove the share, it's as simple of deleting the line from the fstab file and rebooting.
Thanks for reading,
Rich.
Rich.