什麼是桑巴?
在開始安裝之前,我們先來了解一下 Samba 是什麼以及如何使用它。 Samba 是免費的開源軟件,可在 Windows 和 Linux 系統之間實現簡單易用的文件共享。準確地說,它是 SMB/CIFS 協議的開源實現。
(SMB) 服務器消息塊協議是一種客戶端-服務器通信協議,用於共享對網絡上的文件、打印機、串行端口和其他資源的訪問。 (CIFS) 通用 Internet 文件系統協議是 SMB 協議的一種方言。描述協議各種變體的消息包的集合稱為方言。
這意味著您可以使用 Samba 共享文件和打印服務,並在 Linux/Unix 服務器和 Windows 客戶端之間使用身份驗證和授權、名稱解析和服務公告。
您可以使用 Samba 執行五項基本任務。
- 您可以與您的 Windows 機器共享您的 Linux 驅動器。
- 您可以使用 Linux 計算機訪問 SMB 共享。
- 您可以與您的 Windows 機器共享您的 Linux 打印機。
- 您可以與 Linux 機器共享您的 Windows 打印機。
- 您可以在 Unix / Linux 服務器上設置域控制器,並將 Windows 客戶端與域控制器集成。
Samba 還充當域控制器或常規域成員。此選項使 Samba 成為混合 Windows 和 Linux 計算機的基本工具。
先決條件
- 要安裝 Samba,您需要以具有 sudo 權限的用戶或 root 用戶身份登錄 Linux 服務器。
- 為了簡化本教程中的步驟,請使用 root 用戶。如果您想繼續以用戶身份安裝軟件(具有 sudo 權限),請務必輸入“”。須藤“ 命令位於每個命令的開頭。
- 您還需要一台源/目標 Windows 計算機。
在 CentOS 7 上安裝 Samba
在 CentOS 和其他基於 RedHat 的發行版上的安裝通常最好使用 yum 工具進行管理,因此在 CentOS 7 上安裝 Samba 並使用它來使您的系統保持最新。
首先,刪除可能仍緩存在系統上的所有軟件包。要一次清除有效存儲庫中的所有緩存文件,請使用以下命令:
[[email protected] ~]# yum clean all
接下來,確保所有系統包都已更新。 -y 標誌用於自動檢查提示。
[[email protected] ~]# yum -y update
您現在可以繼續安裝 Samba。 可以使用以下命令輕鬆安裝 Samba: 注意如何再次使用 -y 標誌來自動確認安裝提示。
[[email protected] ~]# yum -y install samba samba-client samba-common
Yum 是一個很好的工具,可以自動安裝 Samba 需要的依賴項。在安裝輸出結束時,您應該會看到與此輸出類似的內容。
Installed:
samba.x86_64 0:4.9.1-10.el7_7 samba-client.x86_64 0:4.9.1-10.el7_7 samba-common.noarch 0:4.9.1-10.el7_7
Dependency Installed:
libsmbclient.x86_64 0:4.9.1-10.el7_7 libwbclient.x86_64 0:4.9.1-10.el7_7
samba-client-libs.x86_64 0:4.9.1-10.el7_7 samba-common-libs.x86_64 0:4.9.1-10.el7_7
samba-common-tools.x86_64 0:4.9.1-10.el7_7 samba-libs.x86_64 0:4.9.1-10.el7_7
Complete!
安裝後,您可以看到 Samba 的版本。
[[email protected] ~]# smbd --version
Version 4.9.1
配置 Samba
安裝 Samba 後,您需要對其進行配置以匹配您的設置和標準。 在您開始在 Linux 機器上配置 Samba 之前,您需要在 Windows 機器上驗證您的工作組。去做這個[このPC]或者[マイコンピュータ]→[プロパティ]→[システムの詳細設定]→[コンピュータ名]右鍵單擊。這將打開帶有所需數據的以下窗口。
或者,您可以運行 cmd(從 Windows 開始框中)並運行以下命令:
net config workstation
此命令顯示有關工作站域的所需信息。你會看到這樣的東西。
現在您已經獲得了有關 Windows 計算機上工作組的信息,您可以繼續在 Linux 系統上進行 Samba 配置。
Samba 配置文件位於 /etc/samba/smb.conf 中。在進行任何更改之前,請務必製作原始配置文件的備份副本。
要備份原始配置文件 smb.conf,請使用以下命令創建名為 smb.conf_orig 的備份副本。
[[email protected] ~]# cp /etc/samba/smb.conf /etc/samba/smb.conf_orig
現在您可以進行編輯了。首先,為匿名文件共享服務配置 Samba。任何人都可以讀取或寫入此共享。首先,創建一個名為“anonymous_shared_directory”的目錄,用於存儲文件。
[[email protected] ~]# mkdir -p /samba/anonymous_shared_directory
接下來,您需要對目錄應用適當的權限。
[[email protected] ~]# chmod -R 0775 /samba/anonymous_shared_directory
[[email protected] ~]# chown -R nobody:nobody /samba/anonymous_shared_directory
如果您像我們一樣使用 SELinux,則需要更改 samba 共享的 SELinux 安全上下文。
[[email protected] ~]# chcon -t samba_share_t /samba/anonymous_shared_directory
然後,要對配置文件進行必要的更改,您需要使用 CLI 文本編輯器(Nano 或 Vim)打開 /etc/samba/smb.conf 文件。
[[email protected] ~]# vim /etc/samba/smb.conf
然後添加並編輯以下指令以配置匿名共享
[global]
workgroup = WORKGROUP
security = user
netbios name = centos7
printcap name = cups
idmap config * : backend = tdb
cups options = raw
map to guest = bad user
[Anonymous]
comment = anonymous file share
path = /samba/anonymous_shared_directory
browseable = yes
writable = yes
guest ok = yes
guest only = yes
read only = no
force user = nobody
現在 Samba 配置文件如下所示:
[[email protected] samba]# cat /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
workgroup = WORKGROUP
security = user
netbios name = centos7
printcap name = cups
idmap config * : backend = tdb
cups options = raw
map to guest = bad user
[Anonymous]
comment = anonymous file share
path = /samba/anonymous_shared_directory
browseable = yes
writable = yes
guest ok = yes
guest only = yes
read only = no
force user = nobody
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
然後不要忘記保存更改並關閉文本編輯器。要檢查您當前的 Samba 設置,請運行以下命令:
[[email protected] samba]# testparm
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Registered MSG_REQ_POOL_USAGE
Registered MSG_REQ_DMALLOC_MARK and LOG_CHANGED
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[Anonymous]"
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Loaded services file OK.
Server role: ROLE_STANDALONE
現在按 Enter 查看服務定義轉儲。
在繼續並啟動 Samba 服務之前,您需要配置防火牆以使用它。 必須打開以下端口才能使 Samba 正常運行:
Port 137 (TCP) - netbios-ns - NETBIOS Name Service
Port 138 (TCP) - netbios-dgm - NETBIOS Datagram Service
Port 139 (TCP) - netbios-ssn - NETBIOS session service
Port 445 (TCP) - microsoft-ds - if you are using Active Directory
其他端口:
Port 389 (TCP) - for LDAP (Active Directory Mode)
Port 445 (TCP) - NetBIOS was moved to 445 after 2000 and beyond, (CIFS)
Port 901 (TCP) - for SWAT service (not related to client communication)
如何為 Samba 服務打開這些端口取決於 Linux 服務器上的防火牆類型。大多數Linux服務器使用csf防火牆,所以首先配置csf.conf文件。讓我們在您選擇的文本編輯器中打開 csf 配置文件。
[[email protected] ~]# vim /etc/csf/csf.conf
現在將端口添加到相應的部分。
# Allow incoming TCP ports
TCP_IN = "20,21,25,53,80,110,139,143,443,445,465,587,993,995,53835"
# Allow outgoing TCP ports
TCP_OUT = "20,21,25,53,80,110,139,143,113,443,445,587,993,995,53835"
最後,保存您的更改並重新加載防火牆規則。
[[email protected] ~]# csf -r
如果您正在運行 Firewalld 服務,請使用以下命令添加該服務:
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=samba
現在重新加載 firewalld 服務。
[[email protected] ~]# firewall-cmd --reload
最後,讓我們啟動 Samba 服務,並確保它在系統啟動時自動啟動。 Samba 使用兩個系統服務運行,smb.service 和 nmb.service。
smbd 服務實現文件共享和打印服務,監聽 139 和 445 端口。 nmbd 服務在客戶端實現 NetBIOS over IP 命名服務,並偵聽端口 137。
讓我們從以下命令開始
[[email protected] ~]# systemctl start smb.service
[[email protected] ~]# systemctl start nmb.service
必須啟用這些服務才能在系統啟動時自動啟動。
[[email protected] ~]# [[email protected] ~]# systemctl enable smb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[[email protected] ~]# systemctl enable nmb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nmb.service to /usr/lib/systemd/system/nmb.service.
要測試匿名共享的新實現,請轉到您的 Windows 計算機並[スタート]->[ファイル名を指定して実行]點擊並輸入服務器的IP地址如下:
下一個,[OK]按下可在文件資源管理器中顯示匿名目錄。
然後雙擊進入目錄,右鍵新建一個文本文檔。
選擇文件名並保存。為此,請將文件命名為 testfile。
創建文件後,確保它可以在您的 Linux 機器上運行
[[email protected] ~]# [[email protected] ~]# ls -l /samba/anonymous_shared_directory
total 0
-rwxr--r--. 1 nobody nobody 0 Mar 30 22:03 testfile.txt
現在您已經了解瞭如何配置 Samba 以使用匿名共享,讓我們繼續安全共享。
為私人共享配置 Samba
這種類型的共享需要用戶名和密碼才能訪問,因此您需要創建一個允許訪問共享的用戶/用戶組。 一旦您創建了一組 Samba 用戶,您就可以輕鬆地將所需的權限一次性分配給所有用戶。
要創建一個新組,請使用以下命令
[[email protected] ~]# groupadd sambausergroup
您已經成功地為您的用戶創建了一個名為“samba 用戶組”的組。
讓我們繼續創建一個用戶。
將其命名為“newtestuser”,使用單個命令將其分配給一個組,並拒絕 shell 訪問(因為 Samba 共享不需要它或它的密碼)。這允許更安全的性行為。
由於 Samba 用戶帳戶與系統帳戶是分開的,我們計劃僅將 Samba 權限授予不需要 shell 訪問/密碼的用戶。但是,在合併時,“桑巴-winbind用於將系統用戶和密碼與 samba 用戶數據庫和其他功能同步的軟件包。
要添加用戶,請使用以下命令
[[email protected] ~]# useradd -s /sbin/nologin -g sambausergroup newtestuser
請注意,我們使用 -s 標誌拒絕用戶 shell 訪問,然後使用第二個標誌 -g 將其分配給組。
用戶還需要做一件事來訪問共享。這是一個密碼。因此,讓我們創建一個用戶的 Samba 訪問密碼。
[[email protected] ~]# smbpasswd -a newtestuser
New SMB password:
Retype new SMB password:
Added user newtestuser.
受密碼保護的共享 samba 用戶需要一個地方來存儲他們的文件,因此他們需要為他們創建一個目錄。此目錄稱為“password_protected_directory”
[[email protected] samba]# mkdir -p /samba/password_protected_directory
創建的組“sambausergroup”中的用戶需要對該目錄中的文件具有讀、寫和運行權限,因此使用以下命令授予他們權限。
[email protected] samba]# setfacl -R -m "g:sambausergroup:rwx" /samba/password_protected_directory
您還需要將 SELinux 安全上下文應用於此安全上下文。
[[email protected] samba]# chcon -t samba_share_t /samba/password_protected_directory/
您現在可以繼續配置此共享。 打開samba配置文件,添加私有共享設置
[[email protected] ~]# vim /etc/samba/smb.conf
[Password Protected]
comment = needs username and password to access
path = /samba/password_protected_directory
valid users = @sambausergroup
guest ok = no
writable = yes
browsable = yes
完整的配置文件如下所示
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
workgroup = WORKGROUP
security = user
netbios name = centos7
printcap name = cups
idmap config * : backend = tdb
cups options = raw
map to guest = bad user
[Password Protected]
comment = needs username and password to access
path = /samba/password_protected_directory
valid users = @sambausergroup
guest ok = no
writable = yes
browsable = yes
[Anonymous]
comment = anonymous file share
path = /samba/anonymous_shared_directory
browseable = yes
writable = yes
guest ok = yes
guest only = yes
read only = no
force user = nobody
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
保存更改後,測試程序” 命令。
[[email protected] samba]# testparm
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Registered MSG_REQ_POOL_USAGE
Registered MSG_REQ_DMALLOC_MARK and LOG_CHANGED
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[Password Protected]"
Processing section "[Anonymous]"
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
就在測試新共享之前,重新啟動 Samba 服務以確保更改生效。
[[email protected] samba]# systemctl restart smb nmb
要進行測試,請從您的 Windows 計算機重新連接,然後[スタート]->[ファイル名を指定して実行]單擊並輸入服務器 IP。 服務器 IP -> 好的

