Mount Samba Share on Ubuntu using CIFS Protocol
This guide demonstrates how to mount Samba (SMB/CIFS) network shares on Ubuntu Linux systems. CIFS (Common Internet File System) is the standard protocol for accessing Windows file shares and Samba servers from Linux.
Understanding CIFS and Samba
CIFS (Common Internet File System) is a network file-sharing protocol based on SMB (Server Message Block). It allows Linux systems to mount remote Windows or Samba shares as if they were local directories. The cifs-utils package provides the necessary tools for mounting CIFS shares on Linux.
Prerequisites
Before mounting Samba shares, ensure the CIFS utilities are installed:
1sudo apt update2sudo apt install cifs-utilsBasic Temporary Mount
To mount a Samba share temporarily (until reboot), create a mount point and use the mount command:
1mkdir /mnt/samba2sudo mount -t cifs //192.168.0.5/sataDrive /mnt/samba -o username=usr,password=123Command breakdown:
-t cifs- Specifies the filesystem type as CIFS//192.168.0.5/sataDrive- The remote share path (server IP/hostname and share name)/mnt/samba- Local mount point directory-o username=usr,password=123- Mount options for authentication
Secure Credentials Management
For better security, avoid storing passwords in plain text. Use a credentials file instead:
1# Create a secure credentials file2sudo nano /etc/samba/credentials3
4# Add your credentials:5username=usr6password=1237domain=WORKGROUP1# Secure the credentials file (readable only by root)2sudo chmod 600 /etc/samba/credentials3sudo chown root:root /etc/samba/credentials1# Mount using the credentials file2sudo mount -t cifs //192.168.0.5/sataDrive /mnt/samba -o credentials=/etc/samba/credentials,uid=1000,gid=1000Permanent Mount Configuration
To automatically mount the Samba share at boot, add an entry to /etc/fstab:
1sudo nano /etc/fstab2//192.168.0.5/sataDrive /mnt/samba cifs username=usr,password=123,uid=1000,gid=1000,rw,iocharset=utf8,vers=3.0 0 0Understanding fstab Mount Options
| Option | Description |
|---|---|
uid=1000 | Sets the owner UID (use id -u to find yours) |
gid=1000 | Sets the owner GID (use id -g to find yours) |
rw | Mount as read-write (use ro for read-only) |
iocharset=utf8 | Character encoding for file names |
vers=3.0 | SMB protocol version (2.0, 2.1, 3.0, 3.1.1) |
0 0 | Dump and fsck options (always 0 for network shares) |
Recommended fstab Entry with Credentials File
For improved security and reliability, use this enhanced configuration:
1# Using credentials file (recommended)2//192.168.0.5/sataDrive /mnt/samba cifs credentials=/etc/samba/credentials,uid=1000,gid=1000,file_mode=0755,dir_mode=0755,vers=3.0,_netdev,x-systemd.automount 0 0Additional options explained:
file_mode=0755- Permission mode for files (rwxr-xr-x)dir_mode=0755- Permission mode for directories_netdev- Wait for network to be available before mountingx-systemd.automount- Mount automatically when accessed (on-demand)nofail- Continue boot even if mount fails (add if optional)
Apply fstab Changes
After editing /etc/fstab, test your configuration:
1# Test mount all entries in fstab2sudo mount -a3
4# Verify the mount5df -h | grep samba6mount | grep samba7
8# Check if files are accessible9ls -la /mnt/sambaAdvanced Mount Options
Guest Access (No Authentication)
1# Temporary guest mount2sudo mount -t cifs //192.168.0.5/public /mnt/public -o guest,uid=1000,gid=10003
4# fstab entry for guest access5//192.168.0.5/public /mnt/public cifs guest,uid=1000,gid=1000,_netdev 0 0Domain Authentication
1# Mount with domain credentials2sudo mount -t cifs //server.domain.com/share /mnt/domain -o username=user,password=pass,domain=DOMAIN3
4# fstab with domain5//server.domain.com/share /mnt/domain cifs credentials=/etc/samba/domain-creds,domain=DOMAIN,uid=1000,gid=1000 0 0Performance Tuning Options
1# High-performance mount options2//192.168.0.5/sataDrive /mnt/samba cifs credentials=/etc/samba/credentials,uid=1000,gid=1000,vers=3.1.1,cache=loose,actimeo=30 0 0Performance options:
cache=loose- More aggressive caching for better performancecache=strict- Strict cache coherency (safer but slower)cache=none- Disable caching (not recommended)actimeo=30- Attribute cache timeout in seconds
Troubleshooting
Check CIFS Version Support
1# List available SMB protocol versions2sudo modinfo cifs | grep version3
4# Test connection with specific version5sudo mount -t cifs //192.168.0.5/share /mnt/test -o username=usr,password=123,vers=2.1Debug Mount Issues
1# Enable verbose output for debugging2sudo mount -t cifs //192.168.0.5/share /mnt/samba -o username=usr,password=123,vers=3.0 -vvv3
4# Check system logs for errors5sudo dmesg | grep CIFS6sudo journalctl -u systemd-networkd -fCommon Error Solutions
Error: "mount error(13): Permission denied"
1# Verify credentials and share permissions2smbclient -L //192.168.0.5 -U usr3
4# Try different protocol version5sudo mount -t cifs //192.168.0.5/share /mnt/samba -o username=usr,password=123,vers=1.0Error: "mount error(2): No such file or directory"
1# Check if server is reachable2ping 192.168.0.53
4# Verify share name5smbclient -L //192.168.0.5 -U usr -NError: "Host is down"
1# Install and use smbclient for testing2sudo apt install smbclient3smbclient //192.168.0.5/sataDrive -U usrUnmounting Samba Shares
1# Unmount a specific share2sudo umount /mnt/samba3
4# Force unmount if busy5sudo umount -l /mnt/samba # Lazy unmount6sudo umount -f /mnt/samba # Force unmount7
8# Check what's using the mount9lsof +D /mnt/sambaChecking Mount Status
1# View all mounted filesystems2mount | grep cifs3
4# Check mount details5findmnt /mnt/samba6
7# Show space usage8df -h /mnt/samba9
10# Test read/write access11touch /mnt/samba/test.txt && rm /mnt/samba/test.txt