Samba: Share Files with Windows

Khimananda Oli 7 min read Virtualization
Samba: Share Files with Windows

By Khimananda Oli | Last reviewed: August 2026

Cross-platform file sharing remains a fundamental requirement in mixed-OS environments, yet misconfigured shares are a frequent source of security incidents and audit failures. When you need to implement Samba: Share Files with Windows clients from an Ubuntu server, the difference between a fragile hack and a production-grade solution lies in explicit user mapping, strict filesystem permissions, and disciplined firewall rules. This guide walks through the exact configuration I use in enterprise environments to ensure reliable access without exposing your infrastructure to unnecessary risk.

Windows ClientSMB/CIFS Requestsmbd / nmbdAuth + Share Config/etc/samba/smb.confsmbpasswd DBLinux FilesystemPOSIX Perms + ACLsUFW FirewallPort 445 Restricted
Samba: Share Files with Windows architecture — authentication, config, and filesystem layers must align for secure access

How do you install and configure Samba to share files with Windows on Ubuntu?

Before editing any configuration, ensure your base system is hardened. I always start with a fresh Ubuntu security hardening baseline because Samba exposes network services that attackers actively probe. Install the required packages and create a dedicated directory for sharing:

sudo apt update && sudo apt install -y samba samba-common-bin
sudo mkdir -p /srv/samba/shared
sudo chown nobody:nogroup /srv/samba/shared
sudo chmod 2770 /srv/samba/shared

The setgid bit (2770) ensures new files inherit the group ownership, which prevents permission drift when multiple users write to the same share. This is a common oversight that causes "access denied" errors weeks after deployment.

Defining the share in smb.conf

Edit /etc/samba/smb.conf and add your share definition at the bottom. Avoid modifying the global section unless necessary; defaults in Ubuntu 24.04+ are sensible for most cases:

[SharedDocs]
   path = /srv/samba/shared
   browseable = yes
   read only = no
   valid users = @sambashare
   write list = @sambashare
   force group = sambashare
   create mask = 0660
   directory mask = 2770
   guest ok = no

Key points: never set guest ok = yes in production. Always use valid users or valid groups to explicitly authorize access. The force group directive combined with the setgid directory ensures consistent ownership regardless of which authenticated user creates a file.

Creating Samba users and testing configuration

Samba maintains its own password database separate from Linux PAM. Add users to the system first, then enable them for Samba:

  1. Create a system group: sudo groupadd sambashare
  2. Add existing users: sudo usermod -aG sambashare username
  3. Set Samba passwords: sudo smbpasswd -a username
  4. Validate syntax: testparm -s (this catches 90% of config errors before restart)
  5. Restart services: sudo systemctl restart smbd nmbd

Always run testparm before restarting. A typo in smb.conf will prevent smbd from starting, and debugging without validated config wastes time. On Windows, connect via \\SERVER_IP\SharedDocs and authenticate with the Samba credentials you just set.

What are the critical security settings for Samba file sharing?

Security in Samba isn't optional—it's where most deployments fail audits. In my experience helping teams achieve SOC 2 compliance, these three controls are non-negotiable:

  • Disable SMBv1 globally: Add server min protocol = SMB2_10 to the [global] section. SMBv1 has known remote code execution vulnerabilities and should be treated as deprecated since 2017.
  • Restrict network access at the firewall: Never expose port 445 to the internet. Use UFW to limit access to specific subnets: sudo ufw allow from 192.168.1.0/24 to any port 445 proto tcp. If you're managing UFW rules, make this rule explicit rather than relying on default policies.
  • Enforce encryption in transit: For sensitive data, add server smb encrypt = required to force encrypted sessions. Modern Windows clients support this natively, but test with legacy systems first.
Disable SMBv1server min protocol = SMB2_10UFW Subnet Restrictionallow from 192.168.1.0/24 port 445Explicit User Authvalid users + smbpasswdEncrypt Transit (Optional)server smb encrypt = requiredFilesystem Permissionssetgid + force group alignment
Security layers for Samba: Share Files with Windows — each control must be applied in sequence to prevent gaps

A common mistake is assuming Samba respects Linux file permissions alone. It doesn't. Samba evaluates its own valid users directive before checking filesystem permissions. Both must align: if a user is in valid users but lacks write permission on the directory, they'll get confusing access errors. Always verify both layers independently during setup.

How does Samba compare to NFS and native Windows sharing for cross-platform access?

Choosing the right protocol depends on your environment's constraints. Here's how they stack up in practice for mixed Linux-Windows deployments:

CriteriaSamba (SMB)NFSWindows Native Share
Windows CompatibilityNative (no client software)Requires NFS client + UID mappingNative
Linux Server SupportFull (samba package)Full (nfs-kernel-server)Not applicable
Authentication IntegrationStandalone, LDAP, ADKerberos or IP-based trustActive Directory native
Performance (LAN)Good (SMB3 multichannel)Excellent (lower overhead)Good
Security ModelUser/group + encryptionNetwork-level trust (weaker)AD-integrated ACLs
Audit TrailVFS modules + loggingLimited without extra toolingWindows Event Log
Best ForMixed OS, compliance needsLinux-to-Linux, high throughputPure Windows environments

For teams operating in Nepal or similar regions with mixed infrastructure, Samba strikes the best balance. NFS performs better for pure Linux clusters, but the moment a Windows workstation needs access, the UID/GID mapping complexity outweighs the performance gain. If you're already running Active Directory, consider joining the Samba server to the domain using realm join for unified identity management—this eliminates duplicate user accounts and simplifies offboarding.

