Set up Samba client on CentOS/RHEL 7 for file sharing on Windows.

alemartinezz

Alejandro Martínez

Posted on January 23, 2022

Set up Samba client on CentOS/RHEL 7 for file sharing on Windows.

This article is intended as a general guide. Installing and configuring a SAMBA client on CENTOS 7 for file sharing on Windows.

Banner

Step 1 — Check windows machine Workgroups

Before you proceed to configure samba, make sure the Windows machine is in the same workgroup to be configured on the CentOS server. To check the value in windows machine run the command at cmd prompt:



net config workstation


Enter fullscreen mode Exit fullscreen mode

Workspace visualization

Step 2 — Install SAMBA

First install Samba4 and required packages from the default CentOS repositories using the:



dnf install samba* -y


Enter fullscreen mode Exit fullscreen mode

Step 3 — Configure

The main samba configuration file is:



nano /etc/samba/smb.conf


Enter fullscreen mode Exit fullscreen mode

But, before configuring samba, I suggest you to take a backup of the default file like this.



cp /etc/samba/smb.conf /etc/samba/smb.conf.orig


Enter fullscreen mode Exit fullscreen mode

Edit the file according to your needs. In my case I will share the /var/www/html folder and allow the smbgrp group to access it.
Config file 1
Config file 2

Secure Samba file sharing

As root, create the user you want to access the shared folder. In my case I will be creating a group “smbgrp” allowed to access the shared folder. This allows you to add more users in the future.



groupadd smbgrp
# Assign your user to this group.
usermod user -aG smbgrp
# Set a password for this user.
sudo smbpasswd -a user
# Also set the appropriate permissions on the directory.
chmod -R 0775 /var/www/html
# Assign the full control of the shared folder to the user.
chown -R user:smbgrp /var/www/html


Enter fullscreen mode Exit fullscreen mode

In my case, I will be sharing the html folder in /var/www/html. This is useful for web developers, because lets you edit in realtime a file in the centos server from windows. In a classic Apache HTTP server.

Allow Samba server default ports through firewall



firewall-cmd --permanent --add-port=137/tcp
firewall-cmd --permanent --add-port=138/tcp
firewall-cmd --permanent --add-port=139/tcp
firewall-cmd --permanent --add-port=445/tcp
firewall-cmd --permanent --add-port=901/tcp
firewall-cmd --reload


Enter fullscreen mode Exit fullscreen mode

Restart SAMBA services:



systemctl start smb
systemctl start nmb
systemctl status smb
systemctl status nmb


Enter fullscreen mode Exit fullscreen mode

Step 4 — Verify the shared folder

In order to verify the correct installation, create an example file in the shared folder with touch:



touch /var/www/html/hola.txt


Enter fullscreen mode Exit fullscreen mode

In windows, press “Windows Key+R” and submit your ip address, preceded by two inverted slashes: \
Enter to host
You will see your shared folder like this:
Shared folder visualization
If you set the user and folder permissions as described before, you should see a login window. In which you must enter your centOS user and the password selected.
Login Step
There is the hola.txt file created from centOS 7.
File created from linux
Finally, start and enable samba services to start automatically at next boot:



systemctl enable smb.service
systemctl enable nmb.service
systemctl start smb.service
systemctl start nmb.service

Enter fullscreen mode Exit fullscreen mode




# Conclusion

Setting up Samba is easy, and something to consider if you want easy file sharing between Linux and Windows machines, or even Linux and Linux machines. I gave some context of how to set it up, but there are tons of use cases for Samba. You can also tie it in to different authentication/authorization schemes if you’d like an use it with Active Directory as well. Check out Samba.org for more information.

💖 💪 🙅 🚩
alemartinezz
Alejandro Martínez

Posted on January 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related