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.
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-utilsCreate the directory where the drive will be mounted:
sudo mkdir -p /MOUNT_POINTTo manually mount the network drive, follow these steps:
-
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,rwWhere:
//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.
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,rwTo automatically mount the network drive at system startup, follow these steps:
-
Edit the
fstabfile:sudo nano /etc/fstab
-
Add the following line at the end of the
fstabfile://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 -
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 anfsck(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.
//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
To avoid storing credentials directly in the fstab file, you can use a credentials file. Follow these steps:
-
Create a credentials file:
sudo nano /etc/samba/credentials
-
Add the credentials to the file:
username=USERNAME password=PASSWORD -
Ensure the file has the appropriate permissions:
sudo chmod 600 /etc/samba/credentials
-
Edit the
fstabfile:sudo nano /etc/fstab
-
Add the following line at the end of the
fstabfile://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 -
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 anfsck(file system check) on the share at startup.
//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
To unmount the network drive, use the following commands:
If you mounted the drive manually, you could unmount it with:
sudo umount /mnt/datastore-iaIf you mounted the drive automatically using fstab, you can unmount it with:
sudo umount /mnt/datastore-iaFollowing these steps, you can mount and unmount your network drive either manually or automatically and ensure that your credentials are stored securely.