writing
How to Create a Samba (SMB/CIFS) File Share on Ubuntu 22.04
Introduction
Samba allows Linux systems to expose directories as SMB/CIFS shares, making them accessible from Windows, macOS, and other Linux machines. This is useful when you need to transfer files between systems or provide centralized storage without installing additional software on Windows.
This guide demonstrates how to configure a secure Samba share on Ubuntu 22.04.
Prerequisites
- Ubuntu 22.04
- A directory you want to share
- Root or sudo privileges
- Network connectivity between the Linux server and Windows clients
Step 1. Install Samba
Update the package repository and install Samba.
sudo apt update
sudo apt install samba -y
Verify that the service is installed.
systemctl status smbd
Step 2. Prepare the Shared Directory
In this example, the shared directory is:
/mnt/shared
Create it if necessary.
sudo mkdir -p /mnt/shared
Check the ownership and permissions.
ls -ld /mnt/shared
Example output:
drwxr-xr-x root root /mnt/shared
If the directory is intended to be read-only, these permissions are generally sufficient.
Step 3. Create a Samba User
Create a user that will authenticate to Samba.
sudo useradd -M -s /usr/sbin/nologin fileshare
Set the Samba password.
sudo smbpasswd -a fileshare
sudo smbpasswd -e fileshare
The Samba password is independent of the Linux account password.
Step 4. Configure the Share
Edit the Samba configuration.
sudo nano /etc/samba/smb.conf
Append the following section.
[SharedFiles]
path = /mnt/shared
browseable = yes
read only = yes
guest ok = no
valid users = fileshare
Configuration Explanation
| Parameter | Description |
|---|---|
path | Directory exposed over SMB |
browseable | Makes the share visible during network browsing |
read only | Prevents clients from modifying files |
guest ok | Disables anonymous access |
valid users | Restricts access to specified Samba users |
If write access is required, replace:
read only = yes
with
read only = no
and ensure the underlying filesystem permissions allow writing.
Step 5. Restart Samba
Apply the new configuration.
sudo systemctl restart smbd
sudo systemctl enable smbd
Verify the service.
sudo systemctl status smbd
Step 6. Configure the Firewall
If UFW is enabled, allow Samba traffic.
sudo ufw allow Samba
or
sudo ufw allow 445/tcp
Step 7. Access the Share from Windows
Open File Explorer and browse to:
\\SERVER-IP\SharedFiles
or
\\SERVER-HOSTNAME\SharedFiles
Authenticate using the Samba credentials created earlier.
Testing the Configuration
Ubuntu provides the testparm utility to validate the configuration.
testparm
List available shares.
smbclient -L localhost -U fileshare
Example output:
Sharename Type
--------- ----
SharedFiles Disk
IPC$ IPC
Common Issues
Permission Denied
Verify the directory permissions.
ls -ld /mnt/shared
Check whether the authenticated user has permission to read the directory.
Cannot Connect from Windows
Verify that the Samba service is running.
systemctl status smbd
Check that TCP port 445 is listening.
sudo ss -tlnp | grep 445
Firewall Blocking Access
Verify firewall rules.
sudo ufw status
Security Recommendations
- Disable guest access unless explicitly required.
- Create dedicated Samba accounts instead of using administrative users.
- Use read-only shares whenever clients do not need to modify data.
- Restrict network access using firewall rules where possible.
- Regularly update Samba packages with system updates.
Conclusion
Samba provides a straightforward way to share directories between Linux and Windows environments using the SMB protocol. A read-only configuration is often the safest option for scenarios such as distributing exported data, backups, software repositories, or log files, while writable shares can be enabled when collaboration is required.