下面根據實際工做中遇到的一個共享目錄的多用戶權限需求案例來講明下Samba用戶權限的設置。linux
1、需求場景
領導:李一(liyi)
正式員工(zhengshiyuangong):劉二二(liuerer)、於三三(yusansan)、車四四(chesisi)、馮五五(fengwuwu)
實習生(shixisheng):步六六(buliuliu)、王七七(wangqiqi)vim
李一有整個共享盤的讀寫權限,其中共享盤總體分爲存檔區和編輯區,子目錄按照以下順序進行展現。
具體權限設置以下:windows
2、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-人員
3、在windows本機登陸Samba,驗證各用戶針對各個子目錄的讀寫權限bash
按鍵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共享目錄經過網絡驅動器的方式保存到本地了,以下圖:ssh
===================================================================
舒適提示:學習
以上的權限設置後,仍是有一些問題的。就是說,新建文件的權限是沒有問題的,可是當一個文件新建後,另外一個用戶就沒有權限去操做了。 好比,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