Install and Configure Samba on CentOS to Connect Windows and Linux Networks

samba linux

A typical corporate network has Windows desktops for the users and Linux servers for storing and processing data.

As a result of this configuration, network administrators need a way to make these two operating systems communicate with each other and share assets such as printers and file servers.

Samba is a great solution that network administrators can use to set up printer and file sharing among Linux and Windows devices on the network.

This article introduces Samba and then goes into the details of how you can set up and configure Samba on a CentOS-based network.

Let’s start with an introduction to Samba.

Table Of Content

  1. What is Samba
  2. Prerequisites For Installing Samba on CentOS
    1. A Linux/Unix-based OS
    2. Sufficient System Resources
    3. Network Connectivity
    4. Firewall Configuration
    5. User Accounts
  3. How to Install Samba on CentOS 7 Server
    1. Step # 1: Use Yum to Clear and Update Package Index
    2. Step # 2: Install Samba on CentOS
    3. Step # 3: Verify If the Installation Was A Success
  4. Samba Configuration For Optimal Performance
    1. Verify Windows Workgroup
    2. Back Up Samba Configuration File on Linux
    3. Start Editing Samba Configuration File
    4. Verify The New Samba Settings
    5. Change Firewall Rules
    6. Start Samba Service
    7. Test Anonymous Share
  5. Conclusion

What is Samba?

Samba is a software utility that sets up file and printer sharing between Windows and Linux/Unix computers on the network.

The Samba server uses the Server Message Block (SMB) protocol, which Windows utilizes for sharing files and printers. As a result, Samba allows Windows clients to access Linux-based file and printer servers and Linux clients to access Windows-based file and printer servers.

Samba is a versatile tool that sysadmins can use for enterprise and home networks to enable seamless file and printer sharing across different operating systems.

The best part about Samba is its wide range of configurations and easy customizability that streamlines Samba to match the needs of users and organizations.

Samba is released under the GPL, and thus anyone can use, modify and distribute it freely.

After that short introduction, let’s check out the prerequisites for installing Samba on CentOS server.

Prerequisites For Installing Samba on CentOS

The following are the essential requirements for installing Samba on CentOS server.

A Linux/Unix-based OS

Samba works with all popular Linux/Unix-based OS, such as Ubuntu, Debian, CentOS, and Red Hat Enterprise Linux. For the purpose of this article, we’ll use CentOS.

Sufficient System Resources

Samba has low system requirements consisting of CPU, RAM, and disk space. However, the volume of exact server resources Samba needs for optimal performance depends upon the number of clients and the volume of data being transferred across the networks.

Network Connectivity

Since Samba operates across multiple networks, it needs a properly configured network interface that connects to highly-available network connectivity.

Firewall Configuration

You need to make sure that Samba is whitelisted and that the firewall allows the traffic through ports allocated to Samba.

User Accounts

Samba requires access to user accounts, including accounts with sudo privileges. Samba needs these accounts to control shared resources. You might need to create user accounts For Samba.

How to Install Samba on CentOS 7 Server

Now that you have gone through the prerequisites, let’s dive into the actual steps required to install Samba on a CentOS server.

Step # 1: Use Yum to Clear and Update Package Index

On CentOS and other RHEL based distros, YUM is a great option for managing packages and handling installation.

Before actually installing Samba, removing cached files is a good idea. This ensures a clean slate for the installation process.

Start with the following command:

# yum clean all.

The next step is to update all installed packages so that the installation doesn’t break because of any dependencies. Note that the following command has the -y flag to confirm any prompts automatically.

# yum -y update

yum update

Step # 2: Install Samba on CentOS

At this point, we’re ready to start the installation.

We’ll use the following command to install the Samba server, client, and related files. Note that we have the -y flag to auto-confirm the installation prompts.

# yum -y install samba samba-client samba-common

While the command is executing, Yum will download and install dependencies. The screen should look similar to the following:

yum samba dependencies

Step # 3: Verify If the Installation Was A Success

Once the installation finishes, you can do a quick test to check that Samba has been properly installed and is ready for the next steps.

This simple check involves printing out the Samba version by entering the following command in the terminal:

# smbd --version

smbd version

Samba Configuration For Optimal Performance

Before you start using Samba for sharing resources across various networks, you need to configure the settings so that Samba is able to recognize and communicate with the relevant groups and resources.

Verify Windows Workgroup

Often the first configuration step is to find out the Windows workgroup.

To do this, right-click “This PC” or “My Computer” > Properties > Advanced System Settings > Computer Name.
This will show a window with the data we need.

verify windows workgroup

Next, get the Windows workstation’s domain and related information.

Launch the Windows Command Prompt by entering “cmd” in the Search box (found near the Start menu).

Next, enter the following command:

net config workstation

You will see something similar to the following:

net config workstation

Back Up Samba Configuration File on Linux

Before we continue configuring Samba settings on the Linux machine, it’s important that we first create a backup of the original configuration file. This backup would be used to restore the original settings in case things go wrong.

Use the following command to create a copy of the Samba configuration file with the name “smb.conf_orig”:

