How to Install and Configure Samba on Ubuntu 24.04


Samba is a Linux-based utility that enables file-sharing or printer-sharing facilities between Linux to Windows or Linux itself. Samba connects with all the clients that use the SMB/CIFS protocols. The SMB protocol is implemented by the Samba on Windows clients and the CIFS protocol on the Linux client.

Samba services include integrating the Linux servers/desktops into the active directory and creating a backup of the server that can be accessed from Windows. Thus, Samba because of its file-sharing feature, has been used to serve multiple purposes.

Keeping in view the importance of Samba, this post guides you to install Samba on the latest LTS distribution of Ubuntu, i.e., Ubuntu 24.04, Noble Numbat.

Outline:

You can see from the outline that we have categorized this post into three different sections, i.e., installing, configuring, and establishing a connection.

Installing Samba on Ubuntu 24.04

Samba Server is available on the default repositories of Ubuntu 24.04. Follow the below steps to install Samba on Ubuntu 24.04:

Step 1: Install Samba

Since the installation will be carried out from the default repositories. Thus, it is suggested to update the packages list, first:

sudo apt update

This will help load the possible updated version of Samba.

Let’s install Samba using the command:

sudo apt install samba

Step 2: Verify the Installation

Check the installed version of the samba to verify the installation:

samba -V

Or you can check the installed location of the samba using the command:

whereis samba

You cannot use Samba until you configure it. The upcoming section demonstrates the steps to configure and use Samba.

Configuring Samba on Ubuntu 24.04

Once Samba is installed, it needs to be configured before use. Its configuration includes the fine-tuning of the Samba service, the firewall configuration, and a dedicated Samba user. Let’s process the configuration:

Step 1: Allow Samba Over the Firewall

Since an external connection request would be made whenever you are interacting with Samba. Thus, you have to allow all the incoming connections associated with Samba. Here is the command:

sudo ufw allow samba

Or you can manually open the port dedicatedly for Samba. The Samba listens to Port 445 for the incoming connections.

Step 2: Create a Shared Directory

Samba interacts inside a shared location which is a directory/folder. Let’s create it using the mkdir command:

sudo mkdir <Path>

We created in the home directory:

Note: You can also refer to this Samba share as a Network Drive as its purpose is to share files within the network.

Just to test, we are creating another directory in the “/home/SambaShare” and a text file:

sudo mkdir /home/SambaShare/genie
sudo touch /home/SambaShare/data.txt

Step 3: Add a Samba User

While interacting with multiple systems, it is recommended to have a dedicated user for this purpose. For that, use the below smbpasswd command:

sudo smbpasswd -a <username>

Here’s the breakdown of the command:

  • smbpasswd”: It is a command line utility to manage the Samba username and passwords.
  • -a”: This flag adds the existing Unix/linux user to the Samba user database.

This command searches for the username in the Unix user database and adds it to the Samba user database. If not found, then the “submpasswd” command will create as well as add the user to the

Samba group.

Here, we have used the following command to add the user “ashabbir” to the Samba user database:

sudo smbpasswd -a ashabbir

Now, change the owner of the directory (created in Step 2), to the newly created/added user for Samba:

sudo chown <user>:<usergroup> <Path>

Although it is not necessary but recommended when you are creating a dedicated user. Meanwhile, the shared directory must be owned by that specific user.

Step 4: Add a Service in the Configuration File

Once the above three steps are performed, you need to integrate all these configurations in the main configuration file of Samba. Open the configuration file in any editor, i.e., placed at “/etc/samba/smb.conf”:

sudo nano /etc/samba/smb.conf

Inside this configuration, add the following parameters at the end of the file:

[SambaShare]

path = /home/SambaShare
available = yes
valid users = @ashabbir @root
read only = no
writeable=yes
browseable=yes
public = yes

guest ok = no

Here is the breakdown of the parameters:

  • path” represents the directory path to be shared for Samba share.
  • available” denotes the Samba is available for use. Possible values are “yes” or “no”.
  • valid users” represents the usernames allowed to use this share. For multiple users, you can use their names like @user1 @user2.
  • read only” decides whether the share is read-only or not. Keeping its value “no” grants the “read-write” permissions to the valid users.
  • writeable” allows to write on the shared directory. However, if you have used the “read only = no” then you can skip this parameter.
  • browseable” means that the share can be browsed inside already shared directories/drives or not.
  • public” denotes that the share does not require any authentication.
  • guest ok” parameter decides whether anonymous users are allowed to connect to Samba Share or not.

