Class 46 of 55 • Linux Networking & Servers

Linux Samba Server Setup (SMB/CIFS): Cross-Platform File Sharing Tutorial

A practical masterclass on installing Samba, configuring /etc/samba/smb.conf shares, managing smbpasswd user credentials, validating with testparm, and mounting on Windows and Linux.

In modern heterogeneous corporate environments, enterprise networks rarely run exclusively on Linux or Windows. Workstations commonly run Microsoft Windows 10/11 or macOS, while central file servers, databases, and backup appliances run enterprise Linux. Samba is the open-source software suite that bridges this divide by implementing the Server Message Block (SMB) and Common Internet File System (CIFS) networking protocols.

With Samba, a Linux machine can act as an SMB file server, a network print server, or even a full Microsoft Active Directory Domain Controller (AD DC), allowing Windows and macOS users to drag, drop, and edit files over the network without installing any client software.

1. Samba Architecture: smbd vs nmbd

Samba consists of two primary system service daemons:

  • smbd: The core file and print sharing engine. It manages client authentication, file locking, and data transfer over TCP port 445 (direct-hosted SMB) and legacy TCP port 139 (NetBIOS over TCP).
  • nmbd: The NetBIOS name server daemon. It handles hostname resolution (NetBIOS name to IP address) and advertises the server in Windows "Network Neighborhood" / network discovery over UDP ports 137 and 138.

2. Step-by-Step: Installing Samba Server

Step 2.1: Install Packages

# On Ubuntu / Debian:
sudo apt update
sudo apt install samba samba-common-bin smbclient cifs-utils -y

# On RHEL / Rocky Linux / AlmaLinux / Fedora:
sudo dnf install samba samba-common samba-client cifs-utils -y

Step 2.2: Prepare the Shared Directory

Let's create two practical corporate shares: a Public Anonymous Share (read/write for everyone) and a Secured Corporate Share (restricted to authenticated team members):

# 1. Create directory paths
sudo mkdir -p /srv/samba/public
sudo mkdir -p /srv/samba/secured

# 2. Permissions for public share
sudo chown -R nobody:nogroup /srv/samba/public     # Ubuntu/Debian
# sudo chown -R nobody:nobody /srv/samba/public   # RHEL/Rocky
sudo chmod -R 0777 /srv/samba/public

# 3. Create a dedicated Linux group for secured share access
sudo groupadd smbgroup
sudo chgrp -R smbgroup /srv/samba/secured
sudo chmod -R 0770 /srv/samba/secured

3. Configuring /etc/samba/smb.conf

The entire behavior of Samba is governed by /etc/samba/smb.conf. Before editing, always create a clean backup of the default configuration:

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup
sudo nano /etc/samba/smb.conf

Configure the global parameters and add the share definitions:

[global]
   workgroup = WORKGROUP
   server string = Linux Samba File Server
   security = user
   map to guest = bad user
   dns proxy = no
   log file = /var/log/samba/log.%m
   max log size = 1000

# Share 1: Public Read-Write Share (No Password Required)
[Public]
   comment = Public Shared Storage
   path = /srv/samba/public
   browseable = yes
   writable = yes
   guest ok = yes
   guest only = yes
   create mask = 0777
   directory mask = 0777

# Share 2: Secured Authenticated Share (Restricted to 'smbgroup' members)
[Secured]
   comment = Restricted Corporate Storage
   path = /srv/samba/secured
   browseable = yes
   writable = yes
   guest ok = no
   valid users = @smbgroup
   create mask = 0660
   directory mask = 0770

4. Managing Samba Users & smbpasswd

Samba authenticates users against its own encrypted database (passdb.tdb). A user must already exist in the Linux system before they can be added to Samba:

# 1. Create a Linux system user without shell login privilege
sudo useradd -M -s /usr/sbin/nologin john
sudo usermod -aG smbgroup john

# 2. Assign a Samba password to the user
sudo smbpasswd -a john
# Enter and confirm password when prompted

# 3. Enable the user in Samba
sudo smbpasswd -e john

