Skip to content

Instantly share code, notes, and snippets.

@rferreiraperez
Last active July 30, 2024 08:46
Show Gist options
  • Save rferreiraperez/ddc66bb218616d3fc3bfab4e79d7cbdd to your computer and use it in GitHub Desktop.
Save rferreiraperez/ddc66bb218616d3fc3bfab4e79d7cbdd to your computer and use it in GitHub Desktop.

Mounting a Network Drive on Ubuntu

This document provides the necessary steps to mount a network drive on Ubuntu, both manually using the mount command and automatically using fstab. Additionally, the possibility of using secure credentials with fstab is included.

Prerequisites

Before proceeding with manual or automatic mounting, you need to update the system and install the necessary utilities:

sudo apt update
sudo apt install cifs-utils

Create the directory where the drive will be mounted:

sudo mkdir -p /MOUNT_POINT

Manual Mounting

To manually mount the network drive, follow these steps:

  1. Execute the following command:

    sudo mount -t cifs //SERVER_ADDRESS/SHARE_NAME /MOUNT_POINT -o vers=VERSION,username=USERNAME,password='PASSWORD',gid=GROUP_ID,uid=USER_ID,file_mode=FILE_MODE,dir_mode=DIR_MODE,rw

    Where:

    • //SERVER_ADDRESS/SHARE_NAME: Server address and share name.
    • /MOUNT_POINT: Directory where the share will be mounted.
    • vers=VERSION: CIFS protocol version to use (e.g., 3.0).
    • username=USERNAME: Username to access the share.
    • password='PASSWORD': Password to access the share.
    • gid=GROUP_ID: Group ID that will have access to the share.
    • uid=USER_ID: User ID that will have access to the share.
    • file_mode=FILE_MODE: File permissions (e.g., 0777).
    • dir_mode=DIR_MODE: Directory permissions (e.g., 0777).
    • rw: Mount the share in read/write mode.

Example

sudo mount -t cifs //10.0.0.238/ia/data-analytics /mnt/datastore-ia -o vers=3.0,username=rferreira,password='rferreira',gid=1001,uid=1001,file_mode=0777,dir_mode=0777,rw

Automatic Mounting

To automatically mount the network drive at system startup, follow these steps:

  1. Edit the fstab file:

    sudo nano /etc/fstab
  2. Add the following line at the end of the fstab file:

    //SERVER_ADDRESS/SHARE_NAME /MOUNT_POINT cifs vers=VERSION,username=USERNAME,password=PASSWORD,gid=GROUP_ID,uid=USER_ID,file_mode=FILE_MODE,dir_mode=DIR_MODE,rw 0 0
    
  3. Mount all drives specified in fstab:

    sudo mount -a

Where:

  • //SERVER_ADDRESS/SHARE_NAME: Server address and share name.
  • /MOUNT_POINT: Directory where the share will be mounted.
  • vers=VERSION: CIFS protocol version to use (e.g., 3.0).
  • username=USERNAME: Username to access the share.
  • password='PASSWORD': Password to access the share.
  • gid=GROUP_ID: Group ID that will have access to the share.
  • uid=USER_ID: User ID that will have access to the share.
  • file_mode=FILE_MODE: File permissions (e.g., 0777).
  • dir_mode=DIR_MODE: Directory permissions (e.g., 0777).
  • rw: Mount the share in read/write mode.
  • 0 0: Prevents the system from attempting an fsck (file system check) on the share at startup.

Note: Setting 0 0 at the end of the fstab entry is crucial to prevent the system from trying to perform a file system check (fsck) on the network drive during startup. Network shares do not support fsck and attempting to do so can cause system boot delays or failures.

Example

//10.0.0.238/ia/data-analytics /mnt/datastore-ia cifs vers=3.0,username=rferreira,password=rferreira,gid=1001,uid=1001,file_mode=0777,dir_mode=0777,rw 0 0

Using Secure Credentials with fstab

To avoid storing credentials directly in the fstab file, you can use a credentials file. Follow these steps:

  1. Create a credentials file:

    sudo nano /etc/samba/credentials
  2. Add the credentials to the file:

    username=USERNAME
    password=PASSWORD
    
  3. Ensure the file has the appropriate permissions:

    sudo chmod 600 /etc/samba/credentials
  4. Edit the fstab file:

    sudo nano /etc/fstab
  5. Add the following line at the end of the fstab file:

    //SERVER_ADDRESS/SHARE_NAME /MOUNT_POINT cifs vers=VERSION,credentials=/etc/samba/credentials,gid=GROUP_ID,uid=USER_ID,file_mode=FILE_MODE,dir_mode=DIR_MODE,rw 0 0
    
  6. Mount all drives specified in fstab:

    sudo mount -a

Where:

  • //SERVER_ADDRESS/SHARE_NAME: Server address and share name.
  • /MOUNT_POINT: Directory where the share will be mounted.
  • vers=VERSION: CIFS protocol version to use (e.g., 3.0).
  • credentials=/etc/samba/credentials: Path to the credentials file.
  • gid=GROUP_ID: Group ID that will have access to the share.
  • uid=USER_ID: User ID that will have access to the share.
  • file_mode=FILE_MODE: File permissions (e.g., 0777).
  • dir_mode=DIR_MODE: Directory permissions (e.g., 0777).
  • rw: Mount the share in read/write mode.
  • 0 0: Prevents the system from attempting an fsck (file system check) on the share at startup.

Example

//10.0.0.238/ia/data-analytics /mnt/datastore-ia cifs vers=3.0,credentials=/etc/samba/credentials,gid=1001,uid=1001,file_mode=0777,dir_mode=0777,rw 0 0

Unmounting the Network Drive

To unmount the network drive, use the following commands:

Manual Unmounting

If you mounted the drive manually, you could unmount it with:

sudo umount /mnt/datastore-ia

Automatic Unmounting

If you mounted the drive automatically using fstab, you can unmount it with:

sudo umount /mnt/datastore-ia

Following these steps, you can mount and unmount your network drive either manually or automatically and ensure that your credentials are stored securely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment