Ubuntu: Mount/Unmount network share
🔗 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
- Create a mount point (if it doesn’t already exist):
sudo mkdir -p /mnt/example
- 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.
- 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:
- Edit
/etc/fstab
:
sudo nano /etc/fstab
- 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
- 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.