Samba File Server Setup on Ubuntu 22.04 LTS

Samba is an open-source software suite that provides seamless file and print services to SMB/CIFS clients. It allows Linux machines to act as file servers that can be accessed by Windows, macOS, and other Linux machines on the network.

Prerequisites

  • Ubuntu 22.04 LTS server
  • Minimum 2GB RAM (4GB recommended for large deployments)
  • Sufficient disk space for your file storage needs
  • Root or sudo privileges
  • Static IP address configured
  • Basic knowledge of Linux command line

1 System Preparation

Update your system and set the hostname:

sudo apt update && sudo apt upgrade -y
sudo hostnamectl set-hostname samba-server
exec bash

Install necessary utilities:

sudo apt install -y wget curl nano

2 Install Samba

Install the Samba package:

sudo apt install -y samba

Check if Samba is running:

sudo systemctl status smbd nmbd

Enable Samba to start on boot:

sudo systemctl enable smbd nmbd

3 Configure Firewall

Allow Samba through the firewall:

sudo ufw allow samba
sudo ufw enable

Alternatively, open specific ports manually:

sudo ufw allow 139/tcp
sudo ufw allow 445/tcp
sudo ufw allow 137/udp
sudo ufw allow 138/udp
sudo ufw enable

Verify the firewall status:

sudo ufw status

4 Create Shared Directory

Create a directory for sharing:

sudo mkdir -p /samba/shared

Set appropriate permissions:

sudo chmod -R 0777 /samba/shared
sudo chown -R nobody:nogroup /samba/shared
Note: These permissions allow anyone to read and write to the share. For more secure configurations, see the advanced section.

5 Configure Samba Shares

Back up the original Samba configuration file:

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

Edit the Samba configuration file:

sudo nano /etc/samba/smb.conf

Add the following share configuration at the end of the file:

[shared]
    comment = Ubuntu File Server Share
    path = /samba/shared
    browsable = yes
    read only = no
    guest ok = yes
    create mask = 0777
    directory mask = 0777

Test the Samba configuration for errors:

testparm

Restart Samba to apply changes:

sudo systemctl restart smbd nmbd

6 Create Secure Share with User Authentication

Create a secure directory:

sudo mkdir -p /samba/secure

Create a Samba user:

sudo useradd sambauser
sudo smbpasswd -a sambauser

Set directory permissions:

sudo chown -R sambauser:sambauser /samba/secure
sudo chmod -R 0770 /samba/secure

Add the secure share to the Samba configuration:

sudo nano /etc/samba/smb.conf

Add the following configuration:

[secure]
    comment = Secure File Server Share
    path = /samba/secure
    valid users = sambauser
    browsable = yes
    read only = no
    guest ok = no
    create mask = 0770
    directory mask = 0770

Restart Samba to apply changes:

sudo systemctl restart smbd nmbd

7 Access Samba Shares from Clients

From Windows:

  1. Open File Explorer
  2. In the address bar, type: \\samba-server-ip\shared
  3. For secure share: \\samba-server-ip\secure
  4. Enter credentials when prompted (for secure share)

From Linux:

# Install Samba client if needed
sudo apt install -y smbclient cifs-utils

# List available shares
smbclient -L //samba-server-ip -U%

# Connect to a share
smbclient //samba-server-ip/shared -U username

Mount Samba share permanently:

# Create mount point
sudo mkdir /mnt/samba-share

# Add to /etc/fstab
//samba-server-ip/shared /mnt/samba-share cifs username=sambauser,password=yourpassword,uid=1000,gid=1000 0 0

# Mount the share
sudo mount -a

8 Advanced Configuration Options

Configure Samba as a Domain Member:

# Install required packages
sudo apt install -y winbind krb5-user

# Join domain (replace with your domain details)
sudo net ads join -U administrator

Configure Samba as a Domain Controller:

# Install Samba domain controller packages
sudo apt install -y samba-dsdb-modules samba-vfs-modules

# Configure as AD DC
sudo samba-tool domain provision --use-rfc2307 --interactive

Enable Samba recycle bin functionality:

[shared]
    vfs objects = recycle
    recycle:repository = .recycle/%U
    recycle:keeptree = yes
    recycle:versions = yes
    recycle:maxsize = 0
    recycle:exclude = *.tmp,*.temp,*.o,*.obj
    recycle:noversions = *.doc,*.xls,*.ppt

9 User and Group Management

Create multiple Samba users:

# Create system user
sudo useradd -m -s /bin/bash user1
sudo useradd -m -s /bin/bash user2

# Set Samba passwords
sudo smbpasswd -a user1
sudo smbpasswd -a user2

Create a group for shared access:

sudo groupadd smbgroup
sudo usermod -aG smbgroup user1
sudo usermod -aG smbgroup user2

# Set group ownership on directory
sudo chgrp -R smbgroup /samba/group-share
sudo chmod -R 0770 /samba/group-share

Configure group-based share:

[group-share]
    comment = Group File Share
    path = /samba/group-share
    valid users = @smbgroup
    browsable = yes
    read only = no
    guest ok = no
    create mask = 0770
    directory mask = 0770
    force group = smbgroup

10 Troubleshooting Common Issues

Check Samba service status:

sudo systemctl status smbd nmbd
sudo journalctl -u smbd -f

Check Samba logs:

tail -f /var/log/samba/log.smbd
tail -f /var/log/samba/log.nmbd

Test Samba configuration:

testparm -s

Check network connectivity:

smbclient -L localhost -U%

Common issues and solutions:

Issue Solution
Permission denied errors Check directory permissions and SELinux/AppArmor settings
Cannot access share from Windows Check firewall settings and SMB protocol version
Authentication failures Verify user exists in Samba database with smbpasswd
Slow file transfers Adjust socket options in smb.conf
Command copied to clipboard!