Introduction
Samba is an open-source utility that enables file sharing between machines running on a single network. It enables Linux machines to share files with machines running different operating systems, such as Windows.
This tutorial teaches you how to install and configure Samba in Ubuntu 20.04 or 22.04.
Prerequisites
- Ubuntu system (this guide uses Ubuntu 22.04. The same steps work on Ubuntu 20.04).
- Sudo privileges.
- A text editor (this tutorial uses Vim).
How to Install and Configure Samba on Ubuntu
Most Linux package managers have Samba in their default repository. To configure Samba on Ubuntu, follow the steps below:
Step 1: Install Samba
1. Start by updating the package registry:
sudo apt update
2. Next, use apt to install Samba. Include the -y
argument to auto-approve any queries during the installation:
sudo apt install samba -y
3. Verify the installation with:
whereis samba
The output prints the directory containing Samba. Another method of verifying the installation is by checking the Samba version:
samba -V
The output shows that the system installed Samba version 4.16.4.
4. Lastly, confirm that Samba is running with:
systemctl status smbd
The output shows that the smbd service is enabled and running.
Step 2: Create a Shared Directory
1. To share files with Samba, create a directory containing files for sharing. Use mkdir -p to create the directory under /home:
For example, make a directory called sharing with:
sudo mkdir -p /home/sharing
2. Use ls to verify the outcome.
ls
Step 3: Configure Samba’s Global Options
Configure Samba by editing the smb.conf file located in /etc/samba/smb.conf.
Access the file with Vim:
sudo vim /etc/samba/smb.conf
Next, scroll down to the Global Settings section. Use this section to configure 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 following subsections:
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 that Samba binds to. Networking contains two parameters:
- The first parameter,
interfaces
, sets the network interface for Samba to bind to. - The second parameter,
bind interfaces only
, 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
Authentication, Domain, and Misc
The most significant Authentication parameter is server role
. This parameter determines the server type for Samba.
1. Set Samba as a standalone server:
server role = standalone server
The following is an extensive list of other authentication settings:
obey pam restrictions = yes
unix password sync = yes
passwd program = /usr/bin/passwd
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
pam password change = yes
map to guest = bad user
2. Do not change any settings in the Domain subsection, but scroll down to Misc and set the following:
usershare allow guests = yes
Keep all other Global Settings unchanged.
3. Save and exit the file and run the Samba utility testparm
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.
Still, not configuring the users and the directory limits the Samba functionality.
Step 4: Set Up a User Account
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 saraz. Hence, the username is the same:
sudo smbpasswd -a saraz
2. To add a new user to Samba and the system, use adduser:
sudo adduser username
For instance, add new_user to the system with:
sudo adduser new_user
3. After entering and confirming the system password for new_user, create a new Samba user with:
sudo smbpasswd -a new_user
Next, both users need to have read, write and execute access to the sharing directory. However, saraz has these permissions by default. On the other hand, new_user does not.
4. 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 doesn’t produce any output.
Step 5: Configure Samba Share Directory Settings
1. Access the configuration file once again to add the previously made sharing directory. Go to the end of the file and add:
[sharing]
comment = Samba share directory
path = /home/sharing
read only = no
writable = yes
browseable = yes
guest ok = no
valid users = @saraz @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. This parameter specifies the shared directory location. The example uses a directory in /home, but users can also place the shared files under /samba.
- read only. This parameter allows users to modify the directory and add or change files when set to
no
. - writeable. Grants read and write access when set to
yes
. - browseable. This parameter allows other machines in the network to find the Samba server and Samba share when set to
yes
. Otherwise, users must know the exact Samba server name and type in the path to access the shared directory. - guest ok. When set to
no
, 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.
2. Save the changes and exit the file.
3. Rerun testparm
:
The output confirms that the Samba is adequately configured. For a more verbose output, 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
1. Before connecting to the Samba server, restart the services with:
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 select Connect:
smb://ip-address/sharing
4. The system asks for a Username and Password. Provide the requested info and click Connect again:
5. This adds the sharing directory to the Windows shares location:
Conclusion
After reading this tutorial, you now know how to install and configure Samba on Ubuntu.
Next, learn essential Linux commands with this handy Linux commands cheat sheet.