這將創建兩個目錄,一個匿名目錄和一個受密碼保護的目錄。

雙擊受密碼保護的目錄會打開以下提示,您需要在其中輸入“新測試用戶”憑據。

為了確保一切正常,讓我們在受密碼保護的目錄中創建一個新目錄。

為此,該目錄被命名為“test_directory”。

最後,讓我們從 Linux 機器檢查目錄是否存在。
[[email protected] /]# ls -l /samba/password_protected_directory/
total 0
drwxr-xr-x. 2 newtestuser sambausergroup 6 Apr 3 21:12 test_directory
結論是
我們已經了解了 Samba 是什麼、如何安裝以及如何配置和使用 Samba 進行文件共享,但 Samba 作為免費的開源軟件帶來了更多。 您可以配置並使用它從 Linux 訪問連接到 Windows 系統的打印機,或者您可以將其配置為可以與 Linux 服務器集成的 Active Directory 域控制器。
Samba 在連接方面非常靈活,允許您從 Linux 計算機、Windows 計算機和 macOS 連接到 Samba。
設置 Samba 既快速又簡單,因此如果您想在 Windows 和 Linux 機器的混合網絡環境中輕鬆共享文件或打印機,則值得考慮。
如果這篇文章看起來很長而且很複雜,或者如果您擔心您已採取的某些步驟,我們會提供幫助。立即致電 800.580.4985 或進行聊天或聯繫我們的一位知識淵博且經驗豐富的託管顧問。