Install and Configure Samba Share on Centos 7 with Easy Steps
This guide aims to teach you how to Install and Configure Samba Share on Centos 7. Samba is a suite of applications that enables a Linux server to perform network functions like file serving, authentication/authorization, name resolution, and print services.
Samba implements the Server Message Block (SMB) protocol, allowing Windows clients to seamlessly access Linux directories, printers, and files hosted on a Samba server, as if they were interacting with a Windows server.
A significant feature of Samba is its ability to enable a Linux server to act as a Domain Controller. This allows user credentials managed within a Windows domain to be used directly, eliminating the need to recreate and manually synchronize these credentials on the Linux server.
Note: While Centos 7 has reached its end-of-life, this guide provides instructions for setting up Samba file sharing on Centos 7 for those who still plan to use it. Remember to prioritize security updates and consider migrating to a supported operating system.
To follow this guide on setting up Samba file sharing for Centos 7, you should be logged in to your server as a non-root user with sudo privileges. You can refer to a guide on Initial Server Setup with Centos 7 for assistance with this.
Additionally, you will need to disable SELinux. You can follow the instructions in a guide on How To Disable SELinux on Centos.
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
Next, install Samba using the following command:
sudo yum install samba samba-common samba-client
This command installs all necessary dependencies and required packages for Samba.
After the installation, start and enable the Samba service on Centos 7 using these commands:
# sudo systemctl start smb.service
# sudo systemctl enable smb.service
Confirm that the Samba service is active and running using the following command:
sudo systemctl status smb.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..."
...
2. Configure Samba File Sharing on Centos 7
To properly configure Samba, you need to modify the Samba configuration file and create shared Samba directories. The following steps outline this process.
Set Samba Global Settings
Open the Samba configuration file using your preferred text editor (here, vi
is used):
sudo vi /etc/samba/smb.conf
Within the [global]
section, locate the workgroup
line and ensure it is configured as follows:
workgroup = WORKGROUP
Save and close the file after making the changes.
You can now create both public and private directories for sharing. Create these directories using the following commands:
# sudo mkdir /public
# sudo mkdir /private
Reopen the Samba configuration file and add the share definitions and authentication methods to the end of the file:
sudo vi /etc/samba/smb.conf
Add the following share configurations:
[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
Save and close the file after adding the share configurations.
Now, create the Samba share user group that will be used to access the private share as defined in the configuration file.
Create the group using the following command:
sudo groupadd smbshare
Set the correct permissions for the private share and public share using these commands:
# sudo chgrp -R smbshare /private/
# sudo chgrp -R smbshare /public
Next, set the appropriate permissions for the directories:
# sudo chmod 2770 /private/
# sudo chmod 2775 /public
Note: The 2
at the beginning of the chmod
commands represents the SGID bit. This ensures that newly created files 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 using the following command:
sudo useradd -M -s /sbin/nologin sambauser
Add the user to the Samba share group on Centos 7 with the following command:
sudo usermod -aG smbshare sambauser
Finally, set a password for the Samba user:
sudo smbpasswd -a sambauser
**Output**
New SMB password:
Retype new SMB password:
Added user sambauser.
Enable the created account using the following command:
sudo smbpasswd -e sambauser
**Output**
Enabled user sambauser.
Verify Samba Configuration
After completing the above steps, you can test whether your Samba configuration on Centos 7 is working correctly using the following command:
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
A successful output from testparm
indicates that your configuration is correct.
At this point, you can create demo files in the Samba shares. To do this, run the following commands:
# sudo mkdir /private/demo-private /public/demo-public
# sudo touch /private/demo1.txt /public/demo2.txt
To apply the changes, restart the Samba service on Centos 7:
sudo systemctl restart nmb
Before setting up Samba clients, you can test accessing your share files with the command below on Centos 7:
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: >
Now, let’s demonstrate accessing the share from Windows. Open a run box using Win+R
, enter your Centos 7 IP address in the box, and click Ok
:
[Windows Run Box Image Placeholder]
You will be prompted to enter your Samba user credentials. Enter the credentials and click Ok
.
[Samba Credentials Dialog Placeholder]
The Samba shared folders on Centos 7 should then appear:
[Samba Shared Folders in Windows Explorer Placeholder]
You can open files and create new files within these shared folders, and they should be visible on your server machine as well.
Mount Network Drive
You can permanently mount the Samba share on your Windows system. Right-click on This PC
-> Map Network Drive
. This will open a window where you can provide the path details and click Finish
.
[Map Network Drive Dialog Placeholder]
Then, enter the Samba user credentials and click Ok
.
The share will then be available under This PC
.
3. Set up Samba Linux Client
You can also access the share folders from a Linux client. For this, you need to have Samba packages installed on your client machine. In this example, the Linux client is also Centos 7:
sudo yum install samba samba-common samba-client
Then, navigate to File manager
-> Other locations
and add your share using the syntax below:
smb://server-name/Share_name
Enter the credentials for the Samba user. That’s it! You have successfully mounted the Samba share on your Linux client machine.
Conclusion
This guide has shown you how to Install and Configure Samba Share on Centos 7. Samba file sharing provides seamless file and printer sharing between Windows and Linux computers over a network, promoting interoperability between different operating systems. This setup facilitates collaboration and resource sharing within a mixed-OS environment. This is a detailed way to Install and Configure Samba Share on Centos 7.
Now you have learned how to Install and Configure Samba Share on Centos 7. Hope you enjoyed it.
Alternative Solutions for File Sharing
While Samba is a robust and widely used solution, other methods exist for sharing files between Linux and Windows systems. Here are two alternatives:
1. Network File System (NFS)
NFS is a distributed file system protocol originally developed by Sun Microsystems. While primarily used in Unix-like environments, NFS can be configured for file sharing between Linux and Windows using an NFS client on the Windows machine.
Explanation:
NFS allows a server to export directories to clients over a network. The clients can then mount these directories as if they were local file systems. This offers transparent access to shared files. However, setting up NFS with Windows clients can be slightly more complex than Samba due to the need for third-party NFS client software.
Steps on Centos 7 (Server Side):
-
Install NFS Server:
sudo yum install nfs-utils
-
Create a Shared Directory:
sudo mkdir /nfs_share sudo chown nobody:nobody /nfs_share
-
Configure
/etc/exports
:Add the following line to
/etc/exports
to share the directory:/nfs_share *(rw,sync,no_subtree_check,no_root_squash)
rw
: Read-write access.sync
: Data is written to disk before the server replies.no_subtree_check
: Disables subtree checking.no_root_squash
: Allows root on the client to have root privileges on the shared directory. Use with caution!
-
Export the Share and Start NFS Services:
sudo exportfs -a sudo systemctl start nfs-server sudo systemctl enable nfs-server sudo systemctl start rpcbind sudo systemctl enable rpcbind sudo systemctl start nfs-lock sudo systemctl enable nfs-lock sudo systemctl start nfs-idmapd sudo systemctl enable nfs-idmapd
-
Firewall Configuration:
Allow NFS traffic through the firewall:
sudo firewall-cmd --permanent --add-service=nfs sudo firewall-cmd --permanent --add-service=mountd sudo firewall-cmd --permanent --add-service=rpc-bind sudo firewall-cmd --reload
Steps on Windows (Client Side):
- Install an NFS Client: Windows does not natively support NFS client functionality. You will need to install a third-party NFS client. Popular options include "Dopus NFS Client" or "WinNFSd" (for a server implementation that allows other Windows machines to access files on your Windows machine via NFS).
- Mount the NFS Share: The exact steps depend on the chosen NFS client. Typically, you will need to specify the NFS server’s IP address and the exported directory path.
Code Example (NFS Export Configuration):
/nfs_share 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
This example allows read-write access to the /nfs_share
directory for clients on the 192.168.1.0/24
network. Remember to replace this with your actual network.
2. Secure Copy (SCP) and Secure FTP (SFTP)
SCP and SFTP are secure file transfer protocols that run over SSH. They provide a secure way to copy files between Linux and Windows systems.
Explanation:
SCP is a command-line utility for securely copying files between systems. SFTP is a more feature-rich protocol that allows for file management operations like listing directories, creating directories, and deleting files. Both are inherently secure as they leverage SSH encryption.
Steps on Centos 7 (Server Side):
-
Ensure SSH Server is Running: Centos 7 typically has SSH installed and running by default. Verify with:
sudo systemctl status sshd
If not running, start and enable it:
sudo systemctl start sshd sudo systemctl enable sshd
-
Firewall Configuration: If the firewall is enabled, allow SSH traffic:
sudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --reload
Steps on Windows (Client Side):
-
Install an SCP/SFTP Client: Many SCP/SFTP clients are available for Windows. Popular choices include:
- PuTTY: Includes
pscp
(SCP client) andpsftp
(SFTP client). - WinSCP: A graphical SFTP and SCP client.
- FileZilla: Supports SFTP.
- PuTTY: Includes
-
Use the Client to Transfer Files: The process depends on the chosen client.
-
Using
pscp
(PuTTY):pscp user@server_ip:/path/to/remote/file C:pathtolocaldirectory
-
Using
psftp
(PuTTY):Connect to the server:
psftp user@server_ip
Use commands like
get
,put
,ls
,cd
to manage and transfer files. -
Using WinSCP: Launch WinSCP, enter the server details (hostname, username, password), select SFTP as the protocol, and connect. You can then drag and drop files between the local and remote systems.
-
Code Example (SCP Command):
pscp sambauser@192.168.1.100:/home/sambauser/myfile.txt C:UsersMyUserDownloads
This command copies myfile.txt
from the /home/sambauser
directory on the server at 192.168.1.100
to the C:UsersMyUserDownloads
directory on the Windows machine. The user sambauser
must have SSH access to the Centos 7 server.
Both NFS and SCP/SFTP offer viable alternatives to Samba for file sharing. NFS provides transparent file access, while SCP/SFTP provides secure file transfer. The choice depends on your specific needs and the complexity you are willing to manage. However, the Install and Configure Samba Share on Centos 7 process is generally simpler and more widely supported in mixed environments.