smbpasswd Cheat Sheet:

  • smbpasswd -a <user>: Adds a new Samba user and sets password.
  • smbpasswd -d <user>: Disables a Samba user account.
  • smbpasswd -e <user>: Enables a previously disabled Samba user.
  • smbpasswd -x <user>: Deletes the user from the Samba database.

5. Validating with testparm & Starting Services

Always run testparm to verify that there are no typographical syntax errors before restarting the services:

# Check configuration syntax
testparm -s

If testparm outputs Loaded services file OK, start and enable the daemons:

# On Ubuntu / Debian:
sudo systemctl restart smbd nmbd
sudo systemctl enable smbd nmbd

# On RHEL / Rocky / AlmaLinux:
sudo systemctl restart smb nmb
sudo systemctl enable smb nmb

Opening Ports in the Firewall

# Ubuntu UFW:
sudo ufw allow samba

# RHEL Firewalld:
sudo firewall-cmd --permanent --add-service=samba
sudo firewall-cmd --reload

6. Connecting to Samba from Windows and Linux Clients

Accessing from Microsoft Windows (10 / 11)

  1. Press Windows Key + R to open the Run dialogue.
  2. Type the UNC server path: \\192.168.1.100 (substituting your Linux server IP) and press Enter.
  3. Windows Explorer displays both the Public and Secured folders. Double-clicking Secured prompts for the Samba credentials created earlier (john and password).
  4. To map it permanently as a network drive (e.g. Z:): Right-click This PC → Map network drive → \\192.168.1.100\Secured → Check "Reconnect at sign-in".

Accessing from Another Linux Machine (cifs-utils)

# 1. Inspect remote shares from Linux CLI
smbclient -L //192.168.1.100 -U john

# 2. Mount manually into local directory
sudo mkdir -p /mnt/samba_secured
sudo mount -t cifs //192.168.1.100/Secured /mnt/samba_secured -o username=john

# 3. Mount permanently in /etc/fstab with a secure credentials file
sudo nano /etc/samba/credentials
# Enter:
# username=john
# password=secretpass
sudo chmod 600 /etc/samba/credentials

# Append to /etc/fstab:
# //192.168.1.100/Secured  /mnt/samba_secured  cifs  credentials=/etc/samba/credentials,_netdev,uid=1000,gid=1000  0  0

7. Practical Terminal Lab: Verifying Read/Write Locks

# Check currently active Samba client connections and open file locks
sudo smbstatus

# Check connected machines and IP addresses
sudo smbstatus --shares

8. Student Viva & Exam Practice Q&A

This directive specifies how Samba handles login attempts that provide an unrecognized username. Setting it to bad user automatically converts invalid or anonymous login attempts to the guest account (unprivileged nobody user). This allows users to access public shares marked with guest ok = yes without being prompted for a username and password.
Windows clients do not understand POSIX octal file permissions (like 644 or 775). When a Windows user creates a new file or directory on a Samba share, Samba uses create mask to set the default Linux octal file permissions (e.g. 0664) and directory mask (e.g. 0775) for new folders, preventing newly created files from having unsafe or unusable permissions on the server.
Even though Samba performs authentication via its own internal database (passdb.tdb), the actual operating system disk operations (reading inodes, writing file blocks, checking Linux POSIX file ownership) are executed by the Linux kernel. Therefore, the kernel must map every authenticated Samba session to a legitimate POSIX UID and GID registered in /etc/passwd.

9. Frequently Asked Questions (FAQ)

If your network consists entirely of Linux, Unix, and BSD servers, choose NFS because it is native to the Linux kernel, offers lower CPU overhead, and preserves native POSIX permissions and symlinks. If your network includes Microsoft Windows workstations, macOS, or requires Active Directory integration, choose Samba (SMB/CIFS).
This error indicates that the client cannot reach the server on TCP port 445. The most common causes are: 1) Firewall blocking TCP port 445 on the Linux server (check UFW / firewalld), 2) Incorrect IP address or server hostname resolution failure, or 3) smbd service is stopped on the server.
Set browseable = no inside that share's block in smb.conf. The share will not be listed when users browse the server, but users who know the exact path (e.g. \\server\hidden_share$) can still connect directly.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!