一、需求
- 局域網內有若干用戶,所有用戶訪問一個共享目錄
- 每個用戶在共享目錄里有自己的文件夾
- 每個用戶都可以讀取其他人的文件夾
- 每個用戶只能對自己的文件夾有寫入權限
- 所有用戶都屬於filesgroup組
二、環境
服務器:Centos6
主機名:fileserver
IP地址:192.168.1.2
用戶端IP網段: 192.168.1.0/24 , 192.168.2.0/24 , 172.16.1.0/24
三、配置步驟
1、安裝samba,備份原始配置文件,編輯配置文件
[root@fileserver ~]# yum install samba samba-common samba-client [root@fileserver ~]# cp /etc/samba/smb.conf /root [root@fileserver ~]# vi /etc/samba/smb.conf
以下顯示的是編輯過的內容 #======================= Global Settings ===================================== [global] # ----------------------- Network Related Options ------------------------- workgroup = FILEGROUP netbios name = FILESERVER interfaces = lo eth0 192.168.1.2/24 # samba服務監聽的網卡和IP地址 hosts allow = 127. 192.168.1. 192.168.2. 172.16.1. # 允許接入的IP網段: 服務器本機 , 192.168.1.x , 192.168.2.x , 172.16.1.x # --------------------------- Logging Options ----------------------------- log file = /var/log/samba/log.%m #默認不變 max log size = 50 #默認不變 # ----------------------- Standalone Server Options ------------------------ security = user # 單機用戶認證 passdb backend = smbpasswd # 使用/var/lib/samba/private/smbpasswd 文本文件保存用戶和密碼 #============================ Share Definitions ============================== # 這里注釋掉了很多默認配置文件的設置,最后添加了自定義設置 #[homes] ; comment = Home Directories ; browseable = no ; writable = yes ; valid users = %S ; valid users = MYDOMAIN\%S #[printers] ; comment = All Printers ; path = /var/spool/samba ; browseable = no ; guest ok = no ; writable = no ; printable = yes # Un-comment the following and create the netlogon directory for Domain Logons ; [netlogon] ; comment = Network Logon Service ; path = /var/lib/samba/netlogon ; guest ok = yes ; writable = no ; share modes = no # Un-comment the following to provide a specific roving profile share # the default is to use the user's home directory ; [Profiles] ; path = /var/lib/samba/profiles ; browseable = no ; guest ok = yes # A publicly accessible directory, but read only, except for people in # the "staff" group ; [public] ; comment = Public Stuff ; path = /home/samba ; public = yes ; writable = yes ; printable = no ; write list = +staff # 自定義文件共享設置 [files] comment = files path = /opt/files public = no writable = yes printable = no write list = @filesgroup
2、添加組,創建共享文件夾,設置SELINUX
[root@fileserver ~]# groupadd filesgroup [root@fileserver ~]# mkdir /opt/files [root@fileserver ~]# chcon -t samba_share_t /opt/files
3、將samba設為開機自啟動,啟動samba
[root@fileserver ~]# chkconfig smb on
[root@fileserver ~]# service smb start
4、批量添加用戶
useradd -MN %u -s /sbin/nologin && echo %p | passwd --stdin %u && usermod -aG filesgroup %u && mkdir -p /opt/files/%d && chown -R %u: /opt/files/%d && echo -e "%p\n%p" | smbpasswd -a -s %u
指令解釋:
- 此行指令由多條指令組成,指令之間用 && 連接,即上一條指令執行成功后才執行下一條
- %u 代表用戶名,%p 代表密碼,%d 代表用戶文件夾
- 用戶數量較少時可以在文本編輯器里編輯批量指令,每粘貼一行,用查找替換功能依次替換%u為用戶名、%p為密碼、%d為用戶目錄
- 用戶數量很多時建議將用戶名、密碼、文件夾名寫入文件,用腳本讀取文件自動執行
逐條解釋:
useradd -MN %u -s /sbin/nologin 添加用戶,-MN表示不創建用戶家目錄和用戶基本組,-s /sbin/nologin 表示用戶不可登錄服務器 echo %p | passwd --stdin %u 為用戶設置密碼(其實可以省略) usermod -aG filesgroup %u 將用戶加入到filegroup組 mkdir -p /opt/files/%d 在共享目錄創建用戶文件夾 chown -R %u: /opt/files/%d 將用戶文件夾owner設為用戶自己 echo -e "%p\n%p" | smbpasswd -a -s %u 添加samba用戶帳號,smbpasswd -a -s %u 是靜默方式添加samba帳號,echo -e "%p\n%p" 是兩次輸出密碼 %p,中間輸出回車符 \n
5、設置共享目錄所屬組,為共享目錄設置GUID,使所有用戶創建的文件都屬於filesgroup組,禁止filesgroup組以外的用戶讀取、執行
[root@fileserver ~]# chown -R :filesgroup /opt/fils
[root@fileserver ~]# chmod -R g+s /opt/files
[root@fileserver ~]# chmod -R o-rx /opt/files/
[root@fileserver ~]#
6、看一下生成的目錄
[root@fileserver ~]# ls -ldZ /opt/files
drwxr-s---. root filesgroup unconfined_u:object_r:samba_share_t:s0 /opt/files
[root@fileserver ~]#
[root@fileserver ~]# ls -lZ /opt/files
drwxr-s---. user1 filesgroup unconfined_u:object_r:samba_share_t:s0 dir1
drwxr-s---. user2 filesgroup unconfined_u:object_r:samba_share_t:s0 dir2
drwxr-s---. user3 filesgroup unconfined_u:object_r:samba_share_t:s0 dir3
[root@fileserver ~]#
[root@fileserver ~]# ll /var/lib/samba/private/smbpasswd
-rw-------. 1 root root 1435 2015-04-13 16:59 /var/lib/samba/private/smbpasswd
[root@fileserver ~]#
致謝: http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html