Linux 文件共享服务 第12天

2012-09-11 22:18:03 0  category: linux

Samba 简介
在 UNIX 系统中,Samba 是通过服务器消息块协议(SMB)在网络上的计算机之间共享文件和
打印服务的软件包。
SMB(Server Message Block)协议是一种客户端服务器协议,位于应用层,是 Windows 共
享文件和打印服务的标准化协议。可以在两台计算机之间共享文件、打印机。
Samba 是一个开放源代码的软件,
任何拥有 GNU 组织通用公共许可证 GPL 的用户都可以使用,
并免费获得它的源代码和可执行文件。
Samba 的主页:http://www.samba.org
启动服务:/etc/rc.d/init.d/smb start
Samba 有两个守护进程:smbd 和 nmbd
——smbd 监听 139 TCP 端口
——nmbd 监听 137 和 138 UDP 端口
smbd 进程的作用是处理 SMB 请求包,负责用户验证和文件共享;nmbd 进程的作用是处理浏
览共享和计算机名称解析。
Samba 配置
Samba 配置文件应存放在:
/etc/samba/smb.conf
包括四个设置段:
[globa1]设置全局环境选项
[homes]设置用户宿主目录共享
[printers]设置打印机共享
[sharefiles]设置文件共享
注:行前有分号“:”或英镑符“#”表示注释
[globa1]段主要选项设置:
workgroup=指定工作组或域
server string=描述
security=指定安全模式
hosts allow=限定主机访问
log file=指定日志文件存放位置
max log size=指定日志文件大小
[homes]段主要选项设置:
comment=Home Directories
browseable=no
writable=yes
[root@helen ~]# rpm -q samba
samba-3.0.33-3.28.el5
[root@helen ~]# rpm -qa | grep samba
samba-common-3.0.33-3.28.el5
samba-3.0.33-3.28.el5
samba-client-3.0.33-3.28.el5
system-config-samba-1.2.41-5.el5
[root@helen ~]# /etc/rc.d/init.d/smb start
启动 SMB 服务:[确定]
启动 NMB 服务:[确定]
[root@helen ~]# rpm -qa | grep samba
samba-common-3.0.33-3.28.el5
samba-3.0.33-3.28.el5
samba-client-3.0.33-3.28.el5
system-config-samba-1.2.41-5.el5
[root@helen ~]# rpm -ql samba-common | grep smb.conf
/etc/samba/smb.conf
/usr/share/man/man5/smb.conf.5.gz
[root@helen ~]# cp /etc/samba/smb.conf /backup
[root@helen ~]# vi /etc/samba/smb.conf
# 注释的是描述
; 注释的是设置行
[global]
workgroup = MYGROUP 指定工作组或域 不知道工作组或域也可不写
server string = Samba Server Version %v 描述
security = user 指定安全模式
1.share 无权限验证
2.user 为缺省选项,推荐,由 Linux Samba 服务器做验证
3.server 第三方主机验证
4.domain 第三方主机验证(第三方必须是 Windows 域控制器)
;
hosts allow = 127. 192.168.12. 192.168.13. 限定主机访问
1、哪些主机访问 hosts allow、hosts deny 两者同时存在时,允许
优先(大部分情况禁止优先)
2、哪些用户访问
;
log file = /var/log/samba/%m.log 日志的存放位置 %m 标识进程
;
max log size = 50 指定日志文件大小,单位是 KB
[homes]
comment = Home Directories 描述
browseable = no 无权限访问目录是不可见的
writable = yes 只读 yes 可写
;
valid users = %S 有效用户,%S 匹配用户 无关紧要
Samba 应用示例一:
允许用户通过 Windows 客户端访问自己的宿主目录。
1、安装 Samba,不需对配置文件做修改,即可实现此功能

