15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
31.10.2024

How to Install and Configure Samba on Ubuntu: A Complete Step-by-Step Guide

Samba is a powerful open-source software suite that enables seamless file and print sharing between computers running Windows and Unix-like operating systems such as Ubuntu. By installing and properly configuring Samba, you can bridge the gap between Linux and Windows environments, allowing users on both platforms to share files, directories, and even printers across a local or wide-area network.

Whether you are managing a home lab, a small business network, or a production server environment, this comprehensive guide walks you through every step of installing, configuring, and securing Samba on Ubuntu β€” including advanced options that most tutorials skip.

Prerequisites

Before you begin, make sure you have:

  • A server or machine running Ubuntu 20.04, 22.04, or 24.04
  • Sudo or root access to the system
  • A basic understanding of the Linux command line
  • Network connectivity between the machines you want to connect

If you are running Samba on a remote server, a reliable and performant hosting environment is essential. VPS Hosting from AlexHost gives you full root access, dedicated resources, and the flexibility to configure your server exactly as needed.

Step 1: Update System Packages

Before installing any new software, it is critical to update your system's package index and upgrade existing packages to their latest versions. This ensures compatibility and protects against known vulnerabilities.

sudo apt update && sudo apt upgrade -y

Wait for the process to complete before proceeding.

Step 2: Install Samba

Install the Samba package using the APT package manager:

sudo apt install samba -y

Once the installation is complete, verify it was successful by checking the Samba version:

smbd --version

You should see output similar to:

Version 4.15.13-Ubuntu

You can also verify that the Samba service is running:

sudo systemctl status smbd

The output should show the service as active (running).

Step 3: Back Up the Default Samba Configuration File

Before making any changes, always back up the original configuration file. This allows you to restore defaults if something goes wrong:

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

This is a best practice that many guides overlook but that experienced system administrators always follow.

Step 4: Create and Configure a Shared Directory

4.1 Create the Directory

Decide which directory you want to share over the network. For this guide, we will create a dedicated folder named shared inside the home directory:

mkdir ~/shared

4.2 Set Directory Permissions

Set the appropriate permissions on the directory. For a shared environment where multiple users need read and write access:

chmod 777 ~/shared

> Security Note: The chmod 777 setting grants full read, write, and execute permissions to all users. While convenient for testing, this is not recommended for production environments. In a production setup, use more restrictive permissions such as chmod 770 and manage access via group membership.

Step 5: Configure Samba β€” Edit smb.conf

Open the main Samba configuration file using a text editor:

sudo nano /etc/samba/smb.conf

Scroll to the very end of the file and add the following configuration block:

[SharedFolder]
   path = /home/username/shared
   available = yes
   valid users = username
   read only = no
   browsable = yes
   public = yes
   writable = yes

Replace username with your actual Ubuntu system username.

Understanding Each Directive

DirectiveDescription
pathThe absolute path to the directory being shared
availableWhether the share is available to users
valid usersRestricts access to the listed user(s)
read onlySet to no to allow write access
browsableWhether the share appears when browsing the network
publicAllows access without authentication (use carefully)
writableAllows users to write files to the share

After making your changes, save the file by pressing CTRL + O, then Enter, and exit with CTRL + X.

Validate the Configuration

Before restarting Samba, test your configuration file for syntax errors:

testparm

This command parses the smb.conf file and reports any issues. Fix any errors before proceeding.

Step 6: Create a Samba User Account

Samba maintains its own password database separate from the standard Linux system passwords. You must add a Samba-specific password for any user who needs to access the share.

sudo smbpasswd -a username

You will be prompted to enter and confirm a password. This password will be required when connecting to the shared folder from a remote Windows or Linux machine.

> Tip: Use a strong, unique password for each Samba user. Avoid reusing system passwords.

To enable the user account in Samba (if it was previously disabled):

sudo smbpasswd -e username

Step 7: Configure the Firewall

If you have UFW (Uncomplicated Firewall) enabled on your Ubuntu server, you need to allow Samba traffic through the firewall:

sudo ufw allow samba

Verify the rule was added:

sudo ufw status

Samba uses the following ports:

  • TCP 445 β€” SMB over TCP (primary)
  • TCP 139 β€” NetBIOS session service
  • UDP 137 & 138 β€” NetBIOS name and datagram services

Step 8: Restart and Enable Samba Services

Apply your configuration changes by restarting the Samba services:

sudo systemctl restart smbd
sudo systemctl restart nmbd

Enable both services to start automatically at system boot:

sudo systemctl enable smbd
sudo systemctl enable nmbd

The nmbd service handles NetBIOS name resolution, which is important for Windows clients to discover your Samba server by name on the local network.

Step 9: Access the Shared Folder from a Windows System

From any Windows computer on the same network, you can now connect to your Ubuntu Samba share:

  1. Open File Explorer
  2. In the address bar, type the network path to your Ubuntu machine:
   \ubuntu_ip_addressSharedFolder

Replace ubuntu_ip_address with the actual IP address of your Ubuntu server (e.g., \192.168.1.100SharedFolder)

  1. When prompted, enter the Samba username and password you configured in Step 6
  2. The shared folder will now appear in File Explorer, and you can read, write, copy, and delete files just as you would with any local folder

Map the Share as a Network Drive (Optional)

For persistent access, you can map the Samba share as a network drive in Windows:

  1. Right-click This PC in File Explorer
  2. Select Map network drive
  3. Choose a drive letter and enter the network path
  4. Check Reconnect at sign-in for automatic mounting

Step 10: Access the Samba Share from Another Linux System

On another Linux machine, you can connect to the Samba share using the smbclient command-line tool.

First, install smbclient if it is not already present:

