How to Install and Configure Samba on Ubuntu for File Sharing

This blog provides a step-by-step guide on installing and configuring Samba on Ubuntu, enabling smooth file sharing between Windows and Linux over a network. It covers the installation process, configuring Samba settings, setting up user password, and Samba file sharing process.

What is Samba?

Samba is a free and open-source software suite that provides file and print services between Windows and Unix-like operating systems. It allows computers running different operating systems to share files and printers over a local network. Samba is compatible with the Server Message Block (SMB) protocol, also known as the Common Internet File System (CIFS), which is the standard protocol for sharing files, printers, and various network resources in Windows-based environments.

Samba enables interoperability between Windows and Unix-like systems by implementing the SMB/CIFS protocol. It allows Windows clients to access files and printers shared by a Samba server, and it also allows Unix-like systems to share files and printers with Windows clients. This makes it possible to create mixed Windows and Unix-like network environments, where users can seamlessly access shared resources regardless of the underlying operating system.

Steps to Install and Configure Samba on Ubuntu for File Sharing

In this step-by-step tutorial, we will install Samba on a remote Ubuntu server and demonstrate the Samba file sharing between a Windows server and the Ubuntu server on the same network.

Step 1. Install Samba Linux Server

Open a terminal or SSH into your Ubuntu 20.04 server and using the following command the switch to the root user so you have the permission for later operations. Then, input password as prompted.

$ sudo -i
Switch to root user using sudo -i

Next, update the package repositories to ensure you have the latest package information.

$ apt update
update the package repositories

Once the package repositories are updated, you can install Samba server by running the following command:

$ apt install samba

And type y to confirm the installation.

Install Samba

After the installation, you can verify if the Samba is successfully installed by running the command below. You should get the output as the arrow indicates in the following screenshot.

$ whereis samba
Vefiry if Samba is successfully installed

Step 2. Configure Samba

Now, we need to create a directory for the Samba file sharing. Use the command below to create one.

$ mkdir /home/"username"/sambashare/

In this case, we are create a directory under the /home/administrator and name the directory as "sambashare."

Create a directory for file sharing

Then, we need to write the Samba file sharing directory into the configuration file of Samba, which is /etc/samba/smb.conf. You can use your preferred editor to modify the configuration file. Here, we will use the nano editor.

$ nano /etc/samba/smb.conf
Use nano editor to modify Samba configuration file

Add the following lines to the bottom of the file.

$ [sambashare]
  comment = Samba on Ubuntu
  path = /home/administrator/sambashare
  read only = no
  browsable = yes
Add specific lines to the Samba configuration file

After the editing, press Ctrl and O on your keyboard to save the changes and then, press Ctrl and X to exit from the nano text editor.

Next, we need to restart the Samba service so the the changes we made can take effect. Run the following command.

$ service smbd restart
Restart Samba service

If you have enabled firewall on your Ubuntu server, you should also update the firewall rules to allow Samba traffic by using the following command.

$ ufw allow samba

Step 3. Set Up User Accounts for File Sharing

The username used must belong to a system account. However, as Samba doesn’t use the original password of the system accounts, we need to set up a new password for the account for file sharing. In this case, we will use the administrator user. You should replace the "username" with your actual username.

$ smbpasswd -a "username"
Set up Samba password for system account

Step 4. Connect to the Linux Samba Server and Share Files from Windows

In your Windows system, go to This PC > Computer > Map network drive > Map network drive.

Open Map network drive

In the pop-up window, input the file path as \\ip-address\sambashare. Use the IP address of your linux Samba server. Then, check the option of Connect using different credentials and click Finish. Upon the click, a new Window will pop up to prompt you to enter the user account and password to connect to the server. Lastly, click OK to connect to it.

Connect to the Linux Samba server

Once your've successfully connected to the Linux Samba server, you should be able to share files between Windows and Linux smoothly.

Use the Samba server for file sharing