Installing and Configuring Samba on Ubuntu 22.04
Let’s look at the process of installing Samba software on a cloud server with the Ubuntu 22.04 operating system. This guide is also suitable for installing Samba on Debian. Let’s start with a brief description of this software.
What is Samba Copy link
Samba is a software package developed to provide compatibility and interaction between UNIX-like systems and Windows. The software has been distributed under a free license for over 30 years. Samba ensures seamless integration of servers and PCs running UNIX into an AD (Active Directory) system. This software can be used as a controller and as a standard component of a domain. Thus, users can flexibly configure cloud file storages. Samba provides extensive functionality for managing file and database access rights by assigning specific user groups.
Creating a New Server Copy link
Go to the control panel and create a new server.
Select the Ubuntu 22.04 image and then the minimum server configuration.
After creating the server, connect to it via SSH, and you can begin configuration.
And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS or Dedicated Server.
Adding a User Copy link
This is simple — enter the command:
sudo useradd -p new_server_pass new_server_userInstead of new_server_pass and new_server_user, you can use any password and any username. Enter your own data instead of the example ones. Note that we immediately set the password, which was possible thanks to the -p command.
Installing Samba on Ubuntu Copy link
For convenience, we have broken the installation process into separate steps.
Step 1. Preparation Copy link
To start the installation process, use the following command:
sudo apt install samba -yNow you need to remember the system name of the service. In most cases, it is smbd. Therefore, if you want to call the service, use this name.
First, let’s configure autostart, which is done with the command:
sudo systemctl enable smbdNow start it using the familiar command:
sudo systemctl start smbdThen check the system status using:
sudo systemctl status smbdTo stop Samba, use:
sudo systemctl stop smbdTo restart the service, enter:
sudo systemctl restart smbdIf you want Samba to no longer start automatically, use the command:
sudo systemctl disable smbdThe reload command is used to refresh the configuration.
The following command will forcibly open port 445, as well as 137–139. To allow them in the ufw firewall, use:
sudo ufw allow SambaStep 2. Configuring Anonymous Access Copy link
Suppose you have some remote server located outside your cloud. Network security rules require that you never open direct access to it through its IP. You can only do this through a tunnel, which is already set up. Typically, servers with granted access have the address 10.8.0.1, and this is the address we will use further.
To share data and grant anonymous access to it, first open the configuration file. It is located here: /etc/samba/smb.conf. We recommend making a backup of the clean file — this will help you quickly restore the original program state without needing to reinstall. Now remove all comments, leaving only the code, and enter the command testparm to ensure the program works properly. In the shared folder settings, enter the following parameters:
[share]
comment = share
path = /data/public_share
public = yes
writable = yes
read only = no
guest ok = yesAlso, make sure that the following four fields (mask and mode) have matching numeric values (for example, 0777).
Regarding the specific lines:
[share]— the name of the shared folder, which will be visible to everyone connecting to your server;comment— a comment that can be anything;path— the path to the data storage folder;public— gives permission for public access: if you do not want users to view the folder contents, set this tono;writable— determines whether data can be written to the folder;read only— specifies that the folder is read-only: to allow users to create new files, set it tono;guest ok— determines whether guests can access the folder.
Thus, the folder name and path may differ depending on what values you specify for the shared folder. The comment can also be anything, and for the last four parameters, values are set as yes or no. Now restart the program and check if you can connect to the server from Windows.
Step 3. Configuring Access by User Credentials Copy link
To create access by login and password, you first need to create a new directory and configure permissions. In the configuration file, set all parameters to no (see above), except writable: in this line, the value should be yes, meaning that writing in the folder should be enabled.
Use the mkdir command to create a new directory, then create a user with useradd someone (where someone can be any username) and set a password for them with the command passwd. For example:
passwd somethingNow, with the command below, add the new user and try to log in: if everything is configured correctly, you will have access to the folder.
sudo smbpasswd -a someoneStep 4. Configuring Group Access Copy link
Configuring group access is necessary when you need to create restricted access for specific user groups. In smb.conf, after the line guest ok, additionally specify the following lines (all usernames here are generated simply for example):
valid users = admin, mary_smith, jane_jameson, maria ortega, nathalie_brown
write list = admin, nathalie_brownIn the valid users line, list the users who are granted access to the directory. And in the write list, list those who can modify data in the folder.
In addition, after the force directory mode line, add another line with the following value:
inherit owner = yesThis enables inheritance of created objects. Now save the settings and restart the service, after which the new settings should take effect.
Step 5. Connecting to Samba from Windows and Linux Copy link
For quick connection to Samba from Windows, press Ctrl+E and enter the path. Note that you need to use \\ to indicate the network path to the resource. And to avoid reconnecting to the server each time, you can choose the option to connect the resource as a drive, if your security policy allows it. In the new window, specify the drive letter and fill in the required data.
For connecting to Samba from Linux, you use the cifs utilities, which are installed with the command:
sudo apt install cifs-utils -yNext, the resource is mounted and connected. This is done with:
sudo mount.cifs //10.8.0.1/our_share /shareThe path and resource name can be anything. You can also perform automatic mounting using the configuration file fstab with its own settings.
Step 6. Configuring the Network Trash Bin Copy link
This operation is needed to avoid accidental permanent deletion of files. For this, create the following directory:
[Recycle]
comment = Trash for temporary file storage
path = /directory/recycle
public = yes
browseable = yes
writable = yes
vfs objects = recycle
recycle:repository = .recycle/%U
recycle:keeptree = Yes
recycle:touch = Yes
recycle:versions = Yes
recycle:maxsize = 0
recycle:exclude = *.tmp, ~$*
recycle:exclude_dir = /tmpNow, let’s review line by line what these parameters mean:
vfs objects = recycle— indicates use of the corresponding subsystem;repository— the path for storing deleted data;keeptree— whether to keep the directory tree after deletion;touch— whether to change the timestamps of files when they are moved to the trash;versions— whether to assign a version number if files with identical names are deleted;maxsize— the maximum size of a file placed in the trash. A value of 0 disables limits;exclude— which file types to exclude;exclude_dir— which directories to exclude.
Conclusion Copy link
That’s it — now you know how to install Samba on an Ubuntu cloud server and configure it for your own needs.
Don't forget to check our low-latency US VPS!