Setting Up a Samba Server on CentOS/RHEL for File Sharing
Introduction
Samba is a powerful tool that allows Linux and Windows systems to share files and printers across a network. This guide will walk you through installing and configuring a Samba server on CentOS/RHEL, setting up both public and private shared folders, and managing user access.
Step 1: Install Samba
To install Samba, update your packages and install Samba using the following command:
sudo yum install samba -y
Step 2: Start and Enable Samba Service
Once installed, start and enable the Samba service to ensure it runs on boot.
sudo systemctl restart smbd
sudo systemctl enable smbd
Step 3: Create Shared Directories
Create directories for public and private sharing:
mkdir public private
chmod 777 public private # Give full access to these folders
Step 4: Add Users for Samba
Create user accounts that will have access to the Samba shares. Use /sbin/nologin
to prevent shell access:
useradd user1 -s /sbin/nologin
smbpasswd -a user1 # Set a Samba password for user1
useradd user2 -s /sbin/nologin
smbpasswd -a user2 # Set a Samba password for user2
Step 5: Backup and Edit Samba Configuration
Before modifying the Samba configuration file, create a backup of the original file:
cp /etc/samba/smb.conf /etc/samba/smb.conf.original
Open the Samba configuration file with a text editor:
sudo nano /etc/samba/smb.conf
Step 6: Configure Public and Private Shares
Add the following configuration at the end of the file:
Public Share Configuration
The public share is accessible to everyone and does not require authentication:
[public]
comment = Public Shared Folder
path = /public
writeable = yes
browseable = yes
create mask = 666
directory mask = 777
Private Share Configuration
The private share is restricted to specific users:
[private]
comment = Private Shared Folder
path = /private
writeable = yes
public = no
valid users = user1
browseable = yes
Step 7: Apply Ownership and Permissions
To restrict the private folder to user1
:
chown user1:root /private
Step 8: Restart Samba Service
After updating the configuration, restart the Samba service to apply the changes:
sudo systemctl restart smbd
Step 9: Add Additional Samba Users
If you need to add more users to Samba, use the following commands. Here, we’ll add a user named sambauser
:
sudo adduser sambauser
sudo smbpasswd -a sambauser # Add the user to Samba
sudo smbpasswd -e sambauser # Enable the user in Samba
Step 10: Access Samba Shares from a Windows Machine
To verify the Samba shares are accessible from a Windows machine:
Open File Explorer.
Enter
\\<Server_IP_Address>
in the address bar and press Enter.You should see the
public
andprivate
folders (private will prompt for credentials).
Conclusion
You have now set up a Samba server with both public and private shared folders. This configuration enables both open and restricted access, providing flexibility for file sharing across your network.
With Samba, you can customize permissions, add more users, and expand the configuration to meet the needs of your environment. Happy sharing!