install samba on ubuntu

Samba is a reimplementation of the SMB networking protocol that enables resource sharing between different platforms (mainly Linux and Windows though). 

We’ll explain how you can set up a Samba server on Ubuntu, as well as how you can access the shared files from other systems in this article.

Install Samba Server

The main Samba package and all the required dependencies can be downloaded directly from the Ubuntu repos, so the initial installation is very simple.

Update the package index first to ensure you download the latest Samba version.

sudo apt update

Then, install Samba with

sudo apt install samba

Set Samba Password

Samba uses its own set of passwords stored in /etc/samba/smbpasswd. So, you’ll need to add a user to the sambashare group and set a new password for this user. Then, you’ll be able to access Samba shares as this user. 

We’ll set a password for the current user anup like so

sudo smbpasswd -a anup

Set Up Shared Directory

You can share a folder from your file explorer by right-clicking and selecting Local Network Share.

Enable the Share this folder checkbox, configure the permissions as you see fit, then click on Create Share.

You can also set up a shared directory from the terminal, although the process is a bit longer. First, create a directory to share. We’ll name it smbshare but you can change it to whatever you prefer.

mkdir /home/anup/smbshare

Now, we’ll add the smbshare directory that we just created to the Samba config file. It’s a good idea to backup the original config file before making any changes though. 

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup

Once you’ve made a copy of the config file, edit the original with your preferred text editor.

sudo nano /etc/samba/smb.conf

Add the following configurations at the end of the file.

[directoryname]
path = /home/username/directoryname
valid users = username
read only = no

After replacing the directory and usernames, the config should look something like this.

Save the changes and exit the editor. Then, restart the Samba service to apply the new configs.

sudo systemctl restart smbd

After making such changes, it’s good practice to test smb.conf for any syntax errors.

testparm

Access Share from Client System

Samba uses UDP ports 137 and 138 and TCP ports 139 and 445. The easiest way to open these ports on Ubuntu is by allowing the Samba profile.

sudo ufw allow samba

After allowing Samba traffic through the firewall, you should now be able to access the shares from another system.

If doing so from Windows, enter \\hostip\foldername into the Run dialog box.

As mentioned earlier, Samba is primarily used for accessing Linux files from Windows. If you want to access the shares from a Linux client instead, you’ll need to install the client package first.

sudo apt install smbclient

Now, you can connect to the Samba share like so

smbclient //hostip/directoryname -U username
smbclient //192.168.10.56/smbshare -U anup

Or, you can access the shares from your file explorer. Simply enter the server’s IP address into the Connect to Server field, then enter the username and samba password to authenticate.

Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.