Mastering Samba on Linux: Install, Configure, and Manage File Sharing
Learn how to install and configure Samba on Linux, covering the service’s history, required packages, daemon processes, main configuration file settings, command‑line tools, SELinux integration, and step‑by‑step client and server setup for Windows and Linux file sharing.
Table of Contents
1. Fundamentals
1.1 Introduction to Samba Service
In 1987 Microsoft and Intel defined the SMB (Server Message Block) protocol to simplify file and printer sharing within LANs. In 1991 a university student, Tridgwell, created the SMBServer program based on SMB to enable file sharing between Linux and Windows systems.
Required packages: Samba server package Samba-client client package Samba-common common files package
Samba consists of two daemon processes: smbd and nmbd, each started independently.
nmbd
Start script: /etc/rc.d/init.d/nmb Provides NetBIOS name service for CIFS‑based sharing environments
NetBIOS name service is a Windows host‑communication mechanism; CIFS is generally preferred
Listens on UDP port 137 (UCP protocol)
smbd
Start script: /etc/rc.d/init.d/smb Handles file and printer sharing, user authentication, and file locking
Listens on TCP ports 139 and 445
1.2 Installing Samba Service
[root@localhost ~]# yum install samba
Loaded plugins: langpacks, product-id, subscription-manager
…
Installing:
samba x86_64 4.1.1-31.el7
Transaction Summary
================================================================================
Install 1 Package
samba.x86_64 0:4.1.1-31.el7
Complete!2. Samba Main Configuration File
The main configuration file of Samba is similar to Apache's, containing global parameters that affect all shares and section parameters that affect individual shares. Custom shares are defined in the file.
Main configuration file:
/etc/samba/smb.conf2.1 Global Settings
Global settings define the overall sharing environment and apply to every defined share.
2.2 Private Home Directories
Settings that prevent sharing of user home directories
2.3 Printer Sharing
Settings that enable sharing of printer devices
2.4 Custom Shares
Settings for user‑defined shared resources
3. Samba Command‑Line Tools
smbclient– interactive data access smbpasswd – manage Samba account passwords pdbedit – edit the SMB account database testparm – test configuration syntax and display effective settings
3.1 smbclient Command
Notes: smbclient can specify username and password directly or interactively
List all shares of a host: smbclient -L HOST -U USERNAME Access a specific share:
smbclient //SERVER/shared_name -U USERNAME # 1. List shares on an IP address
smbclient -L 198.168.0.1 -U username%password
# 2. Use smbclient like an FTP client (cd, lcd, get, put, mput, etc.)
smbclient //192.168.0.1/tmp -U username%password
# 3. Run a single command
smbclient -c "ls" //192.168.0.1/tmp -U username%password
smbclient //192.168.0.1/tmp -U username%password
smb:/>ls
# 4. Mount remote share via mount or smbcount
mount -t cifs -o username=escape,password=123456 //192.168.0.1/tmp /mnt/tmp
umount /mnt/tmp3.2 smbpasswd Command
Samba uses separate passwords from the system accounts. The smbpasswd command adds or changes a Samba password for a system user.
[root@localhost ~]# smbpasswd -a escape3.3 pdbedit Command
pdbeditmanages the SMB account database. Use the -a option when adding a new account.
[root@localhost ~]# pdbedit -a escape4. Practical Demonstration
4.1 Server Configuration
Step 1: Create Samba user account
Samba uses user‑mode authentication; the account must exist on the system.
Create the account in the Samba database:
[root@localhost ~]# id escape
uid=1000(escape) gid=1000(escape) groups=1000(escape)
[root@localhost ~]# pdbedit -a -u escape
new password: ********
retype new password: ********
Unix username: escape
…Step 2: Create shared directory
Consider file permissions and SELinux context.
Set SELinux context for the share:
[root@localhost ~]# mkdir /home/database
[root@localhost ~]# chown -Rf escape:escape /home/database
[root@localhost ~]# semanage fcontext -a -t samba_share_t /home/database
[root@localhost ~]# restorecon -Rv /home/database
restorecon reset /home/database context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:samba_share_t:s0Step 3: Configure SELinux policies
Enable Samba to access user home directories:
# getsebool -a | grep samba
… (list of samba booleans, all off) …
# setsebool -P samba_enable_home_dirs onStep 4: Edit /etc/samba/smb.conf
# vim /etc/samba/smb.conf
[global]
workgroup = MYGROUP
server string = Samba Server Version %v
log file = /var/log/samba/log.%m
max log size = 50
security = user
passdb backend = tdbsam
load printers = yes
cups options = raw
[database]
comment = Do not arbitrarily modify the database file
path = /home/database
public = no
writable = yesStep 5: Restart Samba services
# systemctl restart smb
# systemctl enable smb
ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
# iptables -F
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]4.2 Windows Client
Samba solves resource‑sharing problems between Linux and Windows systems.
Access a share by entering \\SERVER_IP in the Windows Run dialog.
If the Linux firewall rules are cleared, the Samba login screen appears.
Enter the Samba username (e.g., escape) and the password set with pdbedit to log in.
4.3 Linux Client
Samba also enables file sharing between Linux systems.
Install the client package cifs-utils:
# yum install cifs-utils
Loaded plugins: langpacks, product-id, subscription-manager
…
Installing:
cifs-utils x86_64 6.2-6.el7
Transaction Summary
================================================================================
Install 1 Package
cifs-utils.x86_64 0:6.2-6.el7
Complete!Create an authentication file with Samba credentials and restrict its permissions:
# vim auth.smb
username=escape
password=redhat
domain=MYGROUP
# chmod 600 auth.smbAdd a mount entry to /etc/fstab so the share persists after reboot:
# mkdir /database
# vim /etc/fstab
…
//192.168.10.10/database /database cifs credentials=/root/auth.smb 0 0
# mount -aOr mount manually:
# mount -t cifs //SERVER/shared_name /mount_point -o username=USERNAME,password=PASSWORDAfter mounting, files created from Windows appear in /database and can be edited on Linux.
# cat /database/Memo.txt
i can edit it .Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
