Samba is a free, open-source software suite that enables file, folder, and printer sharing between Linux and Windows systems. With capabilities for authentication, authorization, name resolution, and service announcement, Samba is a versatile tool compatible with a variety of operating systems, including Linux, Unix, OpenVMS, and many more.
In this tutorial, you will learn how to install and configure Samba as a standalone sharing server on CentOS 8.
Prerequisites
- A server running CentOS 8
- A root password configured on your server
Step 1: Install Samba Server
The Samba package is available in the CentOS default repository. Install it using the following command:
dnf install samba samba-common samba-client -y
Once installed, start the SMB service and enable it to launch after a system reboot with:
systemctl start smb systemctl enable smb
Verify the Samba service status with:
systemctl status smb
You should see an output similar to:
? smb.service - Samba SMB Daemon Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2020-03-02 23:03:30 EST; 8s ago Docs: man:smbd(8) man:samba(7) man:smb.conf(5) Main PID: 2072 (smbd) Status: "smbd: ready to serve connections..." Tasks: 4 (limit: 25028) Memory: 33.8M CGroup: /system.slice/smb.service ??2072 /usr/sbin/smbd --foreground --no-process-group ??2074 /usr/sbin/smbd --foreground --no-process-group ??2075 /usr/sbin/smbd --foreground --no-process-group ??2076 /usr/sbin/smbd --foreground --no-process-group
Step 2: Create a Public Share with Samba
Create a public share that can be accessed by all users without requiring a password.
Create a Public Share Directory
First, create a shared folder named ‘public’ and add two files:
mkdir -p /samba/share/public touch /samba/share/public/file1.txt touch /samba/share/public/file2.txt
Then, set the appropriate permissions and ownership:
chmod -R 0755 /samba/share/ chmod -R 0755 /samba/share/public chown -R nobody:nobody /samba/share chown -R nobody:nobody /samba/share/public
Configure Samba
To configure Samba for sharing the public directory, first backup the existing configuration file:
mv /etc/samba/smb.conf /etc/samba/smb.bak
Create a new Samba configuration file:
nano /etc/samba/smb.conf
And add these lines to the file:
[global] workgroup = WORKGROUP server string = Samba Server %v netbios name = samba-server security = user map to guest = bad user dns proxy = no [Public] path = /samba/share/public browsable =yes writable = yes guest ok = yes read only = no
Save the file and restart the Samba service to apply changes:
systemctl restart smb
Check your Samba configuration:
testparm
Expected output:
Load smb config files from /etc/samba/smb.conf Loaded services file OK. Server role: ROLE_STANDALONE Press enter to see a dump of your service definitions # Global parameters [global] dns proxy = No map to guest = Bad User netbios name = SAMBA-SERVER security = USER server string = Samba Server %v idmap config * : backend = tdb [Public] guest ok = Yes path = /samba/share/public read only = No
Configure SELinux and Firewall
Adjust SELinux booleans and set security context on the share directory:
setsebool -P samba_export_all_ro=1 samba_export_all_rw=1 semanage fcontext -a -t samba_share_t "/samba/share/public(/.*)?" restorecon /samba/share/public
Allow Samba service through the firewall:
firewall-cmd --add-service=samba --zone=public --permanent firewall-cmd --reload
Accessing Samba Share from Ubuntu Gnome
To access the Samba share, open the Gnome file manager on your Ubuntu system and click on Connect to Server:
Enter your Samba server’s IP address and click Connect. You’ll see the Samba share as shown below:
Select the Public directory to view your files:
Accessing Samba Share from Ubuntu Command-line
From the command line, you can list available Samba shares using:
smbclient -L //45.58.38.51
Output:
Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.10.4] Sharename Type Comment --------- ---- ------- Public Disk IPC$ IPC IPC Service (Samba Server 4.10.4) Domain=[WORKGROUP] OS=[Windows 6.1] Server=[Samba 4.10.4] Server Comment --------- ------- Workgroup Master --------- -------
Install cifs-utils package:
apt-get install cifs-utils -y
Mount the Samba share to the /mnt directory:
mount -t cifs //45.58.38.51/public /mnt/
Press Enter without entering a password when prompted:
View contents of the mounted share:
ls /mnt/
Output:
file1.txt file2.txt
Creating a Private Share with Samba
Configure a private share on Samba accessible only to authenticated users.
Create User and Group
Create a group named ‘private’:
groupadd private
Create a new user ‘privateuser’ and assign it to the ‘private’ group:
useradd -g private privateuser
Set a password for ‘privateuser’:
smbpasswd -a privateuser
Example output:
New SMB password: Retype new SMB password: Added user privateuser.
Create a Private Share Directory
Create a private directory and two files within it:
mkdir -p /samba/share/private touch /samba/share/private/private1.txt touch /samba/share/private/private2.txt
Set appropriate permissions and ownership:
chmod -R 0770 /samba/share/private chown -R root:private /samba/share/private
Configure SELinux context:
semanage fcontext -a -t samba_share_t "/samba/share/private(/.*)?" restorecon /samba/share/private
Configure Samba
Open the Samba configuration file to define the private share:
nano /etc/samba/smb.conf
Add these lines at the end of the file:
[Private] path = /samba/share/private valid users = @private guest ok = no writable = yes browsable = yes
Save and close the file, then restart the Samba service:
systemctl restart smb
Verify your Samba configuration:
testparm
Expected output:
Load smb config files from /etc/samba/smb.conf Loaded services file OK. Server role: ROLE_STANDALONE Press enter to see a dump of your service definitions # Global parameters [global] dns proxy = No map to guest = Bad User netbios name = SAMBA-SERVER security = USER server string = Samba Server %v idmap config * : backend = tdb [Public] guest ok = Yes path = /samba/share/public read only = No [Private] path = /samba/share/private read only = No valid users = @private
Access Samba Share from Ubuntu Command-line
View available shares:
smbclient -L //45.58.38.51
Connect to the Samba server and view available shares:
smbclient //45.58.38.51/private -U privateuser
Enter the password when prompted to access the Samba shell:
Enter privateuser's password:
List the share contents:
smb: \> ls
Output:
. D 0 Tue Mar 3 10:03:22 2020 .. D 0 Tue Mar 3 10:01:56 2020 private1.txt N 0 Tue Mar 3 10:03:17 2020 private2.txt N 0 Tue Mar 3 10:03:22 2020 51194 blocks of size 2097152. 49358 blocks available
Exit the Samba shell:
smb: \>exit
Mount the Samba share to the /opt directory:
mount -t cifs -o user=privateuser //45.58.38.51/private /opt
Enter the password as requested:
Password for privateuser@//45.58.38.51/private: *********
Check the mounted Samba share in the /opt directory:
ls /opt/
Output:
private1.txt private2.txt
Accessing Samba Share from Ubuntu Gnome
To access the Samba share, navigate to the Gnome file manager, click Connect to Server:
Enter your Samba server IP address and click Connect. You should now see both your public and private Samba shares:
Select the Private directory, enter your username and password, and click Connect to view your files:
Congratulations! You have successfully set up and configured a Samba server on CentOS 8.
Frequently Asked Questions (FAQ)
What is Samba used for?
Samba is used to facilitate file and printer sharing between Linux and Windows systems, as well as for authentication and authorization.
Is Samba free to use?
Yes, Samba is free and open-source software.
Can I use Samba on operating systems other than Linux?
Yes, Samba can also run on Unix, OpenVMS, and other operating systems.
Is it safe to enable public shares on Samba?
Public shares provide unrestricted access to files and can pose a security risk. Only enable them in secure, controlled environments.
How do I manage users and groups in Samba?
You can use Linux user and group management commands alongside Samba-specific commands like smbpasswd
to manage users and groups.
Can Samba be used as a domain controller?
Yes, Samba can be configured as a domain controller to manage Windows clients.