下面根據實際工作中遇到的一個共享目錄的多用戶權限需求案例來說明下Samba用戶權限的設置。
一、需求場景
領導:李一(liyi)
正式員工(zhengshiyuangong):劉二二(liuerer)、於三三(yusansan)、車四四(chesisi)、馮五五(fengwuwu)
實習生(shixisheng):步六六(buliuliu)、王七七(wangqiqi)
李一有整個共享盤的讀寫權限,其中共享盤整體分為存檔區和編輯區,子目錄按照如下順序進行展示。
具體權限設置如下:

二、Samba服務器部署
1)安裝Samba
[root@samba-server ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@samba-server ~]# rpm -qa|grep samba
[root@samba-server ~]# yum install -y samba
2)安全角度考慮,需要設置防火牆策略(不要關閉防火牆)
添加samba服務到防火牆策略中
[root@samba-server ~]# firewall-cmd --add-service samba --permanent
success
重啟防火牆
[root@samba-server ~]# firewall-cmd --reload
success
查看samba服務是否添加到防火牆中:
[root@samba-server ~]# firewall-cmd --list-all|grep samba
services: ssh dhcpv6-client samba
一定要關閉selinux(否則會造成windows客戶機連接Samba失敗)
[root@samba-server ~]# vim /etc/sysconfig/selinux
.....
SELINUX=disabled
[root@samba-server kevin]# setenforce 0
[root@samba-server kevin]# getenforce
Permissive
3)Samba服務配置
[root@samba-server ~]# 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 = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[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 = root
create mask = 0664
directory mask = 0775
[公司共享目錄]
comment = "公司共享目錄下的賬號權限"
path= /data/samba
public = no
valid users = liyi,chesisi,yusansan,liuerer,fengwuwu,buliuliu,wangqiqi,@samba
printable = no
write list = liyi,chesisi,yusansan,liuerer,fengwuwu,buliuliu,wangqiqi
4)創建共享目錄
[root@samba-server ~]# useradd samba
[root@samba-server ~]# mkdir -p /data/samba/編輯區
[root@samba-server ~]# mkdir -p /data/samba/存檔區
[root@samba-server ~]# chown -R samba.samba /data/samba
[root@samba-server ~]# cd /data/samba
[root@samba-server data]# ll samba
總用量 0
drwxr-xr-x 7 samba samba 142 3月 28 03:52 編輯區
drwxr-xr-x 12 samba samba 280 3月 28 03:51 存檔區
5)添加各samba賬號
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin liyi
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin chesisi
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin yusansan
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin liuerer
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin fengwuwu
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin buliuliu
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin wangqiqi
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin zhengshiyuangong
[root@samba-server ~]# useradd -d /data/samba -s /sbin/nologin shixisheng
[root@samba-server ~]# pdbedit -a -u liyi
new password:
retype new password:
.......
[root@samba-server ~]# pdbedit -a -u chesisi
new password:
retype new password:
.......
[root@samba-server ~]# pdbedit -a -u yusansan
new password:
retype new password:
.......
[root@samba-server ~]# pdbedit -a -u liuerer
new password:
retype new password:
.......
[root@samba-server ~]# pdbedit -a -u fengwuwu
new password:
retype new password:
.......
[root@samba-server ~]# pdbedit -a -u buliuliu
new password:
retype new password:
.......
[root@samba-server ~]# pdbedit -a -u wangqiqi
new password:
retype new password:
.......
查看samba賬號列表
[root@samba-server ~]# pdbedit -L
yusansan:1001:
liuerer:1004:
fengwuwu:1006:
liyi:1003:
chesisi:1005:
buliuliu:1009:
wangqiqi:1010:
6)設置共享目錄權限
[root@samba-server samba]# ll
總用量 0
drwxr-xr-x 7 samba samba 142 3月 28 03:52 編輯區
drwxr-xr-x 12 samba samba 280 3月 28 03:51 存檔區
[root@samba-server samba]# cd 存檔區
[root@samba-server 存檔區]# mkdir 01-重大事項
[root@samba-server 存檔區]# chown -R liyi.liyi /data/samba/存檔區/01-重大事項
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/01-重大事項
[root@samba-server 存檔區]# mkdir 02-戰略投資
[root@samba-server 存檔區]# chown -R liuerer.liuerer /data/samba/存檔區/02-戰略投資
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/02-戰略投資
[root@samba-server 存檔區]# setfacl -R -m u:fengwuwu:rx /data/samba/存檔區/02-戰略投資
[root@samba-server 存檔區]# mkdir 03-戰略規划
[root@samba-server 存檔區]# chown -R yusansan.yusansan /data/samba/存檔區/03-戰略規划
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/03-戰略規划
[root@samba-server 存檔區]# setfacl -R -m u:liuerer:rx /data/samba/存檔區/03-戰略規划
[root@samba-server 存檔區]# mkdir 04-公司治理
[root@samba-server 存檔區]# chown -R yusansan.yusansan /data/samba/存檔區/04-公司治理
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/04-公司治理
[root@samba-server 存檔區]# setfacl -R -m u:chesisi:rwx /data/samba/存檔區/04-公司治理
[root@samba-server 存檔區]# setfacl -R -m g:zhengshiyuangong:rx /data/samba/存檔區/04-公司治理
[root@samba-server 存檔區]# gpasswd -a liuerer zhengshiyuangong
[root@samba-server 存檔區]# gpasswd -a yusansan zhengshiyuangong
[root@samba-server 存檔區]# gpasswd -a chesisi zhengshiyuangong
[root@samba-server 存檔區]# gpasswd -a fengwuwu zhengshiyuangong
[root@samba-server 存檔區]# mkdir 05-股東信息
[root@samba-server 存檔區]# chown -R yusansan.yusansan /data/samba/存檔區/05-股東信息
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/05-股東信息
[root@samba-server 存檔區]# setfacl -R -m u:chesisi:rwx /data/samba/存檔區/05-股東信息
[root@samba-server 存檔區]# mkdir 06-上層股東
[root@samba-server 存檔區]# chown -R yusansan.yusansan /data/samba/存檔區/06-上層股東
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/06-上層股東
[root@samba-server 存檔區]# setfacl -R -m u:fengwuwu:rwx /data/samba/存檔區/06-上層股東
[root@samba-server 存檔區]# mkdir 07-投關管理
[root@samba-server 存檔區]# chown -R fengwuwu.fengwuwu /data/samba/存檔區/07-投關管理
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/07-投關管理
[root@samba-server 存檔區]# setfacl -R -m u:chesisi:rx /data/samba/存檔區/07-投關管理
[root@samba-server 存檔區]# mkdir 08-制度信批
[root@samba-server 存檔區]# chown -R yusansan.yusansan /data/samba/存檔區/08-制度信批
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/08-制度信批
[root@samba-server 存檔區]# setfacl -R -m g:zhengshiyuangong:rx /data/samba/存檔區/08-制度信批
[root@samba-server 存檔區]# mkdir 09-部門綜合
[root@samba-server 存檔區]# chown -R yusansan.yusansan /data/samba/存檔區/09-部門綜合
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/09-部門綜合
[root@samba-server 存檔區]# setfacl -R -m g:zhengshiyuangong:rwx /data/samba/存檔區/09-部門綜合
[root@samba-server 存檔區]# mkdir 10-學習共享
[root@samba-server 存檔區]# chown -R yusansan.yusansan /data/samba/存檔區/10-學習共享
[root@samba-server 存檔區]# chmod -R 700 /data/samba/存檔區/10-學習共享
[root@samba-server 存檔區]# setfacl -R -m g:zhengshiyuangong:rwx /data/samba/存檔區/10-學習共享
[root@samba-server 存檔區]# setfacl -R -m g:shixisheng:rx /data/samba/存檔區/10-學習共享
[root@samba-server 存檔區]# gpasswd -a buliuliu shixisheng
[root@samba-server 存檔區]# gpasswd -a wangqiqi shixisheng
[root@samba-server 存檔區]# cd ../編輯區/
[root@samba-server 編輯區]# mkdir /data/samba/編輯區/01-材料
[root@samba-server 編輯區]# chown -R yusansan.yusansan /data/samba/編輯區/01-材料
[root@samba-server 編輯區]# chmod -R 700 /data/samba/編輯區/01-材料
[root@samba-server 編輯區]# setfacl -R -m u:liuerer:rwx /data/samba/編輯區/01-材料
[root@samba-server 編輯區]# mkdir /data/samba/編輯區/02-周報
[root@samba-server 編輯區]# chown -R liuerer.liuerer /data/samba/編輯區/02-周報
[root@samba-server 編輯區]# chmod -R 700 /data/samba/編輯區/02-周報
[root@samba-server 編輯區]# setfacl -R -m u:fengwuwu:rwx /data/samba/編輯區/02-周報
[root@samba-server 編輯區]# mkdir /data/samba/編輯區/03-總結
[root@samba-server 編輯區]# chown -R yusansan.yusansan /data/samba/編輯區/03-總結
[root@samba-server 編輯區]# chmod -R 700 /data/samba/編輯區/03-總結
[root@samba-server 編輯區]# setfacl -R -m u:chesisi:rwx /data/samba/編輯區/03-總結
[root@samba-server 編輯區]# mkdir /data/samba/編輯區/04-記錄
[root@samba-server 編輯區]# chown -R yusansan.yusansan /data/samba/編輯區/04-記錄
[root@samba-server 編輯區]# chmod -R 700 /data/samba/編輯區/04-記錄
[root@samba-server 編輯區]# setfacl -R -m g:zhengshiyuangong:rwx /data/samba/編輯區/04-記錄
[root@samba-server 編輯區]# mkdir /data/samba/編輯區/05-人員
[root@samba-server 編輯區]# chown -R yusansan.yusansan /data/samba/編輯區/05-人員
[root@samba-server 編輯區]# chmod -R 700 /data/samba/編輯區/05-人員
[root@samba-server 編輯區]# setfacl -R -m g:zhengshiyuangong:rwx /data/samba/編輯區/05-人員
[root@samba-server 編輯區]# setfacl -R -m g:shixisheng:rwx /data/samba/編輯區/05-人員
[root@nextcloud ~]# cd /data/samba/存檔區
[root@nextcloud 存檔區]# ll
總用量 0
drwxrwx---+ 2 liyiyi liyiyi 10 3月 28 03:53 01-重大事項
drwxrwx---+ 2 liuerer liuerer 10 3月 28 00:01 02-戰略投資
drwxrwx---+ 2 yusansan yusansan 10 3月 28 00:05 03-戰略規划
drwxrwx---+ 2 yusansan yusansan 10 3月 28 02:19 04-公司治理
drwxrwx---+ 2 yusansan yusansan 10 3月 28 02:19 05-股東信息
drwxrwx---+ 2 yusansan yusansan 10 3月 28 02:20 06-上層股東
drwxrwx---+ 2 fengwuwu fengwuwu 10 3月 28 02:20 07-投關管理
drwxrwx---+ 2 yusansan yusansan 10 3月 28 02:23 08-制度信批
drwxrwx---+ 2 yusansan yusansan 10 3月 28 02:33 09-部門綜合
drwxrwx---+ 2 yusansan yusansan 10 3月 28 03:54 10-學習共享
[root@nextcloud 存檔區]# cd ../編輯區/
[root@nextcloud 編輯區]# ll
總用量 0
drwxrwx---+ 2 yusansan yusansan 10 3月 28 03:56 01-材料
drwxrwx---+ 2 liuerer liuerer 10 3月 28 02:39 02-周報
drwxrwx---+ 2 yusansan yusansan 10 3月 28 03:33 03-總結
drwxrwx---+ 2 yusansan yusansan 10 3月 28 02:41 04-記錄
drwxrwx---+ 2 yusansan yusansan 10 3月 28 02:43 05-人員
由於李峰有整個共享盤的讀寫權限,所以還需給liyi賬號設置所有子目錄的讀寫權限
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/01-重大事項
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/02-戰略投資
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/03-戰略規划
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/04-公司治理
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/05-股東信息
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/06-上層股東
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/07-投關管理
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/08-制度信批
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/09-部門綜合
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/存檔區/10-學習共享
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/編輯區/01-材料
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/編輯區/02-周報
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/編輯區/03-總結
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/編輯區/04-記錄
[root@samba-server ~]# setfacl -R -m u:liyi:rwx /data/samba/編輯區/05-人員
三、在windows本機登錄Samba,驗證各用戶針對各個子目錄的讀寫權限
按鍵Ctrl+E,打開“此電腦”,然后點擊左側欄的“網絡”,接着在上側的網絡欄目里輸入samba地址( 比如samba部署機ip為192.168.10.10):\\192.168.10.10,然后輸入samba賬號和密碼,就可以登錄到Samba共享目錄了了。如上部署后,可以逐個驗證各個samba賬號對各個子目錄的讀寫權限。




