Network & Connectivity

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 nmbd

Check version:

smbd --version

Basic file share

Edit the Samba config:

sudo nano /etc/samba/smb.conf

At the bottom, add a share:

[shared]
   path = /srv/samba/shared
   browseable = yes
   read only = no
   valid users = sambauser
   create mask = 0664
   directory mask = 0775

Create the directory:

sudo mkdir -p /srv/samba/shared
sudo chown sambauser:sambashare /srv/samba/shared
sudo chmod 2775 /srv/samba/shared

Test the config:

testparm

Restart Samba:

sudo systemctl restart smbd

Create 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 sambauser

Connect from Windows

In File Explorer address bar:

\\SERVER_IP\shared

Or 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 -y

Mount manually:

sudo mkdir -p /mnt/samba
sudo mount -t cifs //10.0.0.1/shared /mnt/samba -o username=sambauser,password=yourpass

Mount via fstab (store credentials in a file):

sudo nano /root/.smbcredentials
username=sambauser
password=yourpassword
domain=WORKGROUP
sudo chmod 600 /root/.smbcredentials

In /etc/fstab:

//10.0.0.1/shared   /mnt/samba   cifs   credentials=/root/.smbcredentials,_netdev,uid=1000,gid=1000   0   0

Public 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 = yes

Add to smb.conf global section:

[global]
   map to guest = bad user

Shares 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 = 0775

Firewall 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/tcp

Useful 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-config

Troubleshoot

IssueFix
NT_STATUS_LOGON_FAILUREWrong Samba password, reset with smbpasswd
NT_STATUS_ACCESS_DENIEDCheck valid users and directory permissions
Share not visible on WindowsCheck browseable = yes and firewall
Can't write filesCheck read only = no and directory ownership

On this page