How to install Samba server on CentOS/RHEL 7

In this article, you will learn to install the Samba server on CentOS/RHEL 7 and basic file sharing configuration for the Windows machine.

Samba is an open-source software package. Also, it is free software and available under the GNU (General Public License). So, you can modify and redistribute it. It uses the Server Message Block (SMB) and Common Internet File System (CIFS) as a protocol.

Samba is useful for file and print services. It supports different versions of Microsoft Windows operating systems, Mac operating systems, and Linux operating systems.

Samba Features

Samba is a powerful tool. So, it has many abilities. But, I’m listing some of them below.

  • It can run as an Active Directory Domain Controller.
  • Serve directory trees and printers to Linux, UNIX, and Windows clients.
  • Assist in network browsing (with or without NetBIOS)
  • Authenticate Windows domain logins
  • Provide Windows Internet Name Service (WINS) name server resolution
  • It acts as a Windows NT-style Primary Domain Controller (PDC)
  • Act as a Backup Domain Controller (BDC) for a Samba-based PDC
  • Act as an Active Directory domain member server

Samba Alternatives

There are some alternatives available for Samba. But, they do not have all the features like Samba. So, just for the knowledge, you can find it useful.

  • NQE
  • Power Broker Identity Services
  • NFS
  • RazDC
  • Sharity

Preconditions

  • A VPS or a local server on which CentOS/RHEL 7 is running.
  • A Microsoft windows machine to verify the Samba server setup.
  • You must log-in with a root user or a user with sudo privilege to make the changes.
  • Internet connection

Step 1: Update the Server

So, it is a good practice to check the server for any pending updates. However, it is not necessary to update the server only for the Samba server. You can use the below command to check for system updates.

sudo yum update

If it is aksing for permission, then press y for confirmation and then hit the enter key.

Step 2: Install the Samba server on CentOS/RHEL 7 

So, as per now, the Samba software package is available in the default CentOS/RHEL 7 repository. But, to install it you have to use the below commands.

sudo yum install samba samba-client samba-common

In case, if it is asking for confirmation to install the software, press to confirm and then press the enter key.

Step 3: Starting the Samba server services after install

So, after completing the installation process, now we have to start the Samba services. Also, we have to enable on startup. The below command will start the service, and it will also enable the service to start automatically on the startup of the system at the same time.

sudo systemctl enable --now smb nmb

Note: The smb service helps to file sharing and printing services. And, it listens on TCP ports 139 and 445. However, the nmb service helps to NetBIOS over IP naming services to clients. And, it listens on UDP port 137.

Step 4: Check the status of Samba services

So, it is time to confirm the status of the Samba services if it is running or not. Use the below command to verify that and also you can see in the picture.

sudo systemctl status smb nmb
smb-nmb-service-status
The output of the samba service status check.

Step 5: Configuring the firewall for Samba server

As you have installed Samba successfully, and you have also started the service of Samba. So, now you have to open the ports on the firewall to access the Samba services. Use the below command to complete the step.

sudo firewall-cmd --permanent --add-service samba

Also, reload the firewall service using the below command.

sudo firewall-cmd --reload

Step 6: Confirming the Windows machine workgroup

In this step, you have to check the Windows machine workgroup. It should be in the same workgroup as the CentOS server has configured. So, open the command prompt and use the below command to check it on the Windows machine.

net config workstation

Also, you can see the output in the below picture.

windows-machine-workgroup-check
Windows machine workgroup check.

Step 7: Taking a backup of the Samba configuration file

The Samba configuration file is in /etc/samba/smb.conf. If you look at the configuration file, then you can find that there are pre-configured settings that explain different configuration directives to supervise you.

Lastly, this is a good practice to take a backup copy of the configuration file. The below command will help you to copy a backup file. Also, preserve the SELinux context.

sudo cp --preserve=context /etc/samba/smb.conf /etc/samba/smb.conf.bak

Setting Samba for Anonymous directory share

Step 8: Creating the anonymous share directory

Firstly, you have to create an anonymous share directory. Also, you have to set the permission for this directory.

For example, I’m creating a directory in partition with the name of samba/anonymous-share. And, I will set the permission 0775 and ownership to nobody. So, use the below set of commands to fulfill the task.

sudo mkdir -p /samba/anonymous-share
sudo mkdir -p /samba/anonymous-share
sudo chown -R nobody:nobody /samba/anonymous-share

Secondly, as we have created the anonymous-share directory. So, now we have to change the SELinux context for this shared directory. Use the below command to change the SELinux context.

sudo chcon -t samba_share_t /samba/anonymous-share
Samba-anonymous-directory-create-install-samba-server
Samba anonymous directory create and set the permission and SELinux context.

Step: 9: Configuring the Samba server for anonymous share directory