sudo apt install smbclient -y

Then connect to the share:

smbclient //ubuntu_ip_address/SharedFolder -U username

Enter the Samba password when prompted. You will be dropped into an interactive FTP-like shell where you can use commands such as ls, get, put, and cd to navigate and transfer files.

Mount the Samba Share on Linux

For a more seamless experience, you can mount the Samba share as a local filesystem using cifs-utils:

sudo apt install cifs-utils -y
sudo mkdir /mnt/samba_share
sudo mount -t cifs //ubuntu_ip_address/SharedFolder /mnt/samba_share -o username=username,password=yourpassword

To make the mount persistent across reboots, add an entry to /etc/fstab:

//ubuntu_ip_address/SharedFolder /mnt/samba_share cifs username=username,password=yourpassword,_netdev 0 0

> Security Tip: Instead of storing credentials in /etc/fstab, use a credentials file with restricted permissions.

Step 11: Advanced Samba Configuration Options

Samba's smb.conf supports a wide range of directives that give you fine-grained control over your file sharing environment. Here are the most useful advanced options:

Read-Only Access

To prevent users from modifying files in a share, set:

read only = yes

Guest Access (Anonymous Shares)

To allow users to connect without a password:

guest ok = yes
public = yes

> Use guest access only on trusted, isolated networks. Never enable it on a public-facing server.

Restrict Access by IP Address

To limit which machines can connect to a share:

hosts allow = 192.168.1.0/24
hosts deny = ALL

This restricts access to machines on the 192.168.1.x subnet only.

Multiple Shared Folders

You can define as many shares as needed by adding additional blocks to smb.conf:

[Documents]
   path = /home/username/documents
   valid users = username
   read only = no
   writable = yes

[Backups]
   path = /srv/backups
   valid users = admin
   read only = yes
   browsable = no

Set Maximum Connections

To limit the number of simultaneous connections to a share:

max connections = 10

Force a Specific User or Group

To ensure all files are created with a specific user or group ownership:

force user = samba_user
force group = samba_group

Step 12: Securing Your Samba Installation

Security is paramount, especially if your Samba server is accessible beyond your local network. Follow these hardening best practices:

1. Disable Guest Access

Unless explicitly required, always disable anonymous access:

map to guest = Never

2. Use Strong Passwords

Enforce strong, unique passwords for all Samba users. Avoid dictionary words and use a combination of uppercase, lowercase, numbers, and symbols.

3. Restrict Share Visibility

Set browsable = no on sensitive shares so they do not appear when browsing the network. Users who know the exact path can still connect.

4. Limit Permissions on Shared Directories

Apply the principle of least privilege β€” only grant the permissions users actually need. Avoid chmod 777 in production.

5. Bind Samba to a Specific Interface

If your server has multiple network interfaces, bind Samba only to the internal interface:

[global]
   interfaces = eth0 lo
   bind interfaces only = yes

6. Enable Logging

Enable Samba logging to monitor access and detect suspicious activity:

[global]
   log file = /var/log/samba/log.%m
   max log size = 1000
   log level = 1

7. Keep Samba Updated

Regularly update Samba to patch known security vulnerabilities:

sudo apt update && sudo apt upgrade samba -y

Choosing the Right Hosting Environment for Your Samba Server

The performance and reliability of your Samba file server depend heavily on the underlying infrastructure. Here are some hosting options to consider based on your use case:

  • Small teams and personal projects: Shared Web Hosting provides an affordable entry point for lightweight workloads.
  • Growing businesses and development teams: VPS Hosting offers dedicated resources, full root access, and the scalability needed to run Samba alongside other services.
  • High-performance or enterprise workloads: Dedicated Servers deliver maximum performance, storage capacity, and network throughput β€” ideal for large-scale file sharing environments.
  • AI and data-intensive applications: If you are running Samba alongside machine learning or data processing workloads, GPU Hosting provides the compute power you need.

For teams that also need a managed control panel experience, VPS with cPanel simplifies server management while still giving you the flexibility to install and configure Samba.

Troubleshooting Common Samba Issues

Cannot Connect from Windows

  • Verify the Samba service is running: sudo systemctl status smbd
  • Check the firewall rules: sudo ufw status
  • Confirm the IP address is correct and reachable: ping ubuntu_ip_address
  • Ensure the username and password match what was set with smbpasswd

Permission Denied When Accessing the Share

  • Check directory permissions: ls -la ~/shared
  • Verify the valid users directive in smb.conf matches the Samba username
  • Confirm the user has been added with smbpasswd -a username

Samba Share Not Visible on Network

  • Make sure nmbd is running: sudo systemctl status nmbd
  • Set browsable = yes in the share configuration
  • Check that the Windows machine and Ubuntu server are on the same subnet

Configuration Errors After Editing smb.conf

  • Run testparm to identify syntax errors
  • Restore the backup if needed: sudo cp /etc/samba/smb.conf.bak /etc/samba/smb.conf

Conclusion

Installing and configuring Samba on Ubuntu is one of the most effective ways to enable cross-platform file sharing between Linux and Windows systems. By following this guide, you have learned how to:

  • Install Samba and verify the installation
  • Create and configure shared directories with appropriate permissions
  • Add Samba users and manage authentication
  • Configure the firewall to allow Samba traffic
  • Access shares from both Windows and Linux clients
  • Apply advanced configuration options for access control and performance
  • Harden your Samba installation against common security threats

With the right server infrastructure and a properly configured Samba setup, you can build a reliable, secure, and high-performance file sharing environment that serves both Linux and Windows users seamlessly. Whether you are running Samba on a local machine or a cloud-based VPS Hosting environment, the principles in this guide will help you get the most out of your deployment.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started