如果启动了防火墙需要关闭防火墙: iptables -F
[root@helen ~]# iptables -L
Chain INPUT (policy ACCEPT)
Chain FORWARD (policy ACCEPT)
Chain OUTPUT (policy ACCEPT)
[root@helen ~]# iptables -F
[root@helen ~]# iptables -L
如果安装启用了 SELinux,需要先执行:setsebool -P samba_enable_home_dirs on 或者
执行# vi /etc/selinux/config 将 SELINUX=enforcing 改为 SELINUX=disabled
[root@helen ~]# getsebool -a | grep samba (列出 Samba 策略)
:
:
samba_enable_home_dirs –> off
:
:
[root@helen ~]# setsebool -P samba_enable_home_dirs on
[root@helen ~]# getsebool -a | grep samba
:
:
samba_enable_home_dirs –> on
:
:
2、设置用户 Samba 验证密码
smbpasswd -a 用户名
[root@helen ~]# smbpasswd -a samlee
New SMB password:
Retype new SMB password:
Added user samlee.
3、启动 Samba 服务
/etc/rc.d/init.d/smb start
Windows 客户端访问 Samba 服务器共享资源:
“开始”-“运行”
,输入\\192.168.9.90 服务器地址
[root@helen ~]# ps -le | grep smbd
5 S
0 2942
1 0 78
0 – 3963 -
?
00:00:00 smbd
1 S
0 2947 2942 0 79
0 – 3963 pause ?
00:00:00 smbd
5 S
0 3051 2942 0 78
0 – 4053 -
?
00:00:00 smbd
Samba 服务器端查看访问的客户端信息:
#smbstatus
[root@helen ~]# smbstatus
Samba version 3.0.33-3.28.el5
PID
Username
Group
Machine
——————————————————————-
3088 samlee
samlee
pc-201101180945 (192.168.9.254)
Service
pid
machine
Connected at
——————————————————-
samlee
3088 pc-201101180945 Sun Feb 27 16:47:05 2011
Locked files:
Pid Uid DenyMode Access R/W
Oplock SharePath
Name
Time
——————————————————————————-
3088 500 DENY_NONE 0×100001 RDONLY NONE /home/samlee . Sun Feb 27 16:51:39 2011
3088 500 DENY_NONE 0×100001 RDONLY NONE /home/samlee . Sun Feb 27 16:52:46 2011
[root@helen ~]# ls /var/log/samba/
192.168.9.254.log cores nmbd.log pc-201101180945.log smbd.log
[root@helen ~]# cat /var/log/samba/smbd.log
[2011/02/27 16:02:47, 0] smbd/server.c:main(944)
smbd version 3.0.33-3.28.el5 started.
Copyright Andrew Tridgell and the Samba Team 1992-2008
[2011/02/27 16:02:48, 1] passdb/pdb_tdb.c:tdbsam_open(857)
tdbsam_open: Converting version 0 database to version 3.
[2011/02/27 16:02:48, 1] lib/account_pol.c:account_policy_get(286)
account_policy_get: tdb_fetch_uint32 failed for field 1 (min password length),
returning 0
[2011/02/27 16:02:48, 1] lib/account_pol.c:account_policy_get(286)
Samba 应用示例二:
设置 Samba 共享目录/software,允许用户 jack 与 mary 可以通过 Windows 客户端访问,并
具有读写权限。
1、创建目录/software,添加用户 jack 与 mary
2、如果安装了 SELinux,执行命令启动用户可访问系统目录:
chcon -t samba_share_t 共享目录
3、在 Samba 配置文件/etc/samba/smb.conf 末尾添加:
[software]
path=/software
valid users=jack mary
writable=yes
4、设置用户 jack、mary 的 Samba 验证密码,启动 Samba 服务(服务若已启动,不需重启)

小技巧:
Windows 在关掉所有共享文件和目录时在 DOS 命令方式下输入命令:
net use * /delete /y
C:\Documents and Settings\Administrator>net use
会记录新的网络连接。
状态
本地
远程
网络
——————————————————————————-
OK
\\192.168.9.90\IPC$
Microsoft Windows Network
命令成功完成。
C:\Documents and Settings\Administrator>net use * /delete /y
您有以下的远程连接:
\\192.168.9.90\IPC$(空连接)
继续运行会取消连接。
命令成功完成。
或者
C:\>net use \\192.168.9.90\IPC$ /delete /y (推荐使用此种方法)
在与 \\192.168.9.90\IPC$ 的连接中,有打开的文件和/或没有结束的目录搜索。

