Centos6配置samba服务器并批量添加用户和文件夹

一、需求

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



posted @ 2015-04-13 18:08  Panblack  阅读(8123)  评论(0编辑  收藏  举报