Linux - Mounting a NAS cifs share.

Written below are the manual steps followed by a bash script that can be used to automate the task.

Manual steps.

  1. Install CIFS package

Run:

sudo apt-get install cifs-utils
  1. Create a directory where you will mount the NAS share. Create one for each share you will mount.
    For example

    sudo mkdir /mnt/FOLDER_NAME
    
  2. Create a hidden file in /home/LOCAL_USER. The file name will be used in /etc/fstab later.

  • In a terminal session run the following command (don't forget the "." before the file name):

    vim .FILE_NAME 
    
  • Add the detail of the user account that will access the share. The credentials file format should look like the example below

  • Save the file and quit out of VIM.

    username=USER_NAME
    password=PASSWORD
    
  1. Edit /etc/fstab using an editor, like VIM.
  • From a terminal session run: sudo vim /etc/fstab/
  • Add the following lines to the bottom of your fstab file. make sure to edit your specific details.
  • after updating the fstab file, save and quit VIM. //192.168.0.100/share01 /mnt/share01 cifs credentials=/home/LOCAL_USER/.creds,gid=1000,uid=1000,vers=2.0 //192.168.0.100/share02 /mnt/share02 cifs credentials=/home/LOCAL_USER/.creds,gid=1000,uid=1000,vers=2.0
  • After updating /etc/fstab run the command below from a terminal session to mount the shares
sudo mount -a

Bash script.

Follow these step to create the script file and enable its execution.

Copy the text below in to a file using any text editor. Add your specific details, such as user name and folder names. Save the file with any name, but make sure the file extension is .sh.

#!/bin/sh
# Mount NAS cif shares
# Created: 
# Tested: Ubuntu 20.04

# Install CIFS package
apt-get install cifs-utils

# Creates mount points. The directory names after '/mnt/' can be of your choosing
mkdir /mnt/share01
mkdir /mnt/share02

# Create hidden file in /home/**<user_name>**. The file name will be used in the lines added to /etc/fstab later.
echo 'username=USERNAME_TO_ACCESS_SHARE\npassword=PASSWORD_FOR_USER' >/home/LOCAL_USER/.creds

# Add share locations to /etc/fstab
echo '//192.168.0.100/share01 /mnt/share01 cifs credentials=/home/LOCAL_USER/.creds,gid=1000,uid=1000,vers=2.0\n//192.168.0.100/share02 /mnt/share02 cifs credentials=/home/LOCAL_USER/.creds,gid=1000,uid=1000,vers=2.0' >>/etc/fstab

# mount shares
mount -a

To run the script you must first change the mode to allow you to execute the script file. To do this you must run the following command in a terminal session

chmod +x YOUR_FILE_NAME.sh

Now you can execute the script. From within the directory where your file is located run the following

sudo ./YOUR_SCRIPT_NAME.sh

#linux #shares #ubuntu


You'll only receive email when they publish something new.

More from tengo
All posts