Ubuntu: Mount/Unmount network share

Ubuntu: Mount/Unmount network share

April 23, 2025 Configuration Linux Server System Administration 0

🔗 Mount and Unmount a Network Share (SMB)

📋 Prerequisite:

On most Linux distributions, you need to install the cifs-utils package to mount SMB (Windows) shares:

sudo apt update
sudo apt install cifs-utils

🔧 Manually Mounting a Share

  1. Create a mount point (if it doesn’t already exist):
sudo mkdir -p /mnt/example
  1. Mount the SMB share (replace with your actual server and credentials):
sudo mount -t cifs //server-ip-or-hostname/share-name /mnt/example -o username=yourusername,password=yourpassword

Tip: For better security, use a credentials file instead of putting the username and password directly in the command.

  1. Unmount the share:
sudo umount /mnt/example

⚙️ Automatically Mount at Boot

To mount the share automatically on system startup, add it to the /etc/fstab file:

  1. Edit /etc/fstab:
sudo nano /etc/fstab
  1. Add the following line to the bottom (customize with your values):
# Mount SMB share at boot
//server-ip-or-hostname/share-name /mnt/example cifs credentials=/home/youruser/.smbcredentials,iocharset=utf8,uid=1000,gid=1000,file_mode=0777,dir_mode=0777 0 0

Create a .smbcredentials file in your home directory with:

username=yourusername
password=yourpassword

And secure it:

chmod 600 ~/.smbcredentials
  1. Reload fstab to test it:
sudo mount -a

If there are no errors, the share should now be mounted and will automatically mount at boot.