溫馨提示:
1)如上配置,登錄Samba后,會顯示兩個目錄:一個是賬號目錄(沒有訪問權限,不需要訪問),一個就是samba共享目錄了(在smb.conf里定義的共享名稱,這個可以訪問),如下圖:

2)如何將samba共享目錄保存到本地電腦上,從而不需要每次都輸入賬號和密碼信息?
初次登錄samba,看到共享目錄后,可以右擊共享目錄,選擇"映射網絡驅動器",這樣就將samba共享目錄通過網絡驅動器的方式保存到本地了,如下圖:

===================================================================
溫馨提示:
以上的權限設置后,還是有一些問題的。就是說,新建文件的權限是沒有問題的,但是當一個文件新建后,另一個用戶就沒有權限去操作了。 比如,yusansan用戶在/data/samba/編輯區/05-實習生目錄下創建一個test文件,這時test文件的所有者是yusansan,那么fengwuwu等其他用戶 就不能對這個test文件進行寫操作了。 要想解決上面的問題,可以寫一個腳本,將上面賦予的權限動作循環執行(寫一個簡單的死循環腳本,去一直執行),如下: [root@samba-server ~]# cat /opt/samba_quanxian.sh #!/bin/bash while [ "1" = "1" ] do #設置存檔區權限 /usr/bin/chown -R liyi.liyi /data/samba/存檔區/01-重大事項 /usr/bin/chmod -R 700 /data/samba/存檔區/01-重大事項 /usr/bin/chown -R liuerer.liuerer /data/samba/存檔區/02-戰略投資 /usr/bin/chmod -R 700 /data/samba/存檔區/02-戰略投資 /usr/bin/chown -R yusansan.yusansan /data/samba/存檔區/03-戰略規划 /usr/bin/chmod -R 700 /data/samba/存檔區/03-戰略規划 /usr/bin/chown -R yusansan.yusansan /data/samba/存檔區/04-公司治理 /usr/bin/chmod -R 700 /data/samba/存檔區/04-公司治理 /usr/bin/chown -R yusansan.yusansan /data/samba/存檔區/05-股東信息 /usr/bin/chmod -R 700 /data/samba/存檔區/05-股東信息 /usr/bin/chown -R yusansan.yusansan /data/samba/存檔區/06-上層股東 /usr/bin/chmod -R 700 /data/samba/存檔區/06-上層股東 /usr/bin/chown -R fengwuwu.fengwuwu /data/samba/存檔區/07-投關管理 /usr/bin/chmod -R 700 /data/samba/存檔區/07-投關管理 /usr/bin/chown -R yusansan.yusansan /data/samba/存檔區/08-制度信批 /usr/bin/chmod -R 700 /data/samba/存檔區/08-制度信批 /usr/bin/chown -R yusansan.yusansan /data/samba/存檔區/09-部門綜合 /usr/bin/chmod -R 700 /data/samba/存檔區/09-部門綜合 /usr/bin/chown -R yusansan.yusansan /data/samba/存檔區/10-學習共享 /usr/bin/chmod -R 700 /data/samba/存檔區/10-學習共享 /usr/bin/setfacl -R -m u:fengwuwu:rx /data/samba/存檔區/02-戰略投資 /usr/bin/setfacl -R -m u:liuerer:rx /data/samba/存檔區/03-戰略規划 /usr/bin/setfacl -R -m u:chesisi:rwx /data/samba/存檔區/04-公司治理 /usr/bin/setfacl -R -m g:zhengshiyuangong:rx /data/samba/存檔區/04-公司治理 /usr/bin/setfacl -R -m u:chesisi:rwx /data/samba/存檔區/05-股東信息 /usr/bin/setfacl -R -m u:fengwuwu:rwx /data/samba/存檔區/06-上層股東 /usr/bin/setfacl -R -m u:chesisi:rx /data/samba/存檔區/07-投關管理 /usr/bin/setfacl -R -m g:zhengshiyuangong:rx /data/samba/存檔區/08-制度信批 /usr/bin/setfacl -R -m g:zhengshiyuangong:rwx /data/samba/存檔區/09-部門綜合 /usr/bin/setfacl -R -m g:zhengshiyuangong:rwx /data/samba/存檔區/10-學習共享 #設置編輯區權限 /usr/bin/chown -R yusansan.yusansan /data/samba/編輯區/01-開蓉開樂 /usr/bin/chmod -R 700 /data/samba/編輯區/01-開蓉開樂 /usr/bin/chown -R liuerer.liuerer /data/samba/編輯區/02-開蓉馮博 /usr/bin/chmod -R 700 /data/samba/編輯區/02-開蓉馮博 /usr/bin/chown -R yusansan.yusansan /data/samba/編輯區/03-開樂一鳴 /usr/bin/chmod -R 700 /data/samba/編輯區/03-開樂一鳴 /usr/bin/chown -R yusansan.yusansan /data/samba/編輯區/04-正式員工 /usr/bin/chmod -R 700 /data/samba/編輯區/04-正式員工 /usr/bin/chown -R yusansan.yusansan /data/samba/編輯區/05-人員 /usr/bin/chmod -R 700 /data/samba/編輯區/05-人員 /usr/bin/setfacl -R -m u:liuerer:rwx /data/samba/編輯區/01-材料 /usr/bin/setfacl -R -m u:fengwuwu:rwx /data/samba/編輯區/02-開蓉馮博 /usr/bin/setfacl -R -m u:chesisi:rwx /data/samba/編輯區/03-開樂一鳴 /usr/bin/setfacl -R -m g:zhengshiyuangong:rwx /data/samba/編輯區/04-正式員工 /usr/bin/setfacl -R -m g:zhengshiyuangong:rwx /data/samba/編輯區/05-實習生 /usr/bin/setfacl -R -m g:shixisheng:rwx /data/samba/編輯區/05-實習生 #設置李一總權限 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/01-重大事項 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/02-戰略投資 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/03-戰略規划 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/04-公司治理 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/05-股東信息 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/06-上層股東 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/07-投關管理 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/08-制度信批 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/09-部門綜合 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/存檔區/10-學習共享 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/編輯區/01-材料 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/編輯區/02-周報 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/編輯區/03-總結 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/編輯區/04-記錄 /usr/bin/setfacl -R -m u:liyi:rwx /data/samba/編輯區/05-人員 done 然后執行腳本: [root@samba-server ~]# nohup sh /opt/samba_quanxian.sh & //按鍵ctrl+c [root@samba-server ~]# ps -ef|grep samba_quanxian.sh root 83870 82538 61 23:32 pts/0 00:05:17 sh samba_quanxian.sh root 164997 82538 0 23:41 pts/0 00:00:00 grep --color=auto samba_quanxian.sh 這樣,在各個區域的目錄下創建的文件,就會及時被賦予相應權限。 =========================================================================== 然而后續又發現了一個問題: 上面循環腳本實時執行后,發現用acl授權的用戶訪問samba目錄時,有時有權訪問,有時無權訪問(提示沒有權限,句柄無效)。 最后將腳本中的循環語句(while,do,done)刪除,取消實時執行,在crontab里設置每10秒鍾執行一次,調整后問題解決。 [root@samba-server ~]# crontab -l * * * * * /bin/bash -x /opt/samba_quanxian.sh > /dev/null 2>&1 * * * * * sleep 10;/bin/bash -x /opt/samba_quanxian.sh > /dev/null 2>&1 * * * * * sleep 20;/bin/bash -x /opt/samba_quanxian.sh > /dev/null 2>&1 * * * * * sleep 30;/bin/bash -x /opt/samba_quanxian.sh > /dev/null 2>&1 * * * * * sleep 40;/bin/bash -x /opt/samba_quanxian.sh > /dev/null 2>&1 * * * * * sleep 50;/bin/bash -x /opt/samba_quanxian.sh > /dev/null 2>&1
