在異質網路環境中,Linux 與 Windows 的檔案共享一直是管理員的核心需求。Samba 作為 SMB/CIFS 協定在 Linux/Unix 上的開源實現,讓 Linux 伺服器能完美融入 Windows 網域、工作群組,提供檔案、印表機共享與網域控制器功能。本文記錄從安裝、設定到進階應用的實戰經驗。
smbd (檔案/印表機服務、認證)、nmbd (NetBIOS 名稱解析、瀏覽服務)、winbindd (整合 AD/LDAP 認證、ID 映射)。dnf install samba samba-client samba-common cifs-utils -y
# 或 apt install samba samba-common-bin smbclient cifs-utils
systemctl enable --now smb nmb
# 若加入 AD 網域,需同時啟動 winbind
systemctl enable --now winbind
firewall-cmd --permanent --add-service=samba
firewall-cmd --permanent --add-port=137/udp --add-port=138/udp --add-port=139/tcp --add-port=445/tcp
firewall-cmd --reload
[global]
workgroup = MYWORKGROUP # 工作群組/網域名稱
server string = %h Samba %v # 顯示描述
security = user # 使用者級認證 (standalone)
# security = ads # 加入 AD 網域時使用
# realm = EXAMPLE.COM
# kerberos method = secrets and keytab
# winbind use default domain = yes
# idmap config * : backend = tdb
# idmap config * : range = 10000-20000
passdb backend = tdbsam # 密碼資料庫後端
map to guest = Bad User # 驗證失敗映射為 guest
usershare allow guests = yes
# 效能調校
socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072
min protocol = SMB2 # 禁用不安全的 SMB1
max protocol = SMB3_11
# vfs objects = recycle # 回收桶功能
# recycle:repository = .recycle/%U
# recycle:keeptree = yes
[homes]
comment = Home Directories
browseable = no
read only = no
create mask = 0660
directory mask = 0770
valid users = %S
[public]
comment = Public Share
path = /srv/samba/public
read only = no
guest ok = yes
force user = nobody
force group = nogroup
[project]
comment = Project Team Share
path = /srv/samba/project
read only = no
valid users = @projectteam
force group = projectteam
create mask = 0660
directory mask = 2770 # SGID 繼承群組
# 建立系統使用者 (無登入 Shell)
useradd -M -s /sbin/nologin smbuser1
# 設定 Samba 密碼 (獨立於系統密碼)
smbpasswd -a smbuser1
# 啟用帳號
smbpasswd -e smbuser1
# 列出 Samba 使用者
pdbedit -L -v
# 安裝額外套件
dnf install realmd oddjob oddjob-mkhomedir sssd adcli krb5-workstation -y
# 發現網域
realm discover example.com
# 加入網域 (需 Domain Admin 權限)
realm join --user=admin example.com
# 設定 SSSD 自動建立家目錄
# /etc/sssd/sssd.conf 已由 realm 自動配置
systemctl restart sssd
# 設定 Samba 使用 AD
# smb.conf [global] 區段加入 security=ads, realm, kerberos method 等
systemctl restart smb nmb winbind
# 測試
wbinfo -u
wbinfo -g
net ads testjoin
# 安裝 cifs-utils
# 手動掛載
mount -t cifs //server/share /mnt/share -o username=user,password=pass,vers=3.0,uid=1000,gid=1000
# /etc/fstab 永久掛載 (建議使用憑證檔)
# /etc/samba/credentials
# username=user
# password=pass
# domain=WORKGROUP
mount -t cifs //server/share /mnt/share -o credentials=/etc/samba/credentials,vers=3.0,uid=1000,gid=1000,_netdev 0 0
\\server\share 或 IP。net use Z: \\server\share /persistent:yes。[global]
vfs objects = recycle
recycle:repository = .recycle/%U
recycle:keeptree = yes
recycle:versions = yes
recycle:touch = yes
recycle:maxsize = 0
recycle:exclude = *.tmp *.bak ~$*
recycle:excludedir = /tmp /temp
[global]
vfs objects = full_audit
full_audit:prefix = %u|%I|%m|%S
full_audit:success = mkdir rename unlink pwrite
full_audit:failure = none
full_audit:facility = local7
full_audit:priority = NOTICE
# 需配合 rsyslog 將 local7 導向 /var/log/samba/audit.log
| 現象 | 可能原因 | 解決方向 |
|---|---|---|
| Windows 看不到共享 | 防火牆、SMB1 已停用、NetBIOS 名稱解析失敗 | 開放埠口、啟用 SMB2+、檢查 nmbd、使用 IP 直連 |
| 權限被拒絕 | Linux 檔案權限、SELinux 標籤、Samba create mask、force user/group | 檢查 ls -Z、setsebool -P samba_export_all_rw=on、調整 smb.conf |
| 檔案名稱亂碼 | 字元編碼不一致 | 設定 unix charset = UTF-8, dos charset = CP950 (繁中) |
| 大檔案傳輸中斷 | SMB 簽名、網路 MTU、防火牆逾時 | 調整 server signing、MTU 9000、防火牆連線追蹤 |
Samba 是 Linux 融入 Windows 生態系的關鍵基礎設施。從簡單的工作群組共享到企業級 AD 整合、DFS-R 複寫、CTDB 高可用叢集,掌握其核心設定邏輯與權限模型,能讓您輕鬆打造跨平台協作環境。建議從獨立伺服器模式起步,逐步掌握 smb.conf 精髓,再挑戰網域整合與高可用架構。