Skip to content

Instantly share code, notes, and snippets.

@ParkWardRR
Created October 27, 2023 22:24
Show Gist options
  • Select an option

  • Save ParkWardRR/713c539b6a087e7e200283da1e19ccd2 to your computer and use it in GitHub Desktop.

Select an option

Save ParkWardRR/713c539b6a087e7e200283da1e19ccd2 to your computer and use it in GitHub Desktop.

Revisions

  1. ParkWardRR created this gist Oct 27, 2023.
    51 changes: 51 additions & 0 deletions mount_cifs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/bin/bash

    # Description:
    # This script helps to mount a CIFS network share on Ubuntu/Mint/Debian OS

    # Attempt to mount a network share using cifs protocol.
    # Takes a server share address and target mount point as input, and writes the values to /etc/fstab

    # Update the .smbcredentials file with CIFS username and password
    # Template for .smbcredentials:
    # username=<cifsusername>
    # password=<password>

    # Variables here
    # cifs host1: //smb_server_name/share_name
    # Please replace smb_server_name and share_name with your server name and share name respectively.

    cifs_host="//smb_server_name/share_name"

    # cifs host mountPoint1: mount_point_name
    # Replace mount_point_name with the name where you want your network share to be mounted.

    mount_point="/mnt/mount_point_name"

    # Ask user to update .smbcredentials file with their respective CIFS username and password
    echo "Editing .smbcredentials file, please enter your credentials (CIFS username and password)."
    sudo nano /root/.smbcredentials

    # Create a Mount point Directory using variable mount_point
    if [ ! -d $mount_point ]; then
    sudo mkdir -p $mount_point
    fi

    # Change permissions to root only access for .smbcredentials as it has sensitive data
    sudo chmod 700 /root/.smbcredentials

    # Install cifs-utils if not installed
    sudo apt-get install cifs-utils -y

    # Add the CIFS entry to /etc/fstab
    echo '# CIFS mount for network share' | sudo tee --append /etc/fstab > /dev/null
    echo "${cifs_host} ${mount_point} cifs credentials=/root/.smbcredentials,iocharset=utf8,_netdev,file_mode=0777,dir_mode=0777 0 0" | sudo tee --append /etc/fstab > /dev/null

    # Mount all filesystems mentioned in /etc/fstab
    echo "Mounting all filesystems..."
    sudo mount -a
    # Display all mounted filesystems with their sizes
    echo "Displaying details of all mounted filesystems..."
    df -h

    # End of the script. You can save this script with a .sh extension. Suggested filename: mount_cifs.sh