\\192.168.9.90\IPC$ 已经删除。
就可以不必注销或重启而以另一个用户的身份登陆 samba 服务器。
应射网络驱动器
问题:用户 jack 与 mary 是否可以对/software 进行写操作?
#groupadd softadm
#usermod -G softadm jack
#usermod -G softadm mary
#chgrp softadm /software
#chmod g+w /software
#ls -ld /software
drwxrwxr-x 2 root softadm 512 Jul 14 06:17/software
#grep softadm /etc/group
softadm::100:jack,mary
[root@helen aa]# mkdir /software
[root@helen aa]# useradd jack
[root@helen aa]# passwd jack
Changing password for user jack.
New UNIX password:
BAD PASSWORD: it is too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@helen aa]# useradd mary
[root@helen aa]# passwd mary
Changing password for user mary.
New UNIX password:
BAD PASSWORD: it is too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@helen aa]# tail -2 /etc/passwd
jack:x:507:507::/home/jack:/bin/bash
mary:x:507:507::/home/mary:/bin/bash
[root@helen aa]# groupadd softadmin
[root@helen aa]# gpasswd -a jack softadmin
正在将用户“jack”加入到“softadmin”组中
[root@helen aa]# usermod -G softadmin mary
[root@helen aa]# cat /etc/group | grep softadmin
softadmin:x:892:jack,mary
[root@helen aa]# ls -ld /software
drwxr-xr-x 2 root root 4096 02-27 17:08 /software
[root@helen aa]# chgrp softadmin /software
[root@helen aa]# chmod g+w /software
[root@helen aa]# ls -ld /software
drwxrwxr-x 2 root softadmin 4096 02-27 17:08 /software

[root@helen aa]# chcon -t samba_share_t /software
[root@helen aa]# vi /etc/samba/smb.conf (在文件末尾添加下面的内容)
290
[software]
291
path = /software
292
valid users = jack mary
“/etc/samba/smb.conf” 293L, 9803C written
[root@helen aa]# smbpasswd -a jack
New SMB password:
Retype new SMB password:
Added user jack.
[root@helen aa]# smbpasswd -a mary
New SMB password:
Retype new SMB password:
Added user mary.
[root@helen aa]# /etc/rc.d/init.d/smb restart
关闭 SMB 服务:[确定]
关闭 NMB 服务:[确定]
启动 SMB 服务:[确定]
启动 NMB 服务:[确定]
Samba 服务器检测命令
测试语法错误:
#testparm
[root@helen aa]# vi /etc/samba/smb.conf
292
valid user = jack mary (将 users 写成了 user)
“/etc/samba/smb.conf” 293L, 9802C written
[root@helen aa]# testparm
:
:
Unknown parameter encountered: “valid user”
Ignoring unknown parameter “valid user”
:
:
[software]
path = /software
read only = No
当配置文件中的 valid user 为 valid users 时:
[root@helen aa]# testparm
290
[software]
291
path = /software
292
valid user = jack mary
293
writable = yes
Samba 应用案例三:建立一个公共的只读目录/public,所有人可以浏览目录的内容。
[public]
comment=Read Only Public
path=/public

writable=no
Samba 应用案例四:建立部门资料共享目录/hr,部门每个人都能读写,但不能删除别人的
文件。
[hr]
path=/hr
valid users=zhangsan lisi wangwu
writable=yes
#chmod 1777/hr
FTP 服务器配置
Wu-FTP:古老、配置复杂。
Proftp:功能强大
vsftp:安全、高速、稳定。
系统默认 FTP 软件
启动:/etc/rc.d/init.d/vsftpd start(默认启动后即支持用户登录访问其宿主目录及匿
名访问)
配置文件:/etc/vsftpd/vsftpd.conf
设置选项
anonymous_enable=YES
允许匿名登录
local_enable=YES
允许本地用户登录
xferlog_enable=YES
xferlog_file=/var/log/vsftpd.log
激活上传和下载日志
ftpd_banner=Welcome to Sam’s FTP service,enjoy it
设置欢迎信息
listen_port=10011
指定非标准端口
允许用户可以上传文件到宿主目录
1、若启动 SELinux 开启上传设置
setsebool -P ftp_home_dir 1
*可关闭 SELinux,编辑/etc/selinux/config
setsebool-P allow_ftpd_full_access 1
2、确保 vsftpd 选项开启
write_enable=yes
idle_session_timeout=600
用户会话空闲 10 分钟后被挂断
max_clients=200
服务器的总的并发连接数为 200

