Samba, File Sharing with Windows
Share files between Linux servers and Windows clients using Samba (SMB/CIFS protocol)
Samba implements the SMB/CIFS protocol, allowing Linux servers to share files with Windows clients, and with other Linux systems. Useful for shared storage, mapped network drives, and mixed-OS environments.
Installation
sudo apt update
sudo apt install samba -y
sudo systemctl enable smbd nmbd
sudo systemctl start smbd nmbdCheck version:
smbd --versionBasic file share
Edit the Samba config:
sudo nano /etc/samba/smb.confAt the bottom, add a share:
[shared]
path = /srv/samba/shared
browseable = yes
read only = no
valid users = sambauser
create mask = 0664
directory mask = 0775Create the directory:
sudo mkdir -p /srv/samba/shared
sudo chown sambauser:sambashare /srv/samba/shared
sudo chmod 2775 /srv/samba/sharedTest the config:
testparmRestart Samba:
sudo systemctl restart smbdCreate a Samba user
Samba uses its own password database separate from Linux system passwords:
# Create Linux user first (no shell access)
sudo useradd -M -s /usr/sbin/nologin sambauser
# Set Samba password
sudo smbpasswd -a sambauser
# Enable the user
sudo smbpasswd -e sambauserConnect from Windows
In File Explorer address bar:
\\SERVER_IP\sharedOr map as a network drive:
- Right-click "This PC" → Map network drive
- Folder:
\\SERVER_IP\shared - Enter Samba credentials when prompted
Connect from Linux (CIFS)
Install the CIFS client:
sudo apt install cifs-utils -yMount manually:
sudo mkdir -p /mnt/samba
sudo mount -t cifs //10.0.0.1/shared /mnt/samba -o username=sambauser,password=yourpassMount via fstab (store credentials in a file):
sudo nano /root/.smbcredentialsusername=sambauser
password=yourpassword
domain=WORKGROUPsudo chmod 600 /root/.smbcredentialsIn /etc/fstab:
//10.0.0.1/shared /mnt/samba cifs credentials=/root/.smbcredentials,_netdev,uid=1000,gid=1000 0 0Public share (no password)
For a simple read-only public share (e.g. software repository):
[public]
path = /srv/samba/public
browseable = yes
read only = yes
guest ok = yesAdd to smb.conf global section:
[global]
map to guest = bad userShares with groups
Share accessible by multiple users via a group:
sudo groupadd sambashare
sudo usermod -aG sambashare user1
sudo usermod -aG sambashare user2
sudo smbpasswd -a user1
sudo smbpasswd -a user2[team]
path = /srv/samba/team
browseable = yes
read only = no
valid users = @sambashare
force group = sambashare
create mask = 0664
directory mask = 0775Firewall rules
sudo ufw allow samba
# Or manually:
sudo ufw allow 137/udp
sudo ufw allow 138/udp
sudo ufw allow 139/tcp
sudo ufw allow 445/tcpUseful commands
# List all shares
smbclient -L localhost -U sambauser
# Check connected users
sudo smbstatus
# Test config
testparm
# View logs
sudo tail -f /var/log/samba/log.smbd
# Reload config without restart
sudo smbcontrol smbd reload-configTroubleshoot
| Issue | Fix |
|---|---|
NT_STATUS_LOGON_FAILURE | Wrong Samba password, reset with smbpasswd |
NT_STATUS_ACCESS_DENIED | Check valid users and directory permissions |
| Share not visible on Windows | Check browseable = yes and firewall |
| Can't write files | Check read only = no and directory ownership |