最近我們的小團(tuán)隊(duì)需要在服務(wù)器上共分出一個(gè)共享文件夾用于大家存放公共的資源文檔, 大家想啊,這肯定很簡單呀,在Windows下面只要?jiǎng)?chuàng)建相關(guān)的windows account,共享某個(gè)文件夾,把讀/寫權(quán)限給我們創(chuàng)建的account的,就完成了共享,但在Linux下面就沒有這么美好了,網(wǎng)上查閱資源資料多指向通過Samba完成共享任務(wù),但一些blog只介紹了怎么做,但沒有為什么這么 做,搭建工作且不太順利,對(duì)Linux算不上熟悉,走了很多彎路,所以通過這篇blog深入理解其中的每一步。
Samba的簡介
Samba是在Linux和UNIX系統(tǒng)上實(shí)現(xiàn)SMB協(xié)議的一個(gè)免費(fèi)軟件,由服務(wù)器及客戶端程序構(gòu)成。這些是廢話….. 來看點(diǎn)有意思的。作者Tridgwell申請(qǐng)使用SMBServer ( Server Message Block 的簡寫 ) 注冊(cè)這個(gè)軟件的商標(biāo), 因?yàn)镾MB 是沒有意義的文字而沒有辦法注冊(cè)。然后他就翻字典,看到SAMBA一遍正好包含SMB幾個(gè)字母 ,這這個(gè)詞也是我們熟知的拉丁舞蹈的名稱,然后就有了三八這個(gè)名字🙄。(自百科)
搭建Samba共享目錄, 如果需要使用用戶名/密碼的形式訪問共享目錄,我們需要先創(chuàng)建Linux的user,然后通過smbpasswd創(chuàng)建samba用戶(用戶名需要一致),原文在這里:
To provide authentication on a standalone host, you have to create the accounts locally on the operating system and additionally in the Samba database. By default, Samba uses the tdbsam back end and stores the database in the /usr/local/samba/private/passdb.tdb file. Optionally set a different location in the smb.conf file using the passdb backend parameter. See the smb.conf 5 man page for details(from https://wiki.samba.org/index.php/Setting_up_Samba_as_a_Standalone_Server).
搭建需要用戶名驗(yàn)證的共享目錄
1. 創(chuàng)建共享目錄的用戶, 我們這里使用來組(group)來演示
groupadd smbgrp
useradd fielshare -s /sbin/nologin -g smbgrp -p <password>
#創(chuàng)建同名的smb用戶, 這里的密碼和local用戶的密碼是完全獨(dú)立的,我們最后用的通過smbpasswd創(chuàng)建的用戶
smbpasswd -a fielshare
2. 創(chuàng)建需要共享的工作目錄,設(shè)置好文件夾的權(quán)限
mkdir -p /srv/samba/secure
chmod -R 0770 /srv/samba/secure
chown -R root:smbgrp /srv/samba/secure
搭建Samba共享目錄, 如果需要使用用戶名/密碼的形式訪問共享目錄,我們需要先創(chuàng)建Linux的user,然后通過smbpasswd創(chuàng)建samba用戶(用戶名需要一致),原文在這里:
3. 修改安全上文
chcon -t samba_share_t /srv/samba/secure
這條命令是SELinux(詳見Security-Enhanced Linux)下面的命令, 作用提把/srv/samba/securel切換到samba的上下文中。
4. 修改配置文件smb.conf
修改配置文件之前 ,我們先做好備份工作,以防不測。
cp /etc/samba/smb.conf /etc/samba/smb.conf.orig
在這里我們有以下事情需要做:
1.在[global] section下修改workgroup為WORKGROUP (就是我的電腦=>屬性=> 計(jì)算機(jī)名看到的工作級(jí)的名字)

2.設(shè)置[global] 下的netbios name, 這個(gè)可以是任意,就是我們?cè)谖业姆监徬驴吹降挠?jì)算機(jī)名稱
3. 確定 [global] 下security設(shè)置為user
4.添加共享目錄的配置
#為暴露在我的芳鄰里點(diǎn)進(jìn)去看到的文件夾名稱
[share]
comment = Secure File Server Share
# 為需要共享的目錄
path = /srv/samba/secure
# 可訪問的用戶,多用戶用空格隔開, 以@開頭為用戶組
valid users = @smbgrp
# 關(guān)閉匿名訪問,設(shè)置為no
guest ok = no
writable = yes
browsable = yes
整個(gè)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
netbios name = centos
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = no
cups options = raw
[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
[share]
comment = secure file share
path = /srv/samba/secure
valid users = @smbgrp
guest ok = no
writable = yes
browsable = yes
browseable = yes
注意smb.conf默認(rèn)會(huì)有[home]節(jié)點(diǎn),如果不是不想得一個(gè)和用戶名同名的文件夾,請(qǐng)刪除它。
完成編輯, 保存配置文件,

執(zhí)行testparm后會(huì)得到下面相似的結(jié)果,就是說配置文件沒有問題
[root@localhost software]# testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[printers]"
Processing section "[print$]"
Processing section "[share]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global]
load printers = No
netbios name = CENTOS-SHARE
printcap name = cups
security = USER
idmap config * : backend = tdb
cups options = raw
[printers]
browseable = No
comment = All Printers
create mask = 0600
path = /var/tmp
printable = Yes
[print$]
comment = Printer Drivers
create mask = 0664
directory mask = 0775
force group = @printadmin
path = /var/lib/samba/drivers
write list = @printadmin root
[share]
comment = secure file share
path = /home/share
read only = No
valid users = @smbgrp
[root@localhost software]#
5. 重啟samba服務(wù), 打開我的電腦進(jìn)行測試
systemctl restart smb.service
systemctl restart nmb.service
由于測試機(jī)和Linux主機(jī)不在同一個(gè)網(wǎng)絡(luò),我的芳鄰里面找不到我配置的芳鄰 CENTOS-SHARE, 這里我通過IP直接訪問
6. 別忘了添加防火墻,不然你是看不到你的芳鄰的
firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd --reload


總結(jié)
這里只演示了使用了用戶名的驗(yàn)證模式來共享文件夾,主要是針對(duì)Windows的,對(duì)這一塊不熟悉的同學(xué)可以自行嘗試匿名共享。在設(shè)置過程中,我接觸到以前沒有接觸到東西SELinux,這一塊還是有很多的東西的。對(duì)于SAMBA的使用介紹網(wǎng)上有不少文章的,寫這遍博客的目的也算是多個(gè)視角來告訴大家如何使用。