# How to Install and Configure Samba on CentOS 8

# 1. Install samba and necessary packages

Log into your server and run the command below to install Samba and its dependencies.

    sudo dnf install samba samba-common samba-client

shell
shell

We must also ensure that the Windows and Linux system are in the same workgroup. So, go to your Windows PC and launch command prompt. Type the command:

From the output, we can clearly see that the workstation domain points to ‘WORKGROUP’.This will also be configured later on the Linux machine.

shell
shell

# 2. Configuring Samba

Having installed Samba, it’s time to make a few configurations. But before we do that, we need to back up the samba config file. So, run the command below:

sudo mv /etc/samba/smb.conf /etc/samba/smb.con.bak

Next, we are going to create a shared folder called shared and assign the necessary permissions and ownership as shown.

 sudo mkdir -p /srv/samba/shared
 sudo chmod -R 0755 /srv/samba/shared
 sudo chown -R nobody:nobody /srv/samba/shared
 sudo chcon -t samba_share_t /srv/samba/shared

shell
shell

Now create a new samba configuration file

 sudo vim /etc/samba/smb.conf

Append the configuration below:

Conf
[global]
workgroup = WORKGROUP
server string = Samba Server %v
netbios name = centos-8
security = user
map to guest = bad user
dns proxy = no

[Anonymous]
path = /srv/samba/shared
browsable =yes
writable = yes
guest ok = yes
read only = no

shell
shell

Save and close the configuration file. To verify that the configuration is sound, run testparm command

testparm

shell
shell

# 3. Allow samba service on the firewall

Next, allow Samba across the firewall so that outside users can access samba shares.

sudo firewall-cmd --add-service=samba --zone=public --permanent
sudo firewall-cmd --reload

# 4. Start and enable Samba services

sudo systemctl start smb
sudo systemctl enable smb

Then confirm if smb service is running:

sudo systemctl status smb

shell
shell

# 5. Accessing Samba share from windows machine

From your Windows PC, press Windows Key + R to launch the Run dialog and type

\\hostname-of-samba server

OR

\\IP-address-of-samba-server

shell
shell

This opens a window below with an ‘Anonymous’ folder.

shell
shell

You can create files either from Samba server or from the client and share it with other users

shell
shell

shell
shell

# Creating secure shares in Samba

sudo groupadd secure_group

Then we shall add a new user to the newly created group

sudo useradd -g secure_group linuxuser

Next, we are going to create a new secure folder and later assign the necessary permissions and file ownership as shown below .

sudo mkdir -p /srv/samba/secure_share
sudo chmod -R 0770 /srv/samba/secure_share
sudo chcon -t samba_share -p /srv/samba/secure_share
sudo chown -R root:secure_group /srv/samba/secure_share

shell
shell

Next, we will assign the samba user a password that will be used when accessing the secured file share. This will prompt you to provide a SMP password and later confirm it.

sudo smbpasswd -a linuxuser

![shell]((https://www.linuxtechi.com/wp-content/uploads/2020/02/smbpasswd-user-centos8.png?ezimgfmt=ng:webp/ngcb22)

Now let’s head back to Samba’s configuration file

sudo vim /etc/samba/smb.conf

Append the config lines shown below:

Conf
[secured]
path = /srv/samba/secure_share
valid users = @secure_group
guest ok = no
writable = yes
browsable = yes

shell
shell

Save & exit and then restart Samba service

sudo systemctl restart samba

# Accessing the Samba secure folder from a Windows System

Again, to access Samba share from your windows system hit Windows Key + R to launch the Run dialogue. Type \\hostname or \\ samba-IP and hit ENTER.

shell
shell

You’ll now notice that we have another folder called secured.

shell
shell

To access it, double click on it and a login pop-up will prompt you for your username and password credentials.

shell
shell

Once done, click on the OK button or simply hit ENTER to access the contents of the folder

shell
shell

# Accessing the Samba secure folder from a Linux machine

To access the shared directories from a Linux system, simply run the command:

smbclient --user=linuxuser -L //192.168.43.13

Provide the password when prompted and hit ENTER

shell
shell

To access the secure share run

smbclient //192.168.43.13/secured -U linuxuser

shell
shell

Feel free to create files and directories to share with other samba users.