How to Set Up a Secure Linux SMB Server on Ubuntu
A Samba share is the quickest way to give Windows, macOS, and Linux clients a common file server. Installed with the defaults, though, it accepts old protocol versions and sends traffic in cleartext. This guide builds a share that requires SMB 3 encryption, admits only named users, stays off the internet, and logs who changed what.
Tested on: Ubuntu Server 22.04 LTS (Samba 4.15) and 24.04 LTS (Samba 4.19). On Debian 12 the steps are the same. On older Samba releases, replace server smb encrypt with smb encrypt.
What "secure" means for an SMB server
- No SMB1. SMB1 is the protocol WannaCry spread through. Require SMB 3 at minimum.
- Encryption on the wire. SMB 3 can encrypt every packet with AES-GCM or AES-CCM. Make it mandatory, not optional.
- No guest access. Every connection needs a named account with its own password.
- Least privilege. Access is granted through a group, with Linux permissions that match the share settings.
- Network scope. Port 445 is reachable only from your LAN and never exposed to the internet.
- Audit trail. File creates, renames, and deletes are logged with the username and client IP.
Prerequisites
- Ubuntu Server 22.04 or 24.04 with sudo access
- A static IP or DHCP reservation for the server
- Your LAN subnet (examples use
192.168.1.0/24)
Step 1: Install Samba
sudo apt update
sudo apt install samba
smbd --version
Step 2: Create the share directory and access group
sudo groupadd smbusers
sudo mkdir -p /srv/samba/shared
sudo chown root:smbusers /srv/samba/shared
sudo chmod 2770 /srv/samba/shared
2770 gives the owner and group full access and everyone else none. The leading 2 is the setgid bit, so new files inherit the smbusers group and stay accessible to the whole team.
Step 3: Create Samba users
Samba users must exist as Linux accounts, but they don't need a shell or a home directory:
sudo useradd -M -s /usr/sbin/nologin -G smbusers alice
sudo smbpasswd -a alice # sets the SMB password (separate from any Linux password)
sudo smbpasswd -e alice # enables the account
sudo pdbedit -L # lists Samba users
To give an existing Linux user access, add them to the group with sudo usermod -aG smbusers bob, then run smbpasswd -a bob.
Step 4: Write a hardened smb.conf
Back up the default file, then replace it:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.orig
sudo nano /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
server string = File Server
server role = standalone server
# Protocol and transport security
server min protocol = SMB3
server smb encrypt = required
server signing = mandatory
# No anonymous or guest access
map to guest = never
restrict anonymous = 2
# Network scope: LAN only, SMB over 445 only
hosts allow = 192.168.1.0/24 127.0.0.1
hosts deny = 0.0.0.0/0
smb ports = 445
disable netbios = yes
# Not a print server
load printers = no
printing = bsd
printcap name = /dev/null
disable spoolss = yes
# Logging
logging = file
log file = /var/log/samba/log.%m
max log size = 1000
log level = 1
[shared]
path = /srv/samba/shared
browseable = yes
read only = no
valid users = @smbusers
force group = smbusers
create mask = 0660
directory mask = 2770
# Audit file changes to syslog
vfs objects = full_audit
full_audit:prefix = %u|%I|%S
full_audit:success = mkdirat renameat unlinkat pwrite
full_audit:failure = connect
full_audit:facility = local5
full_audit:priority = notice
What the important lines do:
server min protocol = SMB3refuses SMB1 and SMB2 clients. Every supported version of Windows, macOS, and Linux speaks SMB 3.server smb encrypt = requiredrejects any client that can't encrypt. Usedesiredonly if you must support a legacy device, and document why.valid users = @smbuserslimits the share to the group. The@means "group".create maskanddirectory maskkeep files group-readable and group-writable, matching the setgid directory.
Step 5: Validate and restart
testparm # syntax check; read any warnings
sudo systemctl restart smbd
sudo systemctl disable --now nmbd # NetBIOS is off, so nmbd isn't needed
systemctl status smbd --no-pager
Step 6: Allow SMB through the firewall from the LAN only
sudo ufw allow from 192.168.1.0/24 to any port 445 proto tcp
sudo ufw enable
sudo ufw status numbered
Never port-forward 445 to the internet. For remote access, connect over a VPN first (WireGuard, Tailscale, or your firewall's VPN) and use the share as if you were on the LAN.
Step 7: Test the share and confirm encryption
smbclient //localhost/shared -U alice -m SMB3 -c 'ls'
sudo smbstatus
While a client is connected, smbstatus lists the protocol version and an Encryption column showing the cipher (for example AES-128-GCM). If that column says -, encryption isn't active. Recheck server smb encrypt.
Step 8: Connect clients
Windows
net use S: \\192.168.1.20\shared /user:alice /persistent:yes
Then verify from PowerShell:
Get-SmbConnection | Select-Object ServerName, ShareName, Dialect, Encrypted, Signed
You want Dialect 3.1.1 and Encrypted True.
Linux
sudo apt install cifs-utils
sudo mkdir -p /mnt/shared
printf 'username=alice\npassword=CHANGE_ME\n' | sudo tee /etc/samba/creds-alice >/dev/null
sudo chmod 600 /etc/samba/creds-alice
Add to /etc/fstab (seal requests SMB 3 encryption from the client side):
//192.168.1.20/shared /mnt/shared cifs credentials=/etc/samba/creds-alice,vers=3.1.1,seal,uid=1000,gid=1000,_netdev 0 0
sudo mount -a && df -h /mnt/shared
macOS
In Finder, press Cmd+K and connect to smb://192.168.1.20/shared. macOS negotiates SMB 3 encryption automatically when the server requires it.
Step 9: Read the audit log
The full_audit module writes to syslog facility local5. Send it to its own file:
echo 'local5.* /var/log/samba/audit.log' | sudo tee /etc/rsyslog.d/50-samba-audit.conf
sudo systemctl restart rsyslog
sudo tail -f /var/log/samba/audit.log
Each line shows the user, client IP, share, action, and path, which is what you need when someone asks who deleted a folder.
Troubleshooting
| Error | Usual cause | Fix |
|---|---|---|
NT_STATUS_LOGON_FAILURE | No Samba password, or the account is disabled | sudo smbpasswd -a user, then sudo smbpasswd -e user |
NT_STATUS_ACCESS_DENIED | User isn't in smbusers, or Linux permissions are wrong | id user; ls -ld /srv/samba/shared should show drwxrws--- |
NT_STATUS_CONNECTION_REFUSED | Firewall, hosts allow, or smbd not running | sudo ufw status; ss -tlnp | grep 445 |
| Old client can't connect | The device only speaks SMB1 or SMB2, or can't encrypt | Replace or update it. Don't lower the server minimum for one device. |
| Server doesn't appear in Windows "Network" | NetBIOS is disabled (on purpose) | Connect by \\IP\share, or install wsdd for WS-Discovery |
Security checklist
- SMB1 and SMB2 refused (
server min protocol = SMB3) - Encryption required, and
smbstatusshows a cipher - Guest access off and anonymous access restricted
- Port 445 open to the LAN subnet only
- Share access granted through a group, with a setgid directory
- Audit log enabled and rotated
- Security updates automatic:
sudo apt install unattended-upgrades - Share contents backed up offsite (see automated Linux backups with Restic)
Studying for a certification? Browse the study guides.
Found this useful? Buy me a coffee