# cp /etc/samba/smb.conf /etc/samba/smb.conf_orig

Start Editing Samba Configuration File

Now that we have backed up the file, we can start editing the file.

In this demonstration, we’ll set up Samba so anyone can read and write anonymous files across the networks.

We start by creating a directory named “anonymous_shared_directory” on the Linux machine. This directory is where our test files would go.

# mkdir -p /samba/anonymous_shared_directory

Next, set the proper permissions for this directory.

# chmod -R 0775 /samba/anonymous_shared_directory

# chown -R nobody:nobody /samba/anonymous_shared_directory

If you are using SELinux, change the shared directory’s security context. The new context should as below

# chcon -t samba_share_t /samba/anonymous_shared_directory

Now that the directory is all set up, we can start making changes in the config file.

For this, open the /etc/samba/smb.conf file in your preferred text editor. Since we like Vim, the most popular CLI text editor, we’ll use the following command in the terminal to open the file:

# vi /etc/samba/smb.conf

Now, we need to configure our anonymous share by adding and editing the following directives:

smb conf

Samba’s configuration file typically contains many options, but we only need to change a few for our purposes. After the changes, the file should look like the following screenshot:

samba configuration

After making the changes, remember to save the file before closing the text editor.

Verify The New Samba Settings

You can now verify your current Samba settings by running the following command:

# testparm /etc/samba/smb.conf

The output should be as below:

Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Server role: ROLE_STANDALONE

The command will let you know whether the configuration file is loaded properly. In case it didn’t, you might see errors and warnings instead of the success message.

Change Firewall Rules

Before starting the Samba service, you must configure the CentOS server firewall to allow the ports Samba uses for internal and external data.

Specifically, you need to check the following ports:

allow required ports under firewall

Depending on the type of firewall installed on your Linux server, the process for opening ports for Samba services will vary.

Since the CSF firewall is a very common firewall on Linux servers, we’ll use it to demonstrate the process of allowing traffic through Samba ports.

The process involves editing the CSF config file. For this, open the csf.conf file in your chosen text editor. As we mentioned earlier, we prefer CLI editors. So we’ll open the file in Nano:

# nano /etc/csf/csf.conf

You need to add the Samba’s ports (mentioned above) to the config file. This way, you can easily connect devices and ensure everything works correctly.

csf firewall ports

Remember to save the file. Next, restart the firewall service with the following command to ensure the changes you made take effect.

# csf -r

Start Samba Service

At this point, we’re ready to start Samba services. This requires entering two commands in the terminal:

smbd service – this service provides file sharing and printing capabilities and listens on ports 139 and 445. The syntax of the command is:

# systemctl start smb.service

nmbd service – this service offers NetBIOS over IP naming services to clients and listens to port 137. Here’s the command’s syntax:

# systemctl start nmb.service

If you want these services to be enabled at server bootup, you need to “enable” these services. The right syntax of the commands is:

# systemctl enable smb.serviceCreated symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.

# systemctl enable nmb.serviceCreated symlink from /etc/systemd/system/multi-user.target.wants/nmb.service to /usr/lib/systemd/system/nmb.service.

Test Anonymous Share

Now that the ports are enabled at the firewall and the services activated, we can proceed to test the anonymous share process. For this, follow these steps:

  • Go to your Windows computer and click Start > Run.
  • Enter the Linux server’s IP address and click OK. This will bring up the Anonymous directory in the file explorer.
  • Double-click to access the directory, and right-click to create a new text file.
  • Choose a file name, and save it. For this demo, For example, we will name our file “test file.”
  • Next, switch over to the Linux machine and run the following command in the terminal to find this file:

#  ls -l /samba/anonymous_shared_directory

Now, if you want to test the connection from the Windows end, simply go to Start > Run. Enter the server IP address. Click ‘OK,’ and this should connect you to the server.

samba test connection

And now, you can access the Anonymous directory.

successful samba test connection

Conclusion

Samba is a flexible networking protocol connecting Linux, Windows, and macOS machines. Samba is used to allow printer sharing between Windows and Linux systems. A Linux server can be added to a Windows network by setting it up as an Active Directory Domain Controller. If you want to exchange data between Windows and Linux systems, Samba is a viable option because it is simple to set up and use.

FAQ:

Q. What is Samba?

A. Samba is a free, open-source software that allows file and print sharing between different operating systems. It provides authentication and authorization services for users and groups.

Q. On which ports does the Samba server work?

A.Samba uses two different types of ports to communicate with other devices available in a network. The first port, port 139, is for NetBIOS over TCP/IP, and the second port, port 445, is used for SMB over TCP/IP. SMB is a more modern and secure protocol that is used by default on newer versions of Windows.

Q. What is SWAT?

A.SWAT is GUI based administration tool used for the Samba server

Q. What is the nmbd daemon?

The nmbd is a daemon/service part of the Samba suite of tools. This service is responsible for NetBIOS name resolution services for clients on the network. NetBIOS (Network Basic Input/Output System) is an older protocol used by Windows and other operating systems for file and printer sharing before SMB (Server Message Block).