max_per_ip=3
每个客户机的最大连接数为 3
local_max_rate=50000
anon_max_rate=30000
本地用户的最大传输速率为 50KB/s,匿名用户为 30KB/s。
[root@helen aa]# rpm -qa vsftpd
vsftpd-2.0.5-16.el5_4.1
[root@helen aa]# /etc/rc.d/init.d/vsftpd start
为 vsftpd 启动 vsftpd:[确定]
[root@helen aa]# ftp 192.168.9.90
Connected to 192.168.9.90.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (192.168.9.90:root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,9,90,57,81)
150 Here comes the directory listing.
drwxr-xr-x
2 0
0
4096 Dec 16 2009 pub
226 Directory send OK.
ftp> bye
221 Goodbye.
vsftpd 匿名登录访问目录是 ftp 伪用户的宿主目录:/var/ftp/pub/
[root@Mickey ~]# ftp 192.168.0.105
Connected to 192.168.0.105 (192.168.0.105).
220 (vsFTPd 2.0.5)
Name (192.168.0.105:root): samlee
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,0,105,188,54)
150 Here comes the directory listing.

drwxr-xr-x
2 500
500
1024 Feb 27 09:01 aa
-rw-r–r–
1 0
0
2467371 Feb 28 2011 apache_1.3.31.tar.gz
drwxr-xr-x 26 500
500
3072 Mar 04 2011 etc
-rw-r–r–
1 500
500
14107632 Feb 26 19:39 firefox-4.0b12.tar.bz2
-rw-r–r–
1 500
500
390 Feb 26 15:37 id_rsa.pub
226 Directory send OK.
ftp> get apache_1.3.31.tar.gz
local: apache_1.3.31.tar.gz remote: apache_1.3.31.tar.gz
227 Entering Passive Mode (192,168,0,105,206,131)
150 Opening BINARY mode data connection for apache_1.3.31.tar.gz (2467371 bytes).
226 File send OK.
2467371 bytes received in 0.0921 secs (2.6e+04 Kbytes/sec)
ftp> cd etc
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,0,105,231,157)
150 Here comes the directory listing.
-rw-r–r–
1 500
500
2420 Feb 28 2010 DIR_COLORS.xterm
drwxr-xr-x
4 500
500
1024 Feb 22 11:46 NetworkManager
drwxr-xr-x
4 500
500
1024 Feb 22 11:43 acpi
-rw-r–r–
1 500
500
44 Feb 25 14:51 adjtime
:
:
226 Directory send OK.
ftp> mget *.conf
mget autofs_ldap_auth.conf? y
227 Entering Passive Mode (192,168,0,105,210,170)
150 Opening BINARY mode data connection for autofs_ldap_auth.conf (2726 bytes).
226 File send OK.
2726 bytes received in 0.0159 secs (1.7e+02 Kbytes/sec)
mget capi.conf? y
227 Entering Passive Mode (192,168,0,105,48,163)
150 Opening BINARY mode data connection for capi.conf (351 bytes).
226 File send OK.
351 bytes received in 0.015 secs (23 Kbytes/sec)
ftp> prompt
Interactive mode off.
ftp> prompt
Interactive mode on.
ftp> prompt
Interactive mode off.
ftp> cd etc
250 Directory successfully changed.
ftp> mget *.conf
ftp> put grub.conf

local: grub.conf remote: grub.conf
227 Entering Passive Mode (192,168,0,105,60,227)
150 Ok to send data.
226 File receive OK.
655 bytes sent in 0.000578 secs (1.1e+03 Kbytes/sec)
ftp> mput *.conf
local: autofs_ldap_auth.conf remote: autofs_ldap_auth.conf
227 Entering Passive Mode (192,168,0,105,171,206)
150 Ok to send data.
226 File receive OK.
2726 bytes sent in 0.000532 secs (5e+03 Kbytes/sec)
ftp> bye
221 Goodbye.
[root@Mickey ~]# vi ftp.auto
open 192.168.0.105
user samlee samlee (user 指定用户名和密码)
binary
cd /etc
lcd /home/mickey
prompt
mget *.conf
bye
[root@Mickey ~]# ftp -n <ftp.auto >/dev/null 2>/root/ftp.msg (-n 关闭 ftp 提示模
式)
[root@Mickey ~]# ls /home/mickey/
capi.conf
idmapd.conf
mtools.conf
sestatus.conf
conman.conf
initlog.conf
multipath.conf
smartd.conf
………
[root@helen etc]# nmap 192.168.0.105
:
:
PORT
STATE SERVICE
21/tcp open ftp
:
:
[root@helen etc]# vi /etc/vsftpd/vsftpd.conf
110 listen=YES
111 listen_port=10011
“/etc/vsftpd/vsftpd.conf” 120L, 4630C written
[root@helen etc]# /etc/rc.d/init.d/vsftpd restart
关闭 vsftpd:[确定]
为 vsftpd 启动 vsftpd:[确定]
[root@Mickey ~]# ftp 192.168.0.105
ftp: connect: 拒绝连接

