Install and Configure Samba Share on Centos 7 with Easy Steps
This guide is designed to walk you through the process of how to Install and Configure Samba Share on Centos 7. Samba is a powerful suite of applications that enables a Linux server to perform critical network tasks like file serving, user authentication and authorization, name resolution, and print services. In essence, Samba bridges the gap between Linux and Windows environments.
Samba achieves this by implementing the Server Message Block (SMB) protocol. This allows Windows clients to seamlessly access Linux directories, printers, and files hosted on a Samba server, as if they were interacting with a native Windows server. This transparency is key for integrating Linux servers into primarily Windows-based networks.
A particularly important feature of Samba is its ability to act as a Domain Controller. This functionality allows the Linux server to authenticate users against a Windows domain, eliminating the need to create and maintain separate user accounts on the Linux server itself. This simplifies user management and ensures consistent access control across the network.
Note: While CentOS 7 has reached its end-of-life, this guide remains relevant for those who still need to set up Samba File Sharing on CentOS 7. The following steps are provided for informational purposes and should be followed with caution, considering the security implications of using an unsupported operating system. We strongly recommend migrating to a supported operating system.
To follow this guide on Samba file sharing for Centos 7, you should log in to your server as a non-root user with sudo privileges. If you need guidance on this, refer to our guide on Initial Server Setup with Centos 7.
Additionally, for optimal functionality, it’s recommended to disable SELinux. Refer to our guide on How To Disable SELinux on Centos for instructions. Disabling SELinux can reduce security; evaluate the risks before proceeding.
1. Install Samba File Sharing on Centos 7
Samba packages are readily available within the default CentOS repository. Begin by updating your local package index using the following command:
sudo yum update -y
This command ensures that you have the latest package information before installing Samba.
Next, install Samba, its common components, and the Samba client using the following command:
sudo yum install samba samba-common samba-client
This command installs all the necessary dependencies and required packages for Samba to function correctly.
After the installation is complete, start and enable the Samba service using the following commands:
# sudo systemctl start smb.service
# sudo systemctl enable smb.service
The start
command initiates the Samba service, while the enable
command ensures that the service starts automatically at boot time.
To verify that the Samba service is active and running correctly, use the following command:
sudo systemctl status smb.service
This command displays the current status of the Samba service.
**Output**
● smb.service - Samba SMB Daemon
Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
Active: **active** (**running**) since Tue 2023-02-07 05:32:21 EST; 15s ago
Docs: man:smbd(8)
man:samba(7)
man:smb.conf(5)
Main PID: 8929 (smbd)
Status: "smbd: ready to serve connections..."
...
The output should indicate that the service is "active" and "running."
2. Configure Samba File Sharing on Centos 7
Now, you’ll need to modify the Samba configuration file and create the directories that will be shared. Follow these steps:
Set Samba Global Settings
Begin by opening the Samba configuration file using your preferred text editor. Here, we’ll use the vi editor:
sudo vi /etc/samba/smb.conf
Locate the workgroup
line within the [global]
section and ensure it is set correctly. The default is often WORKGROUP
:
workgroup = WORKGROUP
This setting defines the workgroup that the Samba server will belong to. If your Windows network uses a different workgroup, modify this value accordingly.
Save the changes and close the file.
Now, let’s create two directories: one for public sharing and one for private sharing. Use the following commands:
# sudo mkdir /public
# sudo mkdir /private
These commands create the /public
and /private
directories, which will be used for sharing files.
Reopen the Samba configuration file to add the share definitions and authentication methods:
sudo vi /etc/samba/smb.conf
Add the following lines to the end of the file:
[public]
comment = Public Folder
path = /public
writable = yes
guest ok = yes
guest only = yes
force create mode = 775
force directory mode = 775
[private]
comment = Private Folder
path = /private
writable = yes
guest ok = no
valid users = @smbshare
force create mode = 770
force directory mode = 770
inherit permissions = yes
-
[public]
: Defines the configuration for the public share. -
comment
: A description of the share. -
path
: The directory that will be shared. -
writable
: Allows users to write to the share. -
guest ok
: Allows guest access to the share. -
guest only
: Restricts access to guest user only. -
force create mode
: Sets the permissions for newly created files. -
force directory mode
: Sets the permissions for newly created directories. -
[private]
: Defines the configuration for the private share. -
valid users
: Specifies the users or groups that are allowed to access the share. -
inherit permissions
: Allows new files and directories to inherit the permissions of the parent directory.
Save the changes and close the file.
Next, create a Samba share user group that will be used to control access to the private share.
sudo groupadd smbshare
This command creates a new group named smbshare
.
Set the correct ownership and permissions for the private and public shares:
# sudo chgrp -R smbshare /private/
# sudo chgrp -R smbshare /public
These commands change the group ownership of the /private
and /public
directories to smbshare
.
# sudo chmod 2770 /private/
# sudo chmod 2775 /public
These commands set the permissions for the /private
and /public
directories.
Note: The 2
at the beginning of the chmod
commands represents the SGID bit. This ensures that newly created files and directories inherit the group ownership of the parent directory.
Create a no-login Local User
Create a no-login local user to access the private share:
sudo useradd -M -s /sbin/nologin sambauser
-M
: Do not create a home directory for the user.-s /sbin/nologin
: Sets the user’s shell to/sbin/nologin
, preventing them from logging in directly.
Add the user to the Samba share group:
sudo usermod -aG smbshare sambauser
This command adds the sambauser
to the smbshare
group. The -aG
option ensures that the user is added to the group without being removed from any other groups they may already belong to.
Set a password for the Samba user:
sudo smbpasswd -a sambauser
**Output**
New SMB password:
Retype new SMB password:
Added user sambauser.
This command sets the Samba password for the sambauser
. This password is used for authenticating the user when accessing the Samba share.
Enable the created account:
sudo smbpasswd -e sambauser
**Output**
Enabled user sambauser.
This command enables the Samba user account.
Verify Samba Configuration
Verify that the Samba configuration is working correctly:
sudo testparm
**Output**
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global]
printcap name = cups
security = USER
idmap config * : backend = tdb
cups options = raw
[homes]
browseable = No
comment = Home Directories
inherit acls = Yes
read only = No
valid users = %S %D%w%S
[printers]
browseable = No
comment = All Printers
create mask = 0600
path = /var/tmp
printable = Yes
[print$]
comment = Printer Drivers
create mask = 0664
directory mask = 0775
force group = @printadmin
path = /var/lib/samba/drivers
write list = @printadmin root
[public]
comment = Public Folder
force create mode = 0775
force directory mode = 0775
guest ok = Yes
guest only = Yes
path = /public
read only = No
[private]
comment = Private Folder
force create mode = 0770
force directory mode = 0770
inherit permissions = Yes
path = /private
read only = No
valid users = @smbshare
This command checks the Samba configuration file for syntax errors and displays the configured shares. If the output shows "Loaded services file OK," it indicates that the configuration is valid.
Create some demo files in the Samba shares:
# sudo mkdir /private/demo-private /public/demo-public
# sudo touch /private/demo1.txt /public/demo2.txt
These commands create a new directory demo-private
inside the /private
directory and a new directory demo-public
inside the /public
directory. They also create two empty files, demo1.txt
and demo2.txt
inside /private
and /public
directories respectively.
Restart the Samba service to apply the changes:
sudo systemctl restart nmb
Test accessing the share files from the server itself:
smbclient '\localhostprivate' -U sambauser
**Output**
Enter WORKGROUPsambauser's password:
Try "help" to get a list of possible commands.
smb: > ls
. D 0 Tue Feb 7 05:40:59 2023
.. D 0 Tue Feb 7 05:45:28 2023
demo1.txt N 0 Tue Feb 7 05:40:59 2023
demo-private D 0 Tue Feb 7 05:40:52 2023
51057152 blocks of size 1024. 46640276 blocks available
smb: >
This command uses the smbclient
utility to connect to the private
share as the sambauser
. If the connection is successful, it will display a list of files and directories in the share.
To access the share from Windows, open the Run dialog (Win+R), enter the Centos 7 server’s IP address, and click Ok. Enter your Samba user credentials when prompted.
The Samba shared folders should appear. You can open files and create new ones, and they should be visible on the server.
Mount Network Drive
To mount the Samba share permanently on Windows, right-click on This PC -> Map Network Drive. Provide the path details and click Finish. Enter the Samba user credentials when prompted. The share will then be available under This PC.
3. Set up Samba Linux Client
To access the share folders from a Linux client, ensure Samba packages are installed. On a CentOS 7 client:
sudo yum install samba samba-common samba-client
Navigate to File manager -> Other locations and add your share using the syntax:
smb://server-name/Share_name
Enter the Samba user credentials. The Samba share should now be accessible on your Linux client.
Conclusion
You have now learned how to Install and Configure Samba Share on Centos 7. Samba file sharing facilitates file and printer sharing between Windows and Linux computers, creating a seamless network environment.
Now, let’s explore alternative solutions for file sharing on CentOS 7.
Alternative Solutions for File Sharing on CentOS 7
While Samba is a robust solution, other options exist for file sharing, each with its own strengths and weaknesses. Here are two alternatives:
1. Network File System (NFS)
NFS is a distributed file system protocol that allows users on a client computer to access files over a network in a manner similar to how local storage is accessed. NFS is generally considered faster than Samba in Linux-to-Linux environments.
Explanation:
NFS operates by allowing a server to export one or more directories, making them available to clients. Clients can then mount these exported directories, integrating them into their local file system. Security is managed through IP address-based access control and user ID mapping.
Implementation:
-
Install NFS Server:
sudo yum install nfs-utils -y
-
Create a Shared Directory:
sudo mkdir /nfs_share sudo chown -R nobody:nobody /nfs_share
-
Export the Directory:
Edit
/etc/exports
and add the following line:/nfs_share *(rw,sync,no_root_squash,no_all_squash)
*
: Allows access from any host (replace with specific IP addresses or network for better security).rw
: Allows read and write access.sync
: Forces synchronous writes.no_root_squash
: Allows root user on the client to have root privileges on the shared directory.no_all_squash
: Maps all client users to the same UID on the server.
-
Start and Enable NFS Services:
sudo systemctl start nfs-server sudo systemctl enable nfs-server sudo systemctl start rpcbind sudo systemctl enable rpcbind sudo exportfs -a
-
Configure Firewall:
sudo firewall-cmd --permanent --add-service=nfs sudo firewall-cmd --permanent --add-service=rpc-bind sudo firewall-cmd --permanent --add-service=mountd sudo firewall-cmd --reload
-
On the Client:
-
Install NFS Client:
sudo yum install nfs-utils -y
-
Create a Mount Point:
sudo mkdir /mnt/nfs_share
-
Mount the NFS Share:
sudo mount <server_ip>:/nfs_share /mnt/nfs_share
Replace
<server_ip>
with the IP address of the NFS server. -
Make the Mount Permanent (Optional):
Add the following line to
/etc/fstab
:<server_ip>:/nfs_share /mnt/nfs_share nfs defaults 0 0
-
2. Secure Copy (SCP) and Secure FTP (SFTP)
SCP and SFTP provide secure file transfer capabilities over SSH. They are ideal for transferring files between systems, especially when security is paramount.
Explanation:
SCP allows you to securely copy files between a local and a remote host, or between two remote hosts. SFTP provides a more comprehensive file transfer protocol with features like directory listing and file management. Both rely on SSH for encryption and authentication.
Implementation (SFTP):
-
Ensure SSH Server is Running:
CentOS 7 typically has an SSH server running by default. If not, install and enable it:
sudo yum install openssh-server -y sudo systemctl start sshd sudo systemctl enable sshd
-
SFTP Client:
On the client machine, use an SFTP client (e.g., FileZilla, WinSCP, or the command-line
sftp
utility).-
Command-line SFTP:
sftp <username>@<server_ip>
Replace
<username>
with the user account on the server and<server_ip>
with the IP address of the server.
You will be prompted for the user’s password.
Once connected, you can use commands likels
,cd
,get
(download), andput
(upload) to manage files. -
GUI SFTP Client (FileZilla):
Enter the server IP address, username, and password in the FileZilla quick connect bar and click "Quickconnect."
The client will display the local and remote file systems, allowing you to drag and drop files between them.
-
These alternatives offer different trade-offs in terms of performance, security, and ease of configuration. Choose the solution that best fits your specific needs and environment. For Install and Configure Samba Share on Centos 7, the guide above offers a step-by-step guide to setting up a versatile file-sharing system.