一、需求

  • 局域网内有若干用户,所有用户访问一个共享目录
  • 每个用户在共享目录里有自己的文件夹
  • 每个用户都可以读取其他人的文件夹
  • 每个用户只能对自己的文件夹有写入权限
  • 所有用户都属于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 = . 192.168.. 192.168.. 172.16..  # 允许接入的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

最新文章

  1. 最新Mac OS X 10.12.1 安装cocoapods及使用详解
  2. vtkMapper
  3. SQL查询关于相对路径、矢代、绝对路径、递归、计算列的速度对比跟优化-SOD群记录
  4. linux删除文件后沒有释放空间
  5. sql创建表格 转载
  6. [转载]easyui datagrid 时间格化(JS 日期时间本地化显示)
  7. poj 2186 (强连通缩点)
  8. Vmware ESX 5.0 安装与部署
  9. android Fragment 用法小结
  10. 读书笔记 effective c++ Item 6 如果你不想使用编译器自动生成的函数,你需要明确拒绝
  11. [bzoj1774] [Usaco2009 Dec]Toll 过路费
  12. asp.net -mvc框架复习(11)-基于三层架构与MVC实现完整的用户登录
  13. Linuxc - 通过管道,让小程序更有活力
  14. while循环与 for循环,函数定义与调用
  15. python 接口自动化测试(四)cookie&session
  16. 三层实现办公用品表CRUD(全过程)-ASP
  17. CF 317 A. Lengthening Sticks(容斥+组合数学)
  18. CSS3标签显示模式
  19. POJ 2083 Fractal 分形题目
  20. Spring jdbcTemplat 写入BLOB数据为空

热门文章

  1. javascript jquery console调试方法说明
  2. shim和polyfill,前端术语
  3. 008 Android activity实现多个界面的相互跳转(主要利用Intent)
  4. LeetCode153.寻找旋转排序数组中的最小值
  5. wepsocket 了解一下
  6. [转] 从零开始学Spring Boot
  7. pycharm中使用正则表达式批量添加print括号,完美从python2迁移到python3
  8. 磁盘挂载 fdisk
  9. Knime读取Jason数据
  10. ali代码风格