ftp> user
Not connected.
ftp> bye
[root@Mickey ~]# ftp 192.168.0.105 10011
Connected to 192.168.0.105 (192.168.0.105).
220 (vsFTPd 2.0.5)
Name (192.168.0.105:root): samlee
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> bye
221 Goodbye.
[root@helen etc]# nmap 192.168.0.105(没有 ftp 记录,可提高安全性)
PORT
STATE SERVICE
22/tcp open ssh
111/tcp open rpcbind
844/tcp open unknown
本地用户的访问控制
限制指定的本地用户不能访问,而其他本地用户可以访问:
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd.user_list
限制指定的本地用户可以访问,而其他本地用户不可以访问:
userlist_enable=YES
userlist_deny=NO
userlist_file=/etc/vsftpd.user_list
思考
如何设定网页存放目录,某个用户可以更新网页,另外两个用户只可以查看
1、mkdir /apache
2、授权
1)w
chown jack /apache
2)r
把两个用户加入组,改变/apache 所属组,对组授权 r–
setsebool -P allow_ftpd_full_access 1
3、ftp 192.168.9.75
ftp> cd /apache
[root@helen etc]#
[root@helen etc]#
drwxr-xr-x 2 root
[root@helen etc]#
mkdir /apache
ls -ld /apache
root 4096 02-27 21:44 /apache
chown samlee /apache

[root@helen etc]# ls -ld /apache
drwxr-xr-x 2 samlee root 4096 02-27 21:44 /apache
[root@helen etc]# groupadd webadmin
[root@helen etc]# grep webadmin /etc/group
webadmin:x:893:
[root@helen etc]# gpasswd -a jack webadmin
正在将用户“jack”加入到“webadmin”组中
[root@helen etc]# usermod -G webadmin mary
[root@helen etc]# grep webadmin /etc/group
webadmin:x:893:jack,mary
[root@helen etc]# chgrp webadmin /apache
[root@helen etc]# ls -ld /apache
drwxr-x— 2 samlee webadmin 4096 02-27 21:44 /apache
[root@helen etc]# getsebool -a | grep ftp
allow_ftpd_full_access –> off
[root@helen etc]# setsebool -P allow_ftpd_full_access 1
[root@helen etc]# getsebool -a | grep ftp
allow_ftpd_full_access –> on
[root@Mickey tmp]# ftp 192.168.0.105
Connected to 192.168.0.105 (192.168.0.105).
220 (vsFTPd 2.0.5)
Name (192.168.0.105:root): samlee
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /apache
250 Directory successfully changed.
ftp> put 1.bmp
local: 1.bmp remote: 1.bmp
227 Entering Passive Mode (192,168,0,105,251,58)
150 Ok to send data.
226 File receive OK.
921654 bytes sent in 0.0817 secs (1.1e+04 Kbytes/sec)
ftp> bye
221 Goodbye.
[root@Mickey tmp]# ftp 192.168.0.105
Connected to 192.168.0.105 (192.168.0.105).
220 (vsFTPd 2.0.5)
Name (192.168.0.105:root): mary
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /apache
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,0,105,235,228)
150 Here comes the directory listing.
-rw-r–r–
1 500
500
921654 Feb 27 14:09 1.bmp
226 Directory send OK.
ftp> put 3.bmp
local: 3.bmp remote: 3.bmp
227 Entering Passive Mode (192,168,0,105,178,11)
553 Could not create file.
ftp> bye
221 Goodbye.
[root@Mickey tmp]# ftp 192.168.0.105
Connected to 192.168.0.105 (192.168.0.105).
220 (vsFTPd 2.0.5)
Name (192.168.0.105:root): jack
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /apache
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,0,105,230,83)
150 Here comes the directory listing.
-rw-r–r–
1 500
500
921654 Feb 27 14:09 1.bmp
226 Directory send OK.
ftp> put 3.bmp
local: 3.bmp remote: 3.bmp
227 Entering Passive Mode (192,168,0,105,132,143)
553 Could not create file.
ftp> bye
221 Goodbye.