Why aren't my Samba shares accessible from Windows clients?

Troubleshooting Samba requires systematic elimination. Work through this checklist in order; skipping steps leads to chasing ghosts:

  1. Verify service status: systemctl status smbd nmbd. Both must be active. If smbd fails to start, check journalctl -u smbd for config parse errors.
  2. Test local connectivity: Run smbclient -L localhost -U% on the server itself. If this fails, the problem is configuration—not networking.
  3. Check firewall rules: sudo ufw status verbose. Port 445/tcp must be allowed from the client's subnet. Don't forget that cloud VPC security groups add another layer—verify those too if hosting on AWS or Azure.
  4. Validate user mapping: Confirm the user exists in Samba's database: pdbedit -L | grep username. System users without corresponding smbpasswd entries cannot authenticate.
  5. Inspect filesystem permissions: ls -la /srv/samba/shared. The directory must be writable by the group specified in force group. Test with sudo -u username touch /srv/samba/shared/testfile to isolate POSIX issues from Samba issues.
  6. Review logs with context: Set log level = 2 temporarily in [global], reproduce the issue, then check /var/log/samba/log.smbd. Reset to log level = 0 afterward to avoid disk fill.

If you're also managing databases on the same server, ensure Samba's file locks don't conflict with your database storage paths. I've seen teams accidentally place MySQL data directories inside Samba shares, causing corruption. Keep application data and shared files strictly separated—see MySQL performance tuning guidance for proper storage layout.

Share Not Accessible?smbclient -L localhost works?NOYESCheck smbd status + testparmUFW allows client subnet?User in pdbedit + valid users?Filesystem perms match force group?Enable log level 2 + reproduce
Diagnostic flow for Samba: Share Files with Windows — follow top-down to isolate config, network, auth, or filesystem issues

Implementing Samba: Share Files with Windows Securely in Production

Getting Samba functional takes an hour; making it audit-ready takes discipline. Treat your smb.conf as infrastructure code—version it in Git, review changes in pull requests, and deploy via Ansible or Terraform rather than manual edits. Document every share's purpose, authorized users, and retention policy. If you're preparing for compliance assessments, enable the full_audit VFS module to log all file operations to syslog, then forward those logs to your structured logging pipeline for centralized analysis.

Start with the minimal viable share, validate each security layer independently, and expand only after confirming the foundation holds. If you need help designing a compliant file-sharing architecture or auditing an existing Samba deployment, reach out directly—I regularly assist teams in Nepal and globally with securing cross-platform infrastructure.

Frequently Asked Questions

Run sudo apt update followed by sudo apt install samba. Create a shared directory, edit /etc/samba/smb.conf to define the share path and permissions, then restart the service using sudo systemctl restart smbd to apply changes immediately.

TCP ports 139 and 445 plus UDP ports 137 and 138.

Use sudo smbpasswd -a username to create credentials. Samba maintains a separate password database from the system shadow file, so this step is mandatory even if the Linux account already has a valid login password configured.

Modern Windows disables SMBv1 and often blocks guest access. Ensure your smb.conf specifies server min protocol = SMB2_10 and map to guest = bad user. Verify network profile is Private and check Windows Firewall allows File and Printer Sharing rules.

Yes, when configured correctly with SMB3 encryption, strong authentication, and restricted network access. Disable legacy protocols, enforce signing, use dedicated service accounts, and regularly audit shares. Avoid exposing Samba directly to the internet without additional security layers like VPN or reverse proxy.

Add hosts allow = 192.168.1.0/24 10.0.0.5 inside the share definition in smb.conf. This limits connections at the Samba level regardless of firewall rules. Always test with testparm after editing to validate syntax before restarting the smbd service.

Yes, mount the USB to a permanent path via fstab first. Define the share in smb.conf pointing to that mount point. Set create mask and directory mask appropriately since removable media permissions often differ from internal storage filesystem defaults.

Samba uses SMB protocol native to Windows while NFS requires extra client software.

Check both filesystem permissions and Samba share parameters. Run ls -la on the shared path to verify ownership matches valid users in smb.conf. Confirm SELinux or AppArmor isn't blocking access. Test locally with smbclient before assuming network issues cause the denial.

Yes, Samba can join AD domains as a member server using net ads join. Configure kerberos, winbind, and idmap settings in smb.conf for proper authentication and UID mapping. This enables centralized user management and seamless access control across mixed environments.

Set server smb encrypt = required globally or per-share in smb.conf. Both client and server must support SMB3. Verify with smbstatus --encryption after connection. Note that encryption increases CPU overhead but protects data in transit on untrusted networks.

SMB protocol chattiness causes latency sensitivity. Enable SMB3 multichannel and large MTU where possible. Consider DFS namespaces for distributed access or switch to alternatives like rclone with caching for remote scenarios where native SMB performs poorly despite tuning.

Yes, macOS uses Finder's Connect to Server with smb:// syntax.

Copy /etc/samba/smb.conf and /var/lib/samba/private/passdb.tdb to timestamped backups. Export current share definitions with testparm -s > backup_shares.txt. Document custom scripts, cron jobs, and fstab entries related to shares to ensure complete restoration capability after upgrades.

Check /var/log/samba/log.smbd and log.nmbd for service errors. Enable log level = 2 in smb.conf for detailed diagnostics during issues. Use journalctl -u smbd for systemd-managed instances. Rotate logs regularly to prevent disk exhaustion during verbose debugging sessions.