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 -yWait for the process to complete before proceeding.
Step 2: Install Samba
Install the Samba package using the APT package manager:
sudo apt install samba -yOnce the installation is complete, verify it was successful by checking the Samba version:
smbd --versionYou should see output similar to:
Version 4.15.13-UbuntuYou can also verify that the Samba service is running:
sudo systemctl status smbdThe 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.bakThis 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 ~/shared4.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.confScroll 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 = yesReplace username with your actual Ubuntu system username.
Understanding Each Directive
| Directive | Description |
|---|---|
path | The absolute path to the directory being shared |
available | Whether the share is available to users |
valid users | Restricts access to the listed user(s) |
read only | Set to no to allow write access |
browsable | Whether the share appears when browsing the network |
public | Allows access without authentication (use carefully) |
writable | Allows 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:
testparmThis 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 usernameYou 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 usernameStep 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 sambaVerify the rule was added:
sudo ufw statusSamba 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 nmbdEnable both services to start automatically at system boot:
sudo systemctl enable smbd
sudo systemctl enable nmbdThe 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:
- Open File Explorer
- In the address bar, type the network path to your Ubuntu machine:
\ubuntu_ip_addressSharedFolderReplace ubuntu_ip_address with the actual IP address of your Ubuntu server (e.g., \192.168.1.100SharedFolder)
- When prompted, enter the Samba username and password you configured in Step 6
- 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:
- Right-click This PC in File Explorer
- Select Map network drive
- Choose a drive letter and enter the network path
- 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 -yThen connect to the share:
smbclient //ubuntu_ip_address/SharedFolder -U usernameEnter 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=yourpasswordTo 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 = yesGuest 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 = ALLThis 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 = noSet Maximum Connections
To limit the number of simultaneous connections to a share:
max connections = 10Force 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_groupStep 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 = Never2. 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 = yes6. 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 = 17. Keep Samba Updated
Regularly update Samba to patch known security vulnerabilities:
sudo apt update && sudo apt upgrade samba -yChoosing 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 usersdirective insmb.confmatches the Samba username - Confirm the user has been added with
smbpasswd -a username
Samba Share Not Visible on Network
- Make sure
nmbdis running:sudo systemctl status nmbd - Set
browsable = yesin the share configuration - Check that the Windows machine and Ubuntu server are on the same subnet
Configuration Errors After Editing smb.conf
- Run
testparmto 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.
