Set Up Samba File Sharing on Linux
Use at your own risk. All guides and scripts are provided for educational purposes only. Always review and understand any code before running it — especially with administrative privileges. Test in a safe environment before using in production. Your system, your responsibility.
Samba lets your Linux machine share folders with Windows and macOS computers on the same network. This guide covers installation, basic configuration, and connecting from other devices.
Step 1 – Install Samba
sudo apt update
sudo apt install -y sambaVerify it’s running:
sudo systemctl status smbdStep 2 – Create a Shared Folder
Create the folder you want to share:
sudo mkdir -p /srv/samba/share
sudo chown -R $USER:$USER /srv/samba/share
sudo chmod -R 0775 /srv/samba/shareStep 3 – Configure Samba
Back up the original config first:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bakOpen the config file:
sudo nano /etc/samba/smb.confAdd this block at the bottom of the file:
[share]
comment = My Samba Share
path = /srv/samba/share
browseable = yes
read only = no
valid users = yourusername
create mask = 0664
directory mask = 0775What the options mean:
[share]— the share name (visible on the network)path— the folder being sharedbrowseable— visible when browsing the networkread only— set tonoto allow writingvalid users— only this user can access the sharecreate mask— permissions for new filesdirectory mask— permissions for new folders
Step 4 – Create a Samba User
Samba uses its own password database, separate from Linux system passwords:
sudo smbpasswd -a yourusernameEnter a password when prompted. This is what you’ll use when connecting from Windows or macOS.
Enable the user:
sudo smbpasswd -e yourusernameStep 5 – Test the Configuration
Check for syntax errors:
testparmIf no errors, restart Samba:
sudo systemctl restart smbd
sudo systemctl restart nmbdStep 6 – Allow Samba Through the Firewall
If you have UFW enabled:
sudo ufw allow sambaStep 7 – Connect from Other Devices
From macOS:
- Open Finder
- Press
CMD+K - Enter:
smb://192.168.1.x/share - Enter your Samba username and password
From Windows:
- Open File Explorer
- In the address bar type:
\\192.168.1.x\share - Enter your Samba username and password
From Linux:
# Mount the share (see the network share guide for fstab setup)
sudo mount -t cifs //192.168.1.x/share /mnt/share \
-o username=yourusername,password=yourpasswordMultiple Shares Example
You can add as many shares as you need in smb.conf:
[documents]
path = /srv/samba/documents
valid users = yourusername
read only = no
[media]
path = /srv/samba/media
valid users = @sambausers
read only = yes
[public]
path = /srv/samba/public
browseable = yes
read only = no
guest ok = yes@sambausers— share accessible to a groupguest ok = yes— no password required (use carefully on home networks only)
Useful Commands
| Command | What it does |
|---|---|
sudo systemctl restart smbd |
Restart Samba |
sudo systemctl status smbd |
Check Samba status |
testparm |
Validate smb.conf syntax |
sudo smbpasswd -a username |
Add a Samba user |
sudo smbpasswd -e username |
Enable a Samba user |
sudo smbpasswd -d username |
Disable a Samba user |
smbclient -L //localhost -U username |
List local shares |
Related Links
- Mount a Network Share in Linux (fstab) — mount this share on other Linux machines
- UFW Firewall on Ubuntu and Debian — manage firewall rules
- Samba Documentation — official Samba docs