TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Linux / Networking / Operations

Samba Network Shares for RHEL-Based Linux Distributions

At some point, you're going to have a Linux server that includes directories that clients need to access from your network. Your best bet for this is Samba.
Feb 25th, 2024 6:00am by
Featued image for: Samba Network Shares for RHEL-Based Linux Distributions
Feature clip by Mohamed Hassan from Pixabay.

At some point, you’re going to have a Linux server that includes directories that various users, developers, admins, or clients need to access from your network. If you depend on Linux, your best bet for this is Samba.

Samba is the Linux implementation of the SMB protocol and has been used for years to make sharing both directories and printers across a network, regardless if you want to access the files using Linux or Microsoft Windows. Samba can be installed on most any Linux distribution, but whether you use an Ubuntu or RHEL-based distribution will determine how you achieve success.

I want to show you how to install and configure a Samba share on Rocky Linux, but the process will work for the likes of Red Hat Enterprise Linux, AlmaLinux, OracleLinux, and even Red Hat’s CentOS Stream.

What You’ll Need

To make this work, you’ll need a running instance of Rocky Linux (or one of the distributions mentioned above) and a user with sudo privileges. Of course, you’ll also need a network connection and clients on the same network (that can connect to the Samba server).

That’s it. Let’s get to the sharing.

Installing Samba

The first thing to do is install Samba. Log into your Rocky Linux instance and first make sure everything is up to date with the command:

sudo dnf update

When the update completes, install the necessary Samba components with the command:

sudo dnf install samba samba-common samba-client -y

Once the installation completes, you can then start and enable the Samba service with the command:

sudo systemctl enable --now smb

You can verify the service is running with the command:

sudo systemctl status smb

You should see it listed as active running.

Creating Your First Samba Share

We’re going to create a share in the /srv directory and name it data. Create this directory with the command:

sudo mkdir /srv/data

Of course, you can name that directory anything you like.

After creating the directory, you’ll then need to change the ownership and permission with the following two commands:

sudo chmod -R 755 /srv/data
sudo chown nobody:nobody /srv/data

The first command recursively sets the permissions of /srv/data to 755, which means the owner can do anything with the file or directory, and all other users can read and execute it but not alter the contents. The second command changes the user and group ownership to nobody.

Next, we must make SELinux aware of our changes (so we can keep the security system in enforcing mode) with the command:

sudo chcon -t samba_share_t /srv/data

So far so good.

Configuring the Samba Share

Samba shares are configured in the smb.conf file. Before you open the file, know that it can be a bit overwhelming. Don’t worry because everything we’re going to do is an addition to the bottom of the file and you won’t have to comb through the file to edit it line by line.

Open the file for editing with the command:

sudo nano /etc/samba/smb.conf

We’re going to create a share that configures the following:

  • Set the path to /srv/data
  • Make it browsable to your network
  • Make it writable
  • Prevent guest access
  • Set read-only mode to no
  • Force the 0666 create mode so users can create files
  • Force the 0777 directory mode so users can create directories
  • Make it browseable to our network

The configuration block for this looks like:

[Public]
path = /srv/data
browsable = yes
writable = yes
guest ok = no
read only = no
create mask = 0666
directory mask = 0777

Save and close the file.

Restart Samba with the command:

sudo systemctl restart smb

Opening the Firewall for Samba

Before anyone can access the share, you must first make some adjustments to your firewall. First open the necessary ports with the command:

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

Reload the firewall (so the changes take effect) with the command:

sudo firewall-cmd --reload

Your Samba share is up and running. However, there’s one more problem to resolve. Even though you might have users registered on your Rocky Linux server, Samba isn’t aware of them. Fortunately, there’s a command for that. The problem is that, in order for all of your users to access the share you have to set a Samba password for them. This password will be different than their standard user password.

So, say you have a user named “penguin” who needs access to the Samba share. First, give that user a Samba password with the command:

sudo smbpasswd -a penguin

You’ll be prompted for your sudo password and then to type/verify a Samba password for the user.

Once you’ve taken care of that, you then have to enable the user (sticking with our example above) for Samba which is done with a command like this:

sudo smbpasswd -e penguin

You will not be prompted for a password this time because the above command simply enables the user for Samba.

Now, the penguin user will have access to the Samba share. Of course, you’ll have to take the above steps for all users who need access to the Samba share.

And that’s all there is to create a Samba share on a RHEL-based Linux distribution. Enjoy all that sharing you’ll do across your network.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.