It is important to ensure that all system packages are updated before proceeding with the installation. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade
Installing Samba on Ubuntu is easy thanks to the packages available in the official repositories. Run the following command to install Samba:
sudo apt install samba
Once installed, we need to configure Samba to share files and directories. The main Samba configuration file is located at /etc/samba/smb.conf.
Open the configuration file with your favorite text editor, for example, nano:
sudo nano /etc/samba/smb.conf
Inside this file, you can define the sections and parameters needed to share directories. Below is a basic configuration example for sharing a directory:
[global]
workgroup = WORKGROUP
server string = %h server (Samba, Ubuntu)
dns proxy = no
[shared]
path = /srv/samba/shared
browsable = yes
writable = yes
guest ok = yes
read only = no
create mask = 0755
In this example:
[global] section contains general settings, such as the workgroup name (workgroup = WORKGROUP) and the server name (server string = %h server (Samba, Ubuntu)).
[shared] section defines a shared resource named "shared" located in the /srv/samba/shared directory. This resource is browsable (browsable = yes), writable (writable = yes), and allows guest access (guest ok = yes).
We need to create the directory to be shared and adjust permissions so that Samba can access it:
sudo mkdir -p /srv/samba/shared
sudo chown -R nobody:nogroup /srv/samba/shared
sudo chmod -R 0755 /srv/samba/shared
After making changes to the configuration, we need to restart the Samba service for the changes to take effect:
sudo systemctl restart smbd
sudo systemctl restart nmbd
To ensure that the Samba service is running correctly, check its status:
sudo systemctl status smbd
sudo systemctl status nmbd
If you have a firewall configured on your server, you need to allow Samba traffic. You can do this using ufw (Uncomplicated Firewall) with the following command:
sudo ufw allow samba
To access the shared resource from a Windows machine, open File Explorer and enter the following in the address bar:
\\<server_ip_address>\shared
where <server_ip_address> is the IP address of your Ubuntu server.
If you prefer to enable authentication instead of allowing anonymous access, you can create Samba users with the following command:
sudo smbpasswd -a username
This command will create a Samba user based on an existing system user. Make sure the Ubuntu user already exists before running this command.
Jorge García
Fullstack developer