Saturday, May 25, 2024
HomeTutorialsSamba on Ubuntu: Easy File Sharing on Linux

Samba on Ubuntu: Easy File Sharing on Linux

Samba is a standard Windows interoperability suite of applications for Linux systems. Many Linux users install a Samba server on their devices to interact with Windows systems and share files.

In this tutorial, we’ll show you how to install and configure Samba on Ubuntu.

Let’s get started.

What You’ll Need


Before installing Samba on Ubuntu, let’s see the requirements to follow this tutorial.

  • Ubuntu installed and running on a system
  • A text editor
  • Familiarity with the command line interface
  • A root account or sudo privileges
  • A stable internet connection

For demonstration purposes, we’ll use the Ubuntu 22.04 LTS version for this tutorial. However, it should work for older or newer versions as well.

With that said, let’s now jump right into the tutorial.

Step 1: Install Samba on Ubuntu Linux


Before installing Samba, let’s update our repository list to have the latest packages available. To do that, run the below command:

sudo apt update
Update ubuntu system

After updating your system, you’re ready to install Samba. Install Samba on Ubuntu simply by running this command:

sudo apt install samba
install samba on ubuntu

When prompted for confirmation, press the y button and hit Enter. That should install Samba on your device.

To verify if the installation was successful, you can either check its current version or locate its binary file.

To check Samba’s version, run this command:

samba -V

If you’d like to locate Samba’s binary file, then run this command instead:

whereis samba

samber version checking

As you can see from the above screenshot, we installed Samba successfully on our system. But we also need to run it.

You can check if Samba is already running with this command:

systemctl status smbd
check samba status

From the green active (running) text, we can ensure it works properly. But if not, you’ll have to run it manually.

To run Samba on Ubuntu, use this command:

systemctl start smbd

That should start the Samba service.

Step 2: Create a Shared Directory on Your System


To make your files easily accessible, we will create a separate directory. You’ll store the files you want to share in that directory.

Create the directory with this command:

mkdir -p <parent directory>/<child directory>

When executing this command, replace <parent directory> and <child directory> with suitable directory names. By adding the -p flag, we’re simultaneously creating a directory and sub-directories under it.

You can also verify the created directories with this command:

ls
create directory for samba sharing

With the directories created, let’s move on to configuring Samba.

Step 3: Configure Samba on Ubuntu Linux


After installing Samba, we’ll now configure it for your device using a configuration file. You can find the file in the /etc/samba/ directory.

You can use any Linux text editor to open the file. We’ll use the Nano text editor since it’s simple and easy to use.

To open the Samba configuration file using Nano, run the below command:

sudo nano /etc/samba/smb.conf
samba configuration file

First, scroll down to the Global Settings. We’ll go through each setting and configure them.

Browsing and Identification


We have two settings here—workgroup and server string.

A workgroup is a set of computers sharing the same resources in a local area network (LAN).

In this setting, you should change the workgroup from WORKGROUP to the specified name of your original workgroup.

The server string acts as the unique identifier for your server. You can change it to an easily recognizable name.

workgroup in samba configuration file

Networking


In this category, you can set up the interfaces or IP addresses. The two settings appearing here are interfaces and bind interfaces only.

The first parameter is for determining the network interface Samba will bind to. The second one is a yes/no parameter. Setting it to yes means Samba only binds to the interfaces on the list.

networking in samba configuration file

Debugging


The four settings in this section are log file, max log size, logging, and panic action.

debuggin/accounting in samba configuration file

Authentication


The first setting is the server role. This defines the type of server Samba will be. Set it as a standalone server.

authentication in samba configuration file

For the rest of the settings, you can follow the below format:

authentication in samba configuration file

Domains


In this section, we’ll only deal with the usershare allow guests setting. Set it to yes.

domains in samba configuration file

If you’re done editing all the configurations, then save the settings by pressing Ctrl + O buttons. Then, exit the editor using the Ctrl + X buttons.

The last thing you need to do is test the configuration file for errors. For that, run the below command:

testparm

As you can notice in the picture above, there are no syntax errors. So we can proceed to the next step.

Step 4: Set Up a User Account


Samba has both public shares and private shares. For private shares, we require a username and password. So, let’s set that up now.

First, set the username with the below command:

sudo smbpasswd -a <username>

Replace <username> with the same name as the system user. See the screenshot below for a better demonstration:

We used the system username for the Samba username. After entering the above command, you’ll be asked to create a password as well. Enter a new password and re-enter it for confirmation. Then, press the Enter button to create the user.

If you don’t want to use the current system user for Samba, you must create a new user and add that user to the system group.

In case you created a new user for Samba, you need to grant read, write, and execute permissions to that new user. To do that, run the below command:

sudo setfacl -R -m "u:<username>:rwx" /samba/shared

Remember to replace <username> with the real username you gave to the new user.

Step 5: Configure Samba Share Directory Settings


For this, you will return to the configuration file we worked with earlier. Open that file in Nano with this command:

sudo nano /etc/samba/smb.conf

Scroll to the very bottom of this file. Add the below text to the file:

[shared]
comment = Samba file share
path = /home/samba/shared
read only = no
writable = yes
browseable = yes
guest ok = no
valid users = @zunaid

Once you’re done, save the file with Ctrl + O and exit with the Ctrl + X buttons.

To ensure you didn’t make any mistakes while typing, run this command:

testparm

Once again, there was no problem found in the syntax.

Step 6: Update the Firewall Rules


After configuring Samba, you should configure your firewall to allow traffic from Samba. To do that, simply run the below command:

sudo ufw allow samba

If you get a similar output, then you’ve finished setting up the firewall successfully for Samba.

Step 7: Connect to the Shared Directory


Since we’ve done many configurations in the file, we need to restart Samba to make the changes take effect. Restart Samba with the following command:

sudo systemctl restart smbd

From your desktop, go to Files on the left panel.

Then click on Other locations.

In the Connect to Server box, type in the following:

smb://ip-address/shared

Note that you should put your real IP address here. Additionally, if you used a different directory name, put that instead.

After writing that, press the Connect button.

You will see a prompt asking for a username and password. 

First, select the Registered User option from the Connect As menu. Then, enter your Samba username, Domain, and password. Lastly, click the Connect button.

And that should place the directory in the Windows sharing location.

Final Thoughts


This tutorial covers how to install and configure Samba file sharing on Ubuntu.

We’ve covered the installation process, creating and setting up the shared directory, editing the configuration file, and adding the shared directory to the Windows shared location.

Hopefully, by following this guide, you can set up your own Samba server and seamlessly share files from your Ubuntu desktop.

Want to learn more about file-sharing apps on Linux? Check out our top picks of the best file-sharing software for Linux.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now