Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 部署 Samba 檔案共享服務完整實戰

編輯:Linux 系統常見問題解答

環境說明

  • 虛擬機:VirtualBox / RHEL 5(示範環境,現代系統建議使用 RHEL 8/9、CentOS 7/8、Ubuntu 20.04+)
  • 目標:提供 Windows/Linux 跨平台檔案共享

安裝 Samba 套件

# 掛載安裝光碟作為 YUM 源(離線環境)
mount /dev/cdrom /media/

# 安裝 samba 核心套件與 SWAT 管理介面
yum install -y samba samba-client samba-common samba-swat

若提示缺少 perl(Convert::ASN1),YUM 會自動解決相依性。

啟動服務並設定開機自啟

service smb start
service nmb start
chkconfig smb on
chkconfig nmb on

CentOS 7+ 請改用 systemctl enable --now smb nmb。

建立 Samba 使用者與密碼

Samba 使用者需先存在於系統帳號:

useradd -M -s /sbin/nologin oracle
smbpasswd -a oracle  # 設定 Samba 專用密碼

配置共享目錄 /etc/samba/smb.conf

[oracle_share]
    comment = Oracle Project Share
    path = /home/oracle/share
    browseable = yes
    writable = yes
    valid users = oracle
    create mask = 0664
    directory mask = 0775
    force group = oracle

修改後執行 testparm 驗證語法,再 service smb reload 重載配置。

SELinux 與防火牆放行

# SELinux 允許 Samba 共享家目錄
setsebool -P samba_enable_home_dirs on
semanage fcontext -a -t samba_share_t "/home/oracle/share(/.*)?"
restorecon -Rv /home/oracle/share

# 防火牆開放 139/445 (SMB) 及 137/138 (NMB)
firewall-cmd --permanent --add-service=samba
firewall-cmd --reload

客戶端測試

  • Windows:Win+R → \\IP\oracle_share → 輸入 oracle/密碼
  • Linux:smbclient //IP/oracle_share -U oracle 或掛載 mount -t cifs //IP/oracle_share /mnt -o username=oracle

常見問題排查

  • 權限被拒:檢查 Linux 檔案權限(chown -R oracle:oracle /home/oracle/share)、SELinux 標籤、smb.conf writable 設定。
  • 找不到網路路徑:確認 nmb 服務運行(負責 NetBIOS 名稱解析)、防火牆 UDP 137/138。
  • SWAT 網頁管理:啟用 xinetd 管理的 swat 服務,瀏覽 https://IP:901。
Copyright © Windows教程網 All Rights Reserved