How to Install and Configure Samba on Ubuntu Server 22.04 LTS

Samba is an open-source software that allows file and print sharing between different operating systems, such as Linux and Windows. This guide will walk you through installing and configuring Samba on Ubuntu Server 22.04 LTS, including setting up user authentication.


Step 1: Update Your System

Before installing any new packages, update your systemโ€™s package list and upgrade existing packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install Samba

To install Samba, run the following command:

sudo apt install samba -y

Once the installation is complete, verify that Samba is installed:

smbd --version

Step 3: Configure Samba Shared Directory

Create a directory that will be shared:

sudo mkdir -p /srv/samba/share

Set the appropriate permissions:

sudo chown -R nobody:nogroup /srv/samba/share
sudo chmod -R 0777 /srv/samba/share

Step 4: Configure Samba Settings

Edit the Samba configuration file:

sudo nano /etc/samba/smb.conf

Add the following configuration at the end of the file:

[Shared]
   path = /srv/samba/share
   browsable = yes
   writable = yes
   guest ok = yes
   read only = no
   create mask = 0777
   directory mask = 0777

Save the file (CTRL + X, then Y, then ENTER).


Step 5: Restart Samba Service

After modifying the configuration file, restart the Samba service:

sudo systemctl restart smbd
sudo systemctl enable smbd

To check the status of the Samba service:

sudo systemctl status smbd

Step 6: Create Samba User and Set Password

To create a Samba user, use the following command:

sudo useradd -M -s /sbin/nologin sambauser

Then, set a password for the Samba user:

sudo smbpasswd -a sambauser

Enable the user in Samba:

sudo smbpasswd -e sambauser

Step 7: Allow Samba Through Firewall

If your firewall is enabled, allow Samba traffic:

sudo ufw allow samba

Check the firewall status:

sudo ufw status

Step 8: Access the Samba Share

From a Windows Machine:

  1. Open File Explorer.
  2. Type \\<Ubuntu-Server-IP>\Shared in the address bar and press Enter.
  3. If prompted, enter the Samba username (sambauser) and password.

From Another Linux Machine:

Run the following command:

smbclient \\<Ubuntu-Server-IP>\Shared -U sambauser

Enter the password when prompted.


Conclusion

You have successfully installed and configured Samba on Ubuntu Server 22.04 LTS. Now, your server can share files with Windows and Linux systems. You can further refine access control by modifying the smb.conf file.

If you encounter any issues, check Samba logs:

sudo journalctl -u smbd --no-pager | tail -n 20