Note: Only three parameters are necessary for this service, i.e., path, valid users, and read only.

Our configuration file looks, as:

Step 5: Testing the Configuration

Now, test the Samba configuration using the command:

testparm

The services are loaded successfully, you can scroll down and check for your added service:

Use this Samba utility (testparm), whenever you have changed any part of Samba’s configuration.

Step 6: Restart the Service

Once all the configurations are performed, you need to restart the Samba service using the command:

sudo systemctl restart smbd

Check the status of the service to confirm that Samba is working fine:

sudo systemctl status smbd

Step 7: Test the Samba Connection Locally

Once you are all set with the configurations, you can ensure the working of Samba by connecting to your own system. First, you need to install the Samba client with the CIFS-utils using the following command:

sudo apt install samba-client cifs-utils

The cifs-utils provide user tools to interact while using the Samba share.

After installing the prerequisites, use the below command to connect to your system locally:

smbclient '\\localhost\<share-name>' -U <samba-user>

Replace the <share-name> with the share you created and the <samba-user> with the username you have dedicated for the Samba:

You can see the Samba Share has been accessed and we used the “ls” command in it:

And here you go, the Samba is connected successfully and we are good to go for working with Samba remotely.

File Sharing Using Samba in a Network

As discussed, the primary purpose of the Samba server is to transfer files between Windows and Linux systems. Until now, we have set up the Samba Server and have not yet tested the file transfer. So, let’s learn and practice how you can transfer the files to and from Linux/Windows.

Prerequisites: Install the Samba Client (For Linux Systems Only)

For establishing a connection, you need to install the Samba client on the system, i.e., Windows or Linux. Windows systems are by default equipped with the Samba Client (SMB/CIFS protocols are supported by default). However, you have to get the client support on Linux systems by yourself (by installing the “samba-client”). Here is the command to install Samba Client on various linux distributions:

sudo dnf install samba-client #Fedora/RedHat/CentOS/etc
sudo apt install samba-client #Debian/Ubuntu/Mint/etc
sudo pacman -S samba #Arch/Manjaro/etc

Step 1: Check the Status of the Samba Service

First, check the status of the Samba service:

sudo systemctl status smbd

This ensures the server is working fine.

Step 2: Get the IP Address Where the Samba Share is Available

Samba server connects to your system by using the IP address and the User name. Thus, use them correctly.

First, get the IP address of your machine using one of the commands:

hostname -I

ip a

Moreover, you also need to ensure that you are using the correct share name that is available locally and is integrated into your Samba configuration file.

Step 3: Access the Shared Folder

Now, it is time to connect to your Ubuntu system from either Windows or any other Linux distribution.

  • 3.1: From Windows to Linux (Ubuntu)

Open the Windows File Explorer and put the IP address and the Samba share directory name in the following format:

\\<IP-Address-of-linux>\Sambashare

Alternatively, you can access the Shared drive by adding it as a network drive. Right-click on “This PC” and choose “Add a network location”:

Specify the location of the share by following the same syntax used above:

Follow the onscreen instructions and you will soon land in the shared directory with the content, as can be seen below:

The shared network will appear on your Windows PC as:

  • 3.2: From Linux to Linux (Ubuntu)

If you are a terminal geek, just use the following syntax and get access to the Samba Share:

smbclient '\\IP-Address\<share-name>' -U <samba-user>

Open the file manager of Ubuntu (or any other Linux distro) and navigate to “Other Locations”. Here, you have to put the IP Address and the Samba Share name in the following format:

smb://your_ubuntu_server_ip/share_name

Enter the details in the following highlighted box (which appears at the bottom of the Other Locations window):

Enter the user and password (the user that has the permissions to use Samba):

The share will be accessible:

This is all about the installation, configuration, and establishing a Samba-based connection.

How to Uninstall Samba From Ubuntu 24.04

If the Samba has now no use on your Ubuntu, you can remove it using the command:

sudo apt autoremove samba --purge

That’s how you can play with Samba on Ubuntu.

Bottom Line

Samba is a renowned network-sharing utility that offers additional functionality such as domain controlling. You can install the Samba server on Ubuntu using the “sudo apt install samba‘ command. Once, you have completed the prerequisites, you can access the Samba from the client side (windows or Linux) as discussed in this post. So, this post has demonstrated the installation of Samba and also presented its configuration on Ubuntu 24.04.

Categories