Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Windows 綜合教學 >> 關於 Windows

Windows 與 Linux 檔案系統互存取完整方案:Samba (CIFS) 與 mount.cifs 雙向共享實戰

編輯:關於 Windows

Windows 原生支援 CIFS/SMB 協定,Linux 透過 Samba 提供 CIFS 服務端與客戶端,可實現雙向無縫檔案共享。本文以 Fedora/CentOS 為例,詳細說明「Linux 做 Samba Server 共享給 Windows」與「Windows 共享資料夾掛載到 Linux」兩大經典架構的安裝、配置、驗證全流程。

架構一:Linux (Samba) → Windows (Client)

安裝 Samba

yum install samba -y

配置 /etc/samba/smb.conf

[root]
    comment = root
    path = /
    create mask = 0644
    writeable = yes
    browseable = yes
    valid users = root

建立 Samba 密碼並啟動服務

smbpasswd -a root   # 設定密碼
service smb restart # 或 systemctl restart smb
ps -ef | grep smbd  # 確認進程

Windows 存取驗證

  • 總管網址列輸入 \\192.168.1.110 → 輸入 root/密碼 → 可瀏覽 Linux 根目錄。
  • 右鍵共享資料夾 →「對應網路磁碟機」→ 指定磁碟機代號 → 勾選「重新連線時重新連接」→ 完成,Windows 應用程式可像本地磁碟一樣讀寫。

架構二:Windows (Server) → Linux (Client, mount.cifs)

Windows 建立共享資料夾

  1. 確認「Server」服務啟動 (services.msc)。
  2. 右鍵資料夾 →「內容」→「共享」→「進階共享」→ 勾選「共享此資料夾」→ 設定共享名、權限(讀取/變更/完全控制)。

Linux 掛載 CIFS 共享

mkdir -p /root/sourcecode
mount -t cifs -o username="Test" //192.168.1.100/sourcecode /root/sourcecode
# 輸入密碼後掛載完成,ls /root/sourcecode 即可看到檔案

開機自動掛載 (/etc/fstab)


//192.168.1.100/sourcecode  /root/sourcecode  cifs  username=Test,password=Pass,iocharset=utf8,vers=3.0  0  0

※ 建議使用憑證檔 credentials=/etc/smbcred 避免密碼明文寫入 fstab。

常見問題排查

  • Windows 防火牆阻擋:開啟 TCP 445、139、UDP 137、138;或暫時關閉防火牆測試。
  • SELinux 阻擋:setsebool -P samba_export_all_rw=1 或設定正確 context。
  • 權限不符:Linux 掛載後檔案擁有者為掛載用戶,需配合 uid=xxx,gid=xxx,file_mode=0644,dir_mode=0755 選項。
  • SMB 協定版本:新版 Linux 預設協商 SMB 3.0,舊版 Windows (XP/2003) 需加 vers=1.0 或 vers=2.0。

總結

Samba (CIFS) 是跨平台檔案共享的標準解法,成熟、效能佳、支援 ACL/加密/多通道。若環境為純 Linux 叢集,NFS 更輕量;但異構環境下,CIFS 為首選。

Copyright © Windows教程網 All Rights Reserved