🗂️ Linux Samba Server 安裝與設定教學
Samba 是一個讓 Linux / Unix 系統可與 Windows 系統進行檔案與印表機共享的服務, 它實作了 SMB / CIFS 協定,使 Linux 主機能作為 Windows 的「網路芳鄰」伺服器。 本篇將示範如何安裝與設定 Samba,建立可被 Windows 存取的共用資料夾。
📦 1️⃣ 安裝 Samba 套件
以 CentOS / RHEL / Rocky / AlmaLinux 為例:
yum install samba samba-client samba-common -y systemctl enable smb systemctl enable nmb systemctl start smb systemctl start nmb
✅ 檢查服務狀態:
systemctl status smb systemctl status nmb
若為 Debian / Ubuntu:
apt update apt install samba -y systemctl enable smbd systemctl start smbd
📁 2️⃣ 建立共用資料夾
建立資料夾與存取權限:
mkdir -p /srv/samba/share chown -R nobody:nogroup /srv/samba/share chmod -R 0775 /srv/samba/share
(可依實際需求修改為指定用戶,例如 chown -R user1:user1 /srv/samba/share)
⚙️ 3️⃣ 設定 Samba 共用區
編輯主設定檔:
vi /etc/samba/smb.conf
加入以下段落至檔案底部:
[global] workgroup = WORKGROUP server string = Samba Server %v netbios name = FILESERVER security = user map to guest = Bad User dns proxy = no [ShareFolder] path = /srv/samba/share browsable = yes writable = yes guest ok = yes read only = no create mask = 0775
檢查設定語法是否正確:
testparm
重新啟動服務:
systemctl restart smb nmb
👤 4️⃣ 新增 Samba 使用者
建立系統用戶(若尚未存在):
useradd user1 passwd user1
將用戶加入 Samba 帳號資料庫:
smbpasswd -a user1
啟用帳號:
smbpasswd -e user1
✅ 測試 Samba 登入:
smbclient -L localhost -U user1
💻 5️⃣ 從 Windows 連線測試
在 Windows 檔案總管輸入:
\\192.168.100.120\ShareFolder
登入帳號密碼後即可存取共用目錄。
若啟用了「guest ok = yes」,可直接瀏覽而不輸入帳號。
🧱 6️⃣ 防火牆與 SELinux 設定
開啟必要的 Samba 連線埠:
firewall-cmd --permanent --add-service=samba firewall-cmd --reload
若使用 SELinux,允許 Samba 存取自訂資料夾:
chcon -t samba_share_t /srv/samba/share -R
🔍 7️⃣ 驗證設定
- 查看 Samba 共用列表:
smbclient -L 192.168.100.120 -U user1
tail -f /var/log/samba/log.smbd
netstat -tulnp | grep smb
✅ 小結
- 📦 安裝 Samba 並啟動 smb/nmb 服務
- 📁 建立共用資料夾並設定權限
- 👤 建立 Samba 使用者
- 💻 Windows 可直接透過網路芳鄰或 UNC 路徑存取
若需進階權限控制,可搭配 valid users、write list、hosts allow 等參數。
📚 參考資料
🔗 延伸閱讀
- 🐧🧱 Linux Proxy Server 建置教學(Squid 安裝與設定指南)
- 🔥 LAMP 架構教學:Apache、MariaDB、PHP 整合實戰
- 🔐 Linux 權限與擁有者管理(chmod / chown)教學
— WWFandy・Linux 網路服務筆記
沒有留言: