How to Install and Configure Samba on Ubuntu Server 20.04

We will learn how to install Samba as our network storage server. Samba is a collection of open source applications that implement Server Message Block (SMB) and Common Internet File System (CIFS) protocols on Unix systems.

If your server is using any firewall system, make sure to open the necessary network ports. Samba runs on TCP 139 and 445 and UDP ports 137 and 138.

Step 1 : Install the Samba server with the following command:

sudo apt-get update
sudo apt-get install samba -y

Step 2 : After installation is complete, you can check the Samba version with the following command:

smbd --version

Step 3 : Next, we need to configure Samba to enable sharing on the network. First, create a backup of the original configuration file:

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

Step 4 : Next, open smb.conf and replace its contents with the following:

workgroup = WORKGROUP
server string = Samba Server
netbios name = ubuntu
security = user
map to guest = bad user
dns proxy = no
path = /var/samba/shares/public
browsable =yes
writable = yes
guest ok = yes
read only = no
create mask = 644

Step 5 : Next, we need to create a shared directory:

sudo mkdir -p /var/samba/shares/public

Step 6 : Change the directory permissions to make it world writable

sudo chmod 777 /var/samba/shares/public

Step 7 : Restart the Samba service for the changes to take effect

sudo service smbd restart

Step 8 : Now you can access this Samba share on the Windows client/MacOS

Adding users to the Samba server

We installed the Samba server and created a public share accessible to everyone. In this recipe, we will learn how to add authentication to the Samba server and password protect shared directories.

Follow these steps to add users to the Samba server:

Step 1 : Create a new user account. You can use any existing account or add a new Samba only account with the following command. Change smbuser to your desired username:

sudo useradd -d /home/smbuser -s /sbin/nologin smbuser

Step 2 : Now, we need to allocate a Samba password to this new user. First, enter your sudo password, followed by the new password for your Samba account, and then verify the password:

sudo smbpasswd -a smbuser

Step 3 : Create a shared directory for this user and change its ownership:

sudo mkdir -p /var/samba/shares/smbuser
sudo chown smbuser:smbuser /var/samba/shares/smbuser

Step 4 : Next, edit the Samba configuration to add the preceding share

path = /var/samba/shares/smbuser
browsable = yes
writable = yes
valid users = smbuser

Step 4 : Save the changes to the configuration file and reload the Samba server

sudo service smbd reload

Step 8 : Now you can access this Samba share

Add Comment