Firstly, lets’ open the Samba configuration file. And, modify/add the below lines. The first section global,  modify it and make changes as below. So, to open this configuration file, I’m using the vim editors but you can use it as per your choice.

sudo vim /etc/samba/smb.conf

Add the below lines

[global]
workgroup = WORKGROUP
netbios name = centos
security = user

Also, add the below lines in the last of the configuration file.

[Anonymous]
comment = Anonymous Samba File Share
path = /samba/anonymous-share
public = yes
browsable =yes
writable = yes
guest ok = yes
read only = no
force user = nobody

Note: you can change the above lines if you want to allow for anonymous write or not.

Secondly, you can verify the Samba configuration file using the below command.

testparm

Also, If you want to see all the configuration then you can press the Enter key. You can see in the below picture.

testparm-install-samba-server
testing the configuration file after modification.

Lastly, after making the changes, restart the Samba services.

sudo systemctl restart smb nmb

Step 10: Testing anonymous share

So, this is the time to check our anonymous file share on the Windows machine.

  • Press Windows key + r
  • It will bring a run box.
  • Type there your machine hostname or IP address in the below format
  • If you are typing hostname then it will be like \\hostname
  • And, if you are using IP address then it will be like \\IP_address for example, \\192.168.43.20

windows-run-box-samba

  • So, hit Enter key. And it will bring the anonymous directory. You can try to create some files and verify at your Samba server end.
anonymous-login-windows
The output of the Anonymous directory

Setting Samba for Secure file sharing

Step 11: Creating a samba group

You have to create a group. And after creating the group, you can add the users in that group, and also you can set the password for each user. So, let’s create it. You can follow the below commands.

Firstly, creating a group with the name sambagroup

sudo groupadd sambagroup

Secondly, adding a user sahil in sambagroup

sudo usermod -aG sambagroup sahil

Lastly, create a Samba password for the user sahil using the following command

sudo smbpasswd -a sahil

Step 12: Creating a Samba directory for share secure

So, now create a directory where you can keep the shared files and also give permission as per your choice. Also, you have to change the SELinux context.

sudo mkdir -p /samba/secure
sudo chmod -R 0770 /samba/secure
sudo chown -R root:sambagroup /samba/secure
sudo chcon -t samba_share_t /samba/secure

As a result, you can see in the below picture.

samba-secure-directory-create-install-samba-server
Creating a new directory for Samba secure share.

Step 13: Configuring the Samba server for secure share directory

Firstly, open the Samba configuration file. And, add the below lines. So, to open this configuration file, I’m using the vim editors but you can use it as per your choice.

sudo vim /etc/samba/smb.conf

Add the below lines

[Secure]
comment = Secure File Server Share
path = /samba/secure
valid users = @sambagroup
guest ok = no
writable = yes
browsable = yes

Secondly, you can verify the Samba configuration file using the below command.

testparm

Also, If you want to see all the configuration then you can press the Enter key. You can see in the below picture.

testparm-install-samba-server
testing the configuration file after modification.

Lastly, after making the changes, restart the Samba services.

sudo systemctl restart smb nmb

Step 14: Testing the secure share directory

So, this is the time to check our secure directory share on the Windows machine.

  • Press Windows key + r
  • It will bring a run box.
  • Type there your machine hostname or IP address in the below format
  • If you are typing hostname then it will be like \\hostname
  • And, if you are using IP address then it will be like \\IP_address for example, \\192.168.43.20
windows-run-box-samba
This is the run box, enter here your samba server machine ip address.

 

  • So, hit Enter key. And it will bring the box to enter the credentials. So, go ahead and put the username and password as we have created above in Step 11.
windows-user-login-screen
Enter the user credential of your Samba which we have created in step 11.
  • After entering the username and password, if it is correct then you will get the secure directory window and you can test by creating some files in it.
windows-secure-file-samba-server
The final window of the secure share directory.

Step 15: Managing Samba Services

There are some commands to manage samba services like start, stop, restart, enable, disable.

Firstly, if you want to start the service then you can use the command (I’m using smb and nmb both service once but you can also use it separately.)

sudo systemctl start smb nmb

To stop the services

sudo systemctl stop smb nmb

Also, to check if it is enabled on startup or not.

sudo systemctl is-enabled smb nmb

If it is not enabled on startup then you can use the below command to start it on machine startup

sudo systemctl enable smb nmb

Conclusion

In this tutorial, you learn to install and basic configuration of the Samba server. So, that’s it about install the samba server. I hope, you understand but if you have any questions, you can ask in the comment section.

For further reading about Samba, you can find it useful.

 

2 Comments

  1. Florencia Marv Nadbus
    Florencia Marv Nadbus

    Really enjoyed this blog. Much thanks again. Really Cool.

  2. Alexis Worden Darline
    Alexis Worden Darline

    Permainan setuju untuk bermain bandarq online ini.

Leave a Reply