Introduction
Samba is an open-source utility that enables file sharing between machines running on a single network. The tool enables Linux machines to share files with machines running different operating systems, such as Windows.
In this tutorial, you will learn how to install and configure Samba on Ubuntu.
Prerequisites
- Ubuntu system (this guide uses Ubuntu 24.04 and applies to 22.04 and 20.04).
- A user account with root privileges.
- A text editor.
How to Install Samba on Ubuntu
Most Linux package managers have Samba in their default repository. Follow the steps below to install and configure Samba on Ubuntu:
Step 1: Install Samba
We will use apt, the default package manager for Ubuntu, to install Samba:
1. Refresh the package repository information with:
sudo apt update
2. Run the following command to install Samba:
sudo apt install samba -y
The -y
argument auto-approves any queries during the installation.
3. Verify the installation with whereis command:
whereis samba
The output prints the directory where Samba is installed. Another method to verify the installation is to check the Samba version:
samba -V
The output shows that the system installed Samba version 4.19.5.
4. Lastly, confirm that the Samba service is running with:
systemctl status smbd
The output shows that the smbd service is enabled and running. Press q to return to the terminal.
Step 2: Create a Shared Directory
Create a shared directory to define the specific folder and its contents that will be accessible to other users or devices on the network. Without a shared directory, Samba has nothing to serve to connected clients.
1. Use the mkdir command to create the directory under /home. For example, make a directory called sharing with:
sudo mkdir -p /home/sharing
2. Use ls command to verify the outcome.
ls /home
Step 3: Configure Samba's Global Options
Configure Samba by editing the smb.conf configuration file located in /etc/samba/smb.conf. In this tutorial, we will use nano:
1. Open the configuration file with:
sudo nano /etc/samba/smb.conf
2. Scroll down to the Global Settings section. This section configures the Samba server's behavior, name, role, and network interfaces.
Note: Certain settings in the smb.conf file are marked as comments. To enable and tweak those settings, uncomment them.
The key parameters to consider are in the subsections below.
Browsing/Identification
The Browsing subsection contains the workgroup
and server string
parameters.
- The
workgroup
parameter enables file sharing between a group of computers over a local area network. Ensure theworkgroup
settings correspond to the ones on Windows. - The
server string
setting identifies the Samba server. In our example, the server is named samba_server.
Note: To set the workgroup settings on Windows 10, open the Control Panel and access the System and Security settings. The workgroup
parameter is under the System section.
To configure the identification settings, uncomment the workgroup
and server string
parameters and add these values:
workgroup = WORKGROUP
server string = samba_server
Networking
Use the Networking subsection to configure network interfaces to which Samba binds. Networking contains two parameters:
- The
interfaces
parameter sets the network interface for Samba to bind to. - The
bind interfaces only
parameter ensures that Samba only binds to the interfaces listed in the file. The parameter should always be set toyes
.
To set the interfaces
parameter, first check the available interfaces with the ip command:
ip link
The example output indicates Samba binds to two interfaces: lo
, the loopback interface, and enp0s3
, the network interface.
For example, in this case, the settings are:
interfaces = lo enp0s3
bind interfaces only = yes
Note: The network interfaces Samba binds to may differ from one machine to another.
Debugging
The Debugging subsection has four parameters. Set them as follows:
log file = /var/log/samba/log.%m
max log size = 1000
logging = file
panic action = /usr/share/samba/panic-action %d
log file = /var/log/samba/log.%m
- Stores logs in a file named after the connecting machine (%m
) in the specified directory. This setting makes it easier to debug client-specific issues.max log size = 1000
- Limits log files to 1000 KB before rotation, preventing disk space exhaustion.logging = file
- Configures Samba to log events to files to enable accessible and persistent records.panic action = /usr/share/samba/panic-action %d
- Executes the specified script when a serious error occurs (%d
passes error details), enabling immediate troubleshooting or recovery actions.
Authentication, Domain, and Misc
The most significant Authentication parameter is server role
. This parameter determines the server type for Samba.
1. If there isn't one, add the following line to set Samba as a standalone server:
server role = standalone server
Other authentication settings include:
obey pam restrictions = yes
- Ensures Samba adheres to PAM authentication policies to maintain centralized access control.unix password sync = yes
- Synchronizes Samba passwords with UNIX system passwords to ensure consistent credentials across services.passwd program = /usr/bin/passwd
- Specifies the system command for changing user passwords to integrate with native password management.passwd chat = Enter\snew\s\spassword: %n\n Retype\snew\s\spassword: %n\n password\supdated\ssuccessfully .**
- Automates password change interactions to ensure smooth user updates.pam password change = yes
- Uses PAM to handle password changes. This setting allows the enforcement of system-wide password policies.map to guest = bad user
- Maps unrecognized users to the guest account to ensure limited access for unauthenticated users.
2. Do not change any settings in the Domain subsection, but scroll down to Misc and set the following:
usershare allow guests = yes
The setting allows guest (unauthenticated) access to user-created Samba shares. It enables anyone on the network to access the shared files without a username or password.
Keep all other Global Settings unchanged.
3. Save and exit the file and run the Samba testparm
utility to check for syntax errors:
testparm
The output shows the Loaded services file OK message, which signifies no syntax errors. With Global Settings configured, the Samba server is ready to use.
However, to unlock Samba's full functionality, configure the users and the directory, as shown in the section below.
Step 4: Set Up a User Account
Create a user account in Samba to authenticate and control access to shared resources. Doing so ensures that only authorized users can access sensitive files or directories.
1. To create a user account, set a username and password with:
sudo smbpasswd -a username
Note that the username should belong to a system user. For instance, in this example, the system account on the Ubuntu system is bosko. Hence, the username is the same if you want to add that existing user to Samba:
sudo smbpasswd -a bosko
2. To add a new user to Samba and the system, use the adduser command:
sudo adduser [username]
Replace [username]
with the name of the new user. For instance, add new_user to the system with:
sudo adduser new_user
Enter and confirm the system password for new_user.
3. Create a new Samba user with:
sudo smbpasswd -a new_user
4. Both users need to have read, write, and execute access to the sharing directory. While the user bosko has these permissions by default, new_user does not.
To grant read, write, and execute permissions to the sharing directory, run setfacl
:
sudo setfacl -R -m "u:new_user:rwx" /home/sharing
The command does not produce output.
Step 5: Configure Samba Share Directory Settings
A Samba share directory is a directory on a Linux system configured to be accessible to other devices on the network using the SMB/CIFS protocol. Follow the steps below to configure the directory settings:
1. Access the configuration file once again to add the previously made sharing directory.
sudo nano /etc/samba/smb.conf
2. Go to the end of the file and add the following lines:
[sharing]
comment = Samba share directory
path = /home/sharing
read only = no
writable = yes
browseable = yes
guest ok = no
valid users = @bosko @new_user
Each line grants specific permissions to access the directory. For instance:
[sharing]
. Represents the directory name. This is the directory location Windows users see.comment
. Serves as a shared directory description.path
. Specifies the shared directory location. The example uses a directory in /home, but users can also place the shared files under /samba.read only
. Allows users to modify the directory and add or change files when set tono
.writeable
. Grants read and write access when set toyes
.browseable
. Allows other machines in the network to find the Samba server and Samba share when set toyes
. Otherwise, users must know the exact Samba server name and type in the path to access the shared directory.guest ok
. When set tono
, this parameter disables guest access. Users need to enter a username and password to access the shared directory.valid users
. Only the users mentioned have access to the Samba share. Replace@bosko
and@new_user
with your own values.
3. Save the changes and exit the file.
4. Recheck the syntax:
testparm
The output should confirm that Samba is adequately configured. For a more verbose output of all settings, hit Enter:
Step 6: Update the Firewall Rules
To ensure the Linux firewall allows Samba traffic, run:
sudo ufw allow samba
Step 7: Connect to the Shared Directory
To complete the configuration, connect to the shared directory that we set up earlier. Follow the steps below:
1. First restart the Samba service to ensure all the configuration changes apply:
sudo systemctl restart smbd
The command prints no output.
2. To connect to the shared directory via GUI, access the default file manager and choose the Other Locations option.
3. Type the following into the Enter server address... box and click Connect:
smb://ip-address/sharing
4. The system prompts for a Username and Password. Provide the requested info and click Connect again:
5. This adds the sharing directory to the Windows shares location:
How to Uninstall Samba on Ubuntu
This section shows how to uninstall and remove all Samba-related files from your system. Follow the steps below:
Step 1: Stop the Samba Service
Before removing Samba, stop its services to ensure a clean removal process.
1. Stop the smbd
service with the following command:
sudo systemctl stop smbd
2. Confirm the service is stopped with:
systemctl status smbd
The output indicates the service is inactive.
Step 2: Remove Samba Packages
Uninstall the Samba software packages by following the steps below:
1. Use the apt
package manager to remove Samba and related dependencies:
sudo apt remove --purge samba -y
The --purge
option ensures configuration files are also removed.
2. Clean up residual configuration files and dependencies:
sudo apt autoremove --purge -y
3. Verify Samba is no longer installed:
whereis samba
The output should not display installation directories, which means Samba has been successfully uninstalled.
Step 3: Remove Custom Files
While the --purge
flag removes configuration files, some custom or user-generated files might remain.
1. Use the rm command to manually delete any leftover Samba configuration files in /etc/samba:
sudo rm -rf /etc/samba
2. Clean up log files generated by Samba:
sudo rm -rf /var/log/samba
Step 4: Revert File Permissions
If Samba was used to manage file permissions on shared directories, reset them. Revert ACL changes for the shared directories. We will revert ACL changes for /home/sharing:
sudo setfacl -bR /home/sharing
Step 5: Remove Firewall Rules
Remove the Samba-related firewall rules to secure your system.
1. Delete the UFW rule for Samba:
sudo ufw delete allow samba
2. Reload UFW to apply the changes:
sudo ufw reload
Step 6: Verify the Uninstallation
Ensure Samba services and configurations are completely removed:
1. Check the status of the Samba service:
systemctl status smbd
The output should indicate the service is not found.
2. Confirm no Samba-related packages remain:
dpkg -l | grep samba
The command should return no results.
By following these steps, Samba and all its related files, configurations, and services are completely removed from your Ubuntu system.
Conclusion
This tutorial showed how to install, configure, and uninstall Samba on Ubuntu. Use Samba to enable seamless file and printer sharing between Linux and Windows systems and facilitate cross-platform connectivity in a networked environment.
Next, learn essential Linux commands with this handy Linux commands cheat sheet.