读书笔记 鸟哥的私房菜
Samba服务器
使用的什么协议:smb协议
安装完samba服务器:
smbd :管理文件共享、打印、打印机等信息
nmbd :用来管理群组、netBIOS
侦听的端口:UDP(137、138)、TCP(139、445)
1、需要什么软件包:
# rpm -qa | grep samba
samba-swat-3.0.33-3.14.el5 #这个是通过网页来管理samba服务器的包
samba-client-3.0.33-3.14.el5 #samba客户端的软件包
samba-3.0.33-3.14.el5 #samba服务器软件包
samba-common-3.0.33-3.14.el5 #samba服务器的公共包
2、samba服务器的联机模式:Peer/Peer Domain model
Peer/Peer:工作组模式
Domain model:域模式
PS:一直以来,经过微软的熏陶,我以为PDC如果离开了AD将无法生存,为什么 呢?因为PDC只是AD中的一个主机角色,但是samba给了我不一样解释。
工作组模式在一些工作组里,用的相对较多一些;而对于域模式的话,内容稍多一些;而且通常会存在一些问题。
以案例来说话,用理论来结尾。
服务器:RHEL 5.4
测试方式:使用命令行测试。(linux下写博弄图很麻烦,虽然很好看...:()
1、为myhat公司建立一个samba服务器,为公司的五个部门(HR、IT、Sales、Manager、Support)建立各自部门的目录,允许各个组里的用户可以写自个的目录。并且每个用户的写入最大大小100MB,写到90M就开始报警。如果用户写入超过了95M,就开始发邮件给用户。
HR: jack ,tom
IT: cheng ,Bnsen
Sales: eric ,jasion
Manager: hu ,jiang
Support: jim
2、为myhat公司建立一个公共的文件夹public,每个用户都可以写入。但是,每个用户的写入的内容,只有管理员与他们自己才可以删除,其他用户不能删除。并且这个目录的最大空间200M;写到180M就开始报警。
3、为myhat公司建立一个临时文件夹temp,每个用户都可以写入;这个临时文件夹的内容只保存一天,系统会在当天晚上00:00,就自动移动到另外一个文件夹;并且这个文件夹的名称以当天时间来命名。
分析:
A、实现需要:独立分区挂载、Quota、邮件监控
B、实现需要:公共目录、Quota、粘贴位
C、实现需要:公共目录、计划任务
实现A:
检查一下包的安装情况
# rpm -qa | grep samba
samba-3.0.33-3.14.el5
samba-client-3.0.33-3.14.el5
samba-common-3.0.33-3.14.el5
# mkdir /share
# cat /etc/fstab
输出的其他内容略过......
/dev/sdb1 /share ext3 defaults,usrquota,grpquota 0 0
# mount -a
# mount
输出其他内容略过.......
/dev/sdb1 on /share type ext3 (rw,usrquota,grpquota)
添加用户目录
# mkdir {hr,it,sales,manager,support}
# ls
hr it lost+found manager sales support
添加用户组
# groupadd hr
# groupadd it
# groupadd manager
# groupadd sales
# groupadd support
添加相应的用 户到相应的组
-------------用户的清单-------
HR: jack ,tom
IT: cheng ,Bnsen
Sales: eric ,jasion
Manager: hu ,jiang
Support: jim
-------实际组名全部为小写---------
# useradd -s /sbin/nologin -g hr jack
# useradd -s /sbin/nologin -g hr tom
# useradd -s /sbin/nologin -g it cheng
# useradd -s /sbin/nologin -g it bnsen
# useradd -s /sbin/nologin -g sales eric
# useradd -s /sbin/nologin -g sales jasion
# useradd -s /sbin/nologin -g manager hu
# useradd -s /sbin/nologin -g manager jiang
# useradd -s /sbin/nologin -g support jim
步步为营,检查一两个:
# id jim
uid=508(jim) gid=504(support) groups=504(support) context=root:system_r:unconfined_t:SystemLow-SystemHigh
# id hu
uid=506(hu) gid=502(manager) groups=502(manager) context=root:system_r:unconfined_t:SystemLow-SystemHigh
生成quota的用户配置档及组配置档
# quotacheck -cvug /share/
quotacheck: Scanning /dev/sdb1 [/share] quotacheck: Cannot stat old user quota file: 没有那个文件或目录
quotacheck: Cannot stat old group quota file: 没有那个文件或目录
quotacheck: Cannot stat old user quota file: 没有那个文件或目录
quotacheck: Cannot stat old group quota file: 没有那个文件或目录
done
quotacheck: Checked 8 directories and 2 files
quotacheck: Old file not found.
quotacheck: Old file not found.
# ls
aquota.group aquota.user hr it lost+found manager sales support
针对需求,编辑用户的aquota.user文件,具体如下:
# edquota jack
Disk quotas for user jack (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/sdb1 0 90000 100000 0 0 0
# edquota -p jack tom cheng bnsen eric jasion hu jiang jim
# quotaon /share
配置目录的权限:
# chmod 777 /share/{it,manager,support,hr,sales}
配置samba服务器
我们测试一下设置,OK,文件设置没有问题
# testparm
Load smb config files from /etc/samba/smb.conf
Processing section "[HR]"
Processing section "[IT]"
Processing section "[SALES]"
Processing section "[MANAGER]"
Processing section "[SUPPORT]"
Processing section "[homes]"
Processing section "[printers]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
[global]
workgroup = MYGROUP
netbios name = MYSERVER
server string = Samba Server Version %v
passdb backend = tdbsam
cups options = raw
[HR]
comment = HR Direcory
path = /share/hr
valid users = @hr , jack, tom
read only = No
create mask = 0700
directory mask = 0700
browseable = No
[IT]
comment = IT
path = /share/it
valid users = @it, cheng, bnsen
read only = No
create mask = 0700
directory mask = 0700
[SALES]
comment = SALES
path = /share/sales
valid users = @sales , eric, jasion
read only = No
create mask = 0750
directory mask = 0750
[MANAGER]
comment = MANAGER
path = /share/manager
valid users = @manager , hu, jiang
read only = No
create mask = 0700
directory mask = 0700
browseable = No
[SUPPORT]
comment = Support
path = /share/support
valid users = @support , jim
read only = No
create mask = 0700
directory mask = 0700
[homes]
comment = Home Directories
read only = No
browseable = No
[printers]
comment = All Printers
path = /var/spool/samba
printable = Yes
browseable = No
在/etc/samba/smb.conf中确保有以下几行
security = user
encrypt passwords=yes
smb passwd file =/etc/samba/smbpasswd
# service smb restart
关闭 SMB 服务: [失败]
关闭 NMB 服务: [失败]
启动 SMB 服务: [确定]
启动 NMB 服务: [确定]
因为samba的用户是的添加首先要确保/etc/passwd文件中存在文件名,否则将不能添加。
# smbpasswd -a jack
-----------------------
smbpasswd命令用法:
smbpasswd -a user 添加一个账户
smbpasswd -d user 禁用这个账户
smbpasswd -e user 启用这个账户
smbpasswd -x user 删除这个账户
-----------------------
# smbclient -L //192.168.10.203 -U tom
Password:
Domain=[MYSERVER] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
Sharename Type Comment
--------- ---- -------
IT Disk IT
SALES Disk SALES
SUPPORT Disk Support
IPC$ IPC IPC Service (Samba Server Version 3.0.33-3.14.el5)
tom Disk Home Directories
省出了输出的其他信息........
使用smbclient -L IP地址 可以看到的目录有SUPPORT,SALES、IT目录 HR跟MANAGER都不可用
---------------------------------------------
监控用户的使用量,我写一个小的脚本:
当用户的配置使用超过了95M的时候,就开始给他发邮件,一分钟发一次。
#!/bin/bash
# this is used for monitor quota
# mode by cheng
# ctime 2010-11-16 16:35
user=`/usr/sbin/repquota -a | grep -v Block | grep -v device | grep -v root | \
awk -F " " '{print $1}' | grep -v User | grep -v -`
for i in `echo $user`
do
u=`/usr/sbin/repquota -a | grep $i | awk -F " " '{print $3}'`
if [ $u -gt 95000 ];then
echo "You have reached the available space $u KB" | \
mail -s "`date +%Y/%m/%d-%I:%M` Share Quota waring" $i
echo "$i have reached the available space $u KB" | \
mail -s "`date +%Y/%m/%d-%I:%M` badboy $i quota waring" root
fi
done
设定计划任务
# crontab -l #每一分钟执行一次,如果用户再不进行操作,邮箱一定都爆满了。
*/1 * * * * /quota.sh
PS:实际应用中,请将时间修改成1小时或是24小时。这样的话,相对较合量,用户也不会有大量的邮件要处理。
# su - jasion
[jasion@samba hr]$ dd if=/dev/zero of=/share/hr/96M bs=1M count=96
sdb1: warning, user block quota exceeded.
96+0 records in
96+0 records out
100663296 bytes (101 MB) copied, 0.52603 seconds, 191 MB/s
[jasion@samba hr]$ ls
96M
测试邮件的收发
[root@samba ~]# mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1
[email protected] Wed Nov 17 00:48 16/730 "2010/11/17-12:48 badboy jasion quota waring"
& q
Held 1 message in /var/spool/mail/root
[root@samba ~]# cat /var/mail/jasion
From
[email protected] Wed Nov 17 00:48:01 2010
Return-Path: <
[email protected]>
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.8/8.13.8) with ESMTP id oAGGm13o019920
for <
[email protected]>; Wed, 17 Nov 2010 00:48:01 +0800
Received: (from root@localhost)
by localhost.localdomain (8.13.8/8.13.8/Submit) id oAGGm1a0019915
for jasion; Wed, 17 Nov 2010 00:48:01 +0800
Date: Wed, 17 Nov 2010 00:48:01 +0800
From: root <
[email protected]>
Message-Id: <
[email protected]>
To:
[email protected]
Subject: 2010/11/17-12:48 Share Quota waring
Status: O
You have reached the available space 98408 KB
测试:
[root@samba ~]# smbclient //192.168.10.203/hr -U tom
Password:
Domain=[MYSERVER] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
smb: \> put install.log.syslog
putting file install.log.syslog as \install.log.syslog (54.9 kb/s) (average 54.9 kb/s)
smb: \> ls
. D 0 Wed Nov 17 02:04:28 2010
.. D 0 Wed Nov 17 02:01:16 2010
install.log.syslog A 3995 Wed Nov 17 02:04:29 2010
45000 blocks of size 2048. 44916 blocks available
[root@samba ~]# smbclient //192.168.10.203/it -U tom
Password:
Domain=[MYSERVER] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
tree connect failed: NT_STATUS_ACCESS_DENIED
测试就到这里了,有空大家可以自己测试。
--------A完成之时就是B开始之时------------------
B、只为linux上的quota是针对分区而言的,所以需要为public这个目录独立挂载一个分区
# cat /etc/fstab
省也其他无关输出.......
/dev/sdb1 /share ext3 defaults,usrquota,grpquota 0 0
/dev/sdc2 /public ext3 defaultls,usrquota,grpquota 0 0
# mount -a
# mount
/dev/sdb1 on /share type ext3 (rw,usrquota,grpquota)
/dev/sdc2 on /public type ext3 (rw,usrquota,grpquota)
编辑配置文件,并加上以下内容
# vi /etc/samba/smb.conf
[public]
comment = public
browseable = yes
writeable = yes
valid users = @support, @hr, @it, @manager, @sales
guest ok=no
public = yes
path = /public
# chmod 1777 /public/
# service smb restart
关闭 SMB 服务: [确定]
关闭 NMB 服务: [确定]
启动 SMB 服务: [确定]
启动 NMB 服务: [确定]
测试:
# smbclient //192.168.18.34/public -U jack
Password:
Domain=[MYSERVER] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
smb: \> ls
. D 0 Wed Nov 17 02:17:04 2010
.. D 0 Wed Nov 17 04:20:01 2010
lost+found D 0 Wed Nov 17 02:17:04 2010
64486 blocks of size 131072. 60041 blocks available
smb: \> put install.log.syslog #用jack的身份来传一个文件
putting file install.log.syslog as \install.log.syslog (108.4 kb/s) (average 108.4 kb/s)
尝试用jim这个用户来删除:
[root@samba ~]# smbclient //192.168.18.34/public -U jim
Password:
Domain=[MYSERVER] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]
smb: \> ls
. D 0 Wed Nov 17 04:49:01 2010
.. D 0 Wed Nov 17 04:20:01 2010
install.log.syslog A 3995 Wed Nov 17 04:49:01 2010
lost+found D 0 Wed Nov 17 02:17:04 2010
64486 blocks of size 131072. 60041 blocks available
smb: \> rm install.log.syslog
NT_STATUS_ACCESS_DENIED deleting remote file \install.log.syslog
各位观众,jim没有权限删除jack的文件。
OK,关于这个粘贴位的部分搞定了,接下来要搞定的是什么?当然是quota,这个目录只允许写200M,写了180M还要报警,这里最好还是不要发邮件,要不然所有人都会以你为敌! 为什么你要设置这么少的空间?
在这里,似乎出现了一个问题? quota不是针对用户与组来限制吗?怎么说这个目录只能写200M?
这其实也是通过quota来实现的,但我们需要为quota建立一个公共组。
# groupadd public
# tail -1 /etc/group && tail -1 /etc/gshadow
public:x:505:jack,tom,cheng,bnsen,eric,jasion,hu,jiang,jim
public:!::jack,tom,cheng,bnsen,eric,jasion,hu,jiang,jim
# edquota -g public
Disk quotas for group public (gid 505):
Filesystem blocks soft hard inodes soft hard
/dev/sdc2 0 180000 200000 0 0 0
# quotaon /public/
至于测试结果,大伙有空可以测试下。
------B需求完成的时候就是C需求开始的时候--------------
C.思路:建立temp目录,并且设定计划任务,每天晚上23:00开始按照当前日期建立目录名,然后移动temp里的目录方件到新建立的目录里。这个用脚本可以轻松的实现!
关于共享的部分,跟之前的例子一样,在这里就不写了。详见以上内容。
脚本实现的结果是:如果/share/temp里没有文件,那么它会发一封邮件给管理员“今天没事干",如果有文件或是目录的话,它会将其所有内容转走,并且将转走的清单发一封邮件给管理员。
# chmod u+x /auto_mv_share_temp.sh
# cat /auto_mv_share_temp.sh
---脚本开头---------------------
#!/bin/bash
#this script is used for mv temp/* to new temp
#mode by cheng
#ctime 2010-11-16 21:45
a=`date +%Y%m%d`
test -d /share/$a || mkdir /share/"$a"
ls /share/temp/ > /tmp/$a
if [ -s /tmp/$a ];then
mv /share/temp/* /share/"$a" && echo -n `cat /tmp/$a` \
| mail -s "`date +%Y/%m/%d-%I:%M`_automv_share/temp/" root
else
echo "Today has no nothing to do" | mail -s "`date +%Y/%m/%d-%I:%M`_Today nothing to do" root
fi
rm -rf /tmp/$a
--脚本结尾----------------------
测试一下脚本吧。
# touch google baidu sina cisoc redhat sohu bing
# mkdir cheng myhat 51cto
# /auto_mv_share_temp.sh
# mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1
[email protected] Wed Nov 17 07:08 16/732 "2010/11/17-07:08_automv_share/temp/"
& 1
Message 1:
From
[email protected] Wed Nov 17 07:08:39 2010
Date: Wed, 17 Nov 2010 07:08:39 +0800
From: root <
[email protected]>
To:
[email protected]
Subject: 2010/11/17-07:08_automv_share/temp/
51cto baidu bing cheng cisoc google myhat redhat sina sohu
-----------OVER----------
如果你觉得这个排版不好,可以看附件。
本文出自 “潜入技术的海洋” 博客,请务必保留此出处http://myhat.blog.51cto.com/391263/425121