SMB Server Message Block
之前我们学习过NFS,它是用来做类UNIX系统之间做资源共享,而今天学习的SMB主要是在类unix与windows之间做资源共享.
学习目标
SAMBA工作流程
熟练SAMBA的配置匿名与验证共享模式
熟练使用SAMBA的客户端访问方法和mount方法
学习内容
安装服务器及客户端软件
配置SAMBA匿名共享
配置SAMBA验证共享
(1)Samba工作流程
当客户端访问服务器时,信息通过SMB协议进行传输,其工作过程可以分成四个步骤:
步骤1:协议协商
客户端在访问Samba服务器时,发送negprot指令数据包,告知目标计算机其支持的SMB类型。Samba服务器根据客户端的情况,选择最优的SMB类型,并做出回应。
--------negprot请求------->
客户端 服务器
<--------negprot响应-------
步骤2:建立连接
当SMB类型确认后,客户端会发送session setup指令数据包,提交帐号和密码,请求与Samba服务器建立连接,如果客户端通过身份验证,Samba服务器会对session setup报文作出回应,并为用户分配唯一的UID,在客户端与其通信时使用。
--------session setup 请求------->
客户端 服务器
<--------session setup 响应-------
步骤3:访问共享资源
客户端访问Samba共享资源时,发送tree connect指令数据包,通知服务器需要访问的共享资源名,如果设置允许,Samba服务器会为每个客户端与共享资源连接分配TID,客户端即可访问需要的共享资源。
--------tree connect 请求------->
客户端 服务器
<--------tree connect 响应-------
步骤4:断开连接
共享使用完毕,客户端向服务器发送tree disconnect报文关闭共享,与服务器断开连接。
--------tree disconnect请求------->
客户端 服务器
<--------tree disconnect响应-------
安装涉及的软件包
# rpm -qa | grep samba
samba-swat-3.0.28-0.el5.8 # WEB方式配置程序
samba-common-3.0.28-0.el5.8 # 公共组件
samba-client-3.0.28-0.el5.8 # 客户端程序
samba-3.0.28-0.el5.8 # 服务器端程序
#
服务控制
脚本名 /etc/rc.d/init.d/smb
service smb restart
smbd:SMB服务器
nmbd:netbios名称服务器
nmbd 使用的端口是 137和138
smbd使用的端口是139和445,所以一般禁止samba服务我们控制139和445端
samba服务的主配置文件/etc/samba/smb.conf主要由两部分组成
Global Settings 全局参数设置
该设置都是与Samba 服务整体运行环境有关的选项,它的设置项目是针对所有共享资源的
[Share Definitions] 共享目录
该设置针对的是共享目录私有参数,只对当前的共享资源起作用
配置:
workgroup = MYGROUP # 设定工作组
server string = Samba Server Version %v
# 描述服务器版本 %v是变量 取当前版本
security = user # 指明验证模式 share匿名模式 不验证用户名和密码
user验证模式 验证用户名和密码
注意:匿名用户此时依旧可用 用空密码验证则通过
限制匿名需要用valid users =
server验证服务器模式
domain由WINDOWS域服务器验证
passdb backend = tdbsam # 密码的加密存储模式
load printers = yes
cups options = raw # 访问SAMBA时自动加载打印机
共享目录
SAMBA配置文件中的共享目录是以[abc]开始的,其中abc就是共享名字,每个共享目录除了继承全局参数的配置 还可以有专属参数来控制目录的不同共享种类
默认情况下会开启用户家目录和打印机的共享设置,通过[homes]实现系统中的用户并被smbpasswd添加后的用户可以直接访问各自的加目录
[homes]
comment = Home Directories
browseable = no
writable = yes
valid users = %S
[printers]
comment = All Printers
path = /var/spool/samba
browseable = no
guest ok = no
writable = no
printable = yes
[myshare]
comment = 共享目录的注释信息
path = 你可以共享文件系统的任何目录 写绝对路径
browseable = yes/no 共享名是否可见
public = yes/no 把目录是否匿名共享
writable = yes/no 共享目录是以Read Only还是read write方式共享
其他常用参数
Directory mask = 750设置用户建立目录时的权限 和chmod数字模式写法一样
create mask =750 设置用户创建文件时的权限 和chmod数字模式写法一样
上面两个值对mount加载方式无效
write list = +seker 设置组可以写入此目录权限 组是系统组 SMB不需添加
valid users = user1 user2 允许访问此目录的用户
printable = yes/no
建立SAMBA用户
首先建立系统用户 useradd seker
再将系统用户添加为samba用户 smbpasswd -a seker
SAMBA用户的密码与系统用户的密码没任何关系
客户端工具
smbclient
-L ip 探查目标主机的目录共享信息
得到共享名后可以登陆 -U 指明登录用户
smbclient //192.168.1.31 -U seker
登录后和lftp操作差不多 可以用help来查看帮助
可以把共享目录通过mount加载到本地使用
mount -t cifs //server_IP/共享名 /加载点
建立匿名共享目录
[global]
security = share
[public]
comment = my pub
path = /mnt/pub
writable = yes
public = yes
建立验证共享目录
切记:匿名用户+空密码也能在这个模式下通过验证
[global]
security = user
[my_user]
comment = my dir
path = /mnt/user
writable = yes
public = yes
特殊共享 用户家目录 (默认支持)
[homes]
comment = Home Directories
browseable = no
writable = yes
; valid users = %S 默认被注释掉了.则用户通过自身密码验证和系统权限验证就可以访问其他用户的目录,如果只限制用户访问自己家的资源 开启即可
# smbclient //10.10.10.31/zorro -U seker
Password:
Domain=[STU31] OS=[Unix] Server=[Samba 3.0.25b-0.el5.4]
tree connect failed: NT_STATUS_ACCESS_DENIED
例一:共享目录abc,seker和zorro可以访问,只有seker组的成员才可以写
[abc]
comment = my share
path = /var/ftp/pub
valid users = seker zorro
writable = no
public = no
printable = no
create mask = 0765
write list = @seker
中文支持
display charset (G)
Specifies the charset that samba will use to print messages to stdout and
stderr. The default value is "LOCALE", which means automatically set, depend-
ing on the current locale. The value should generally be the same as the value
of the parameter unix charset.
Default: display charset = "LOCALE" or "ASCII" (depending on the system)
Example: display charset = UTF8
dos charset (G)
DOS SMB clients assume the server has the same charset as they do. This option
specifies which charset Samba should talk to DOS clients.
The default depends on which charsets you have installed. Samba tries to use
charset 850 but falls back to ASCII in case it is not available. Run test-
parm(1) to check the default on your system.
No default
unix charset (G)
Specifies the charset the unix machine Samba runs on uses. Samba needs to
know this in order to be able to convert text to the charsets other SMB
clients use.
This is also the charset Samba will use when specifying arguments to
scripts that it invokes.
Default: unix charset = UTF8
访问控制
hosts allow
Example 1: allow all IPs in 150.203.*.*; except one
hosts allow = 150.203. EXCEPT 150.203.6.66
Example 2: allow hosts that match the given network/netmask
hosts allow = 150.203.15.0/255.255.255.0
Example 3: allow a couple of hosts
hosts allow = lapland, arvidsjaur
Example 4: allow only hosts in NIS netgroup "foonet", but deny access from one
particular host
hosts allow = @foonet
hosts deny = pirate
Default: hosts allow = # none (i.e., all hosts permitted access)
Example: hosts allow = 150.203.5. myhost.mynet.edu.au
hosts deny
Default: hosts deny = # none (i.e., no hosts specifically excluded)
Example: hosts deny = 150.203.4. badhost.mynet.edu.au
[root@stu254 samba]# testparm /etc/samba/smb.conf www.163.com 10.10.10.0
Load smb config files from /etc/samba/smb.conf
Processing section "[homes]"
Processing section "[printers]"
Processing section "[myshare]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Allow connection from www.163.com (10.10.10.0) to homes
Allow connection from www.163.com (10.10.10.0) to printers
Allow connection from www.163.com (10.10.10.0) to myshare
[root@stu254 samba]#
vsftp
学习目标:
了解主动与被动模式
熟悉vsftpd.conf配置文件
掌握匿名用户和本地用户的配置参数
掌握vsftp服务权限和系统权限的限制
匿名和本地用户的限速和黑白名单控制方法
虚拟用户的建立
为不同的虚拟用户分配不同权限
主动与被动
主动
client server
1024+ ---------------> 21
1024+N <-------------- 20
客户端使用1024以上的端口连接服务器的21号端口,并且会事先开启一个1024+N的端口准备让服务器来连接,服务器得知客户端准备的端口后,用20号端口与客户端连接,其中21号端口称为连接链路,20号端口称为数据链路,创建数据链路时server端处于主动去连接客户端的状态 固称为 主动传输模式
被动
client server
1024+ ---------------> 21
1024+M
1024+ <--------------
1024+N --------------->1024+M
客户端使用1024以上的端口连接服务器的21号端口,服务器接到连接请求后,开启一个1024+M的端口等待客户端来连接,再通知客户端这个等待连接的端口号是1024+M,客户端接到服务器端的信息后,开启新的一个1024+N的端口与1024+M进行连接,其中21号端口为连接链路,1024+M端口为数据链路,创建数据连接时,server端处于被客户端连接的状态,固称为 被动传输模式
vsftp 默认是根据客户端不同而使用不同模式,windows客户端则使用主动,linux客户端则使用被动
可以用 pasv_enable=NO 来强制使用主动
安装
[root@mail home]# rpm -qa | grep vsftpd
vsftpd-2.0.5-12.el5
[root@mail home]#
配置文件的位置
[root@mail vsftpd]# pwd
/etc/vsftpd
[root@mail vsftpd]#
默认参数
[root@stu31 ~]# grep -v '^#' /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@stu31 ~]#
匿名用户有ftp anonymous两个 只能下载文件
匿名用户的相关参数(更多的查看 man vsftpd.conf):
anonymous_enable=YES # 开启匿名用户
write_enable=YES # 本地用户可写
anon_upload_enable=YES # 匿名用户可以上传
anon_mkdir_write_enable=YES # 匿名用户可以创建目录
anon_other_write_enable=YES # 匿名用户可以删除文件和目录
查手册,找到匿名用户删改的选项
man 5 vsftpd.conf
anon_other_write_enable
If set to YES, anonymous users will be permitted to perform write
operations other than upload and create directory, such as deletion
and renaming. This is generally not recommended but included for
completeness.
Default: NO
# service vsftpd restart
[root@mail vsftpd]# chown ftp:ftp /var/ftp/pub
可以匿名测试.
修改匿名用户的目录
anon_root
This option represents a directory which vsftpd will try to change
into after an anonymous login. Failure is silently ignored.
Default: (none)
改成 /tmp 目录测试一下.
结果:对--7的目录FTP会自动拒绝.因目录权限太大
匿名账户配置结束.
口令账户登录
local_enable=YES
local_umask=022
[root@mail ~]# lftp -u seker,123 192.168.0.240
lftp seker@192.168.0.240:~> ls
lftp seker@192.168.0.240:~> cd /
cd 成功, 当前目录=/
lftp seker@192.168.0.240:/> cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
1689 bytes transferred
lftp seker@192.168.0.240:/>
权限太大了.
加个参数限制.默认没启用这个参数
chroot_local_user=YES # 限制所有本地用户在家目录里
另一种指定用户限制,非限制用户依旧可以畅通无阻
chroot_local_user=NO
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list 凡是名单中的人不允许离开HOME目录
建立黑名单文件.
[root@mail vsftpd]# cat chroot_list
seker
user1
[root@mail vsftpd]#
测试
[root@mail ~]# lftp -u seker,123 192.168.0.240
lftp seker@192.168.0.240:/> cd /
lftp seker@192.168.0.240:/> ls
lftp seker@192.168.0.240:/> pwd
ftp://seker:123@192.168.0.240/
lftp seker@192.168.0.240:/> cd /etc/
cd: 访问失败: 550 Failed to change directory. (/etc)
root用户默认是不允许登录ftp的
[root@mail ~]# lftp -u root 192.168.0.240
口令:
lftp root@192.168.0.240:~> ls
`ls' at 0 [重新连接前延时: 29]
只要存在于 ftpusers 里的用户都是禁止登陆ftp的
这是由 user_list 控制的 里面的用户都禁止/允许登录ftp
他是黑名单还是白名单取决于 userlist_deny=YES 参数
如果是YES(默认值),则是黑名单,表示里面的人禁止登陆
如果是NO,则是白名单,表示只允许名单里的人登录ftp
[root@mail vsftpd]# cat user_list |grep -v ^#
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@mail vsftpd]#
vsftp日志相关参数
xferlog_enable=YES
如果启用,将会维护一个日志文件,用于详细记录上传和下载,默认记录在/var/log/vsftpd.log
dual_log_enable=YES
如果启用,将会有两份日志/var/log/xferlog and /var/log/vsftpd.log
xferlog 用于记录wu-ftpd的格式日志 经常用于日志分析软件使用
vsftpd.log 则是vsftp自身格式的日志
xferlog_std_format=YES
标准的日志格式 难以阅读
Tue Jun 9 06:05:04 2009 1 10.10.10.31 0 /pub/ftp/my_file b _ i a lftp@ ftp 0 * i
syslog_enable
默认是关闭的 表示用/var/log/messages来取代/var/log/vsftpd.conf
下载上传速率:
local_max_rate=10000 (10k)
anon_max_rate=5000(5k)
主被模式切换
pasv_enable
Set to NO if you want to disallow the PASV method of obtaining a
data connection.
Default: YES
虚拟用户
1.建立用户数据库
# cat ftpuser.txt
test111 # 账户
pass111 # 密码
test222
pass222
# rpm -qf $(which db_load)
db4-utils-4.3.29-9.fc6
#
# db_load -T -t hash -f ~/ftpuser.txt /etc/vsftpd/vsftpd_login.db
2.建立验证模式文件
# mv /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
# vi /etc/pam.d/vsftpd
auth required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
:wq
3.建立一个所有虚拟用户使用的系统用户
# useradd -d /home/vsftp virtual
# chmod 755 /home/vsftp
4.修改配置文件 启用虚拟用户
guest_enable=YES # 开启虚拟用户功能
guest_username=virtual # 指定虚拟用户使用的系统用户
pam_service_name=vsftpd # 以/etc/pam.d/验证模式文件名相同
5.重新启动后 可以用虚拟用户登陆了.
为虚拟用户设置不同权限
virtual_use_local_privs
设置为 YES 时,虚拟用户使用与本地用户相同权限,设置为 NO 时,虚拟用户使用与匿名用户相同权限
其实,这个功能是为每个用户指明私有参数,二次加载时覆盖了全局参数而实现的.
1.添加 per_user 功能参数,如果开启了此功能,必须为每一个用户都有配置文件
vi /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vsftp_user_conf
:wq
2.在/etc/vsftpd/vsftp_user_conf这个目录下建立与虚拟用户同名的文件,文件中写入用户的私有参数
# echo "write_enable=NO" /etc/vsftpd/vsftp_user_conf/test111
我们限制 test111 禁止上传文件
3.重启动vsftpd 登录测试.
APACHE
学习目标
掌握apache服务器的基本配置
学习内容
使用rpm包安装apache
httpd.conf文件的格式
web服务的基本配置方法
userdir alias模块的使用
目录容器
用户认证和访问控制
虚拟主机的实现
1.使用rpm包安装apache
rpm -ivh httpd-version.rpm
rpm -ivh httpd-manual-version.rpm
后台进程:http
启动脚本: /etc/rc.d/init.d/httpd
使用端口: 80(http) 443(https)
网页位置: /var/www/html
手册使用: http://localhost/manual/
配置路径: /etc/httpd/
[root@stu254 httpd]# pwd
/etc/httpd
[root@stu254 httpd]# ls
conf conf.d logs modules run
[root@stu254 httpd]#
2.httpd.conf文件的格式
ServerRoot "/etc/httpd"
Listen 80
Include conf.d/*.conf
User apache
Group apache
ServerName www.example.com:80
DocumentRoot "/var/www/html"
DirectoryIndex index.html index.html.var
3.web服务的基本配置方法
vim /var/www/html/index.html
4.userdir alias模块的使用
userdir配置
httpd.conf
# UserDir disable
UserDir public_html
mkdir /home/seker/public_html
echo "hello.. seker's web..." > /home/seker/public_html/index.html
chmod 755 /home/seker
service httpd restart
http://localhost/~seker/
alias配置
conf.d/down.conf
alias /down /var/ftp/pub
<directory /var/ftp/pub>
options indexes
</directory>
service httpd restart
http://localhost/down
5. 用户认证
conf.d/down.conf
alias /down /var/ftp/pub
<directory /var/ftp/pub>
options indexes
authtype basic
authname "seker password"
AuthBasicProvider file
AuthUserFile /etc/httpd/passwdb
Require user seker zorro # 只允许两个用户访问
# Require valid-user # 允许密码文件中的所有用户访问
</directory>
建立密码文件和用户
htpasswd -c -b /etc/httpd/passwdb zorro 1234
htpasswd -b /etc/httpd/passwdb seker 1234
service httpd restart
http://localhost/down
6. 访问控制
conf.d/down.conf
alias /down /var/ftp/pub
<directory /var/ftp/pub>
options indexes
order deny,allow
deny from all
allow from 10.10.10.32
# 先去把规则全解释一遍,之后发现有冲突,那么使用order表,order表从后向前的顺序应用.
</directory>
service httpd restart
http://localhost/down
7.CGI脚本
CGI(公共网关接口[Common Gateway Interface])定义了网站服务器与外部内容协商程序之间交互的方法,通常是指CGI程序或者CGI脚本,是在网站上实现动态页面的最简单而常用的方法。说白了就是用一种语言输出HTML语法,再被浏览器解释,呈现出页面内容.
默认的情况下 httpd.conf 中已经定义了一个脚本目录 并且允许所有人来访问.
这是一种用ScriptAlias关键字定义CGI目录的方法.
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
也可以再目录容器中使用options参数为目录增加执行CGI的功能
<Directory /usr/local/apache/htdocs/somedir>
Options +ExecCGI
</Directory>
使用系统默认的CGI目录,建立CGI程序
# pwd
/var/www/cgi-bin
# cat cgi.sh
#!/bin/bash
echo -e "Content-type: text/html/n"
uname -a
echo "<br>"
date "+%F %T"
#
访问:
http://10.10.10.31/cgi-bin/cgi.sh
CGI FAQ:
从网络访问CGI程序,浏览器中可能会发生四种情况:
CGI程序的输出
这说明一切正常。
CGI程序的源代码或者一个"POST Method Not Allowed"消息
这说明Apache没有被正确配置以执行CGI程序,重新阅读configuring Apache看看遗漏了什么。
一个以"Forbidden"开头的消息
这说明有权限问题。参考Apache error log和下面的文件的权限。
一个"Internal Server Error"消息
查阅Apache error log,可以找到CGI程序产生的出错消息"Premature end of script headers"。对此,需要检查下列各项,以找出不能产生正确HTTP头的原因。
7.虚拟主机
基于名称的虚拟主机
需要两个域名能够解析到你的服务器
[root@stu254 conf]# tail -n 2 /etc/hosts
10.10.10.31 www.seker.com
10.10.10.31 web.seker.com
[root@stu254 conf]#
# cd /var/www/html/
# mkdir www web
# echo "hello..wwww. this is www.seker.com" >> www/index.html
# echo "hello..webb. this is web.seker.com" >> web/index.html
修改配置文件httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html/www
ServerName www.seker.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/web
ServerName web.seker.com
</VirtualHost>
service httpd restart
http://www.seker.com
http://web.seker.com
基于ip的虚拟主机
需要配置两个ip
[root@stu254 html]# ip a | grep inet |grep eth
inet 192.168.1.31/24 brd 192.168.1.255 scope global eth0
inet 10.10.10.31/24 brd 10.10.10.255 scope global eth1
[root@stu254 html]#
[root@stu254 conf]# tail -n 2 /etc/hosts
10.10.10.31 www.seker.com
192.168.1.31 web.seker.com
[root@stu254 conf]#
修改配置文件httpd.conf
# NameVirtualHost *:80
<VirtualHost 10.10.10.31:80>
DocumentRoot /var/www/html/www
ServerName www.seker.com
</VirtualHost>
<VirtualHost 192.168.1.31:80>
DocumentRoot /var/www/html/web
ServerName web.seker.com
</VirtualHost>
service httpd restart
http://www.seker.com
http://web.seker.com
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jiangcl207504/archive/2009/11/30/4901483.aspx