-

上一章讲解的FTP文件传输服务确实可以让主机之间的文件传输变得简单方便,但是FTP协议的本质是传输文件,而非共享文件,因此要想通过客户端直接在服务器上修改文件内容还是一件比较麻烦的事情。

1987年,微软公司和英特尔公司共同制定了SMB(Server Messages Block,服务器消息块)协议,旨在解决局域网内的文件或打印机等资源的共享问题,这也使得在多个主机之间共享文件变得越来越简单。到了1991年,当时还在读大学的Tridgwell为了解决Linux系统与Windows系统之间的文件共享问题,基于SMB协议开发出了SMBServer服务程序。这是一款开源的文件共享软件,经过简单配置就能够实现Linux系统与Windows系统之间的文件共享工作。当时,Tridgwell想把这款软件的名字SMBServer注册成为商标,但却被商标局以SMB是没有意义的字符而拒绝了申请。后来Tridgwell不断翻看词典,突然看到一个拉丁舞蹈的名字—Samba,而且这个热情洋溢的舞蹈名字中又恰好包含了“SMB”,于是Samba服务程序的名字由此诞生(见图12-1)。Samba服务程序现在已经成为在Linux系统与Windows系统之间共享文件的最佳选择。

图12-1 Samba服务程序的logo

Samba服务程序的配置方法与之前讲解的很多服务的配置方法类似,首先需要先通过Yum软件仓库来安装Samba服务程序(Samba服务程序的名字也恰巧是软件包的名字):

[root@linuxprobe ~ ]# yum install samba Loaded plugins: langpacks, product-id, subscription-manager ………………省略部分输出信息……………… Installing: samba x86_64 4.1.1-31.el7 rhel 527 k Transaction Summary

Install 1 Package
Total download size: 527 k
Installed size: 1.5 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 Installing : samba-4.1.1-31.el7.x86_64 1/1 
 Verifying : samba-4.1.1-31.el7.x86_64 1/1 
Installed:
 samba.x86_64 0:4.1.1-31.el7 
Complete!

安装完毕后打开Samba服务程序的主配置文件,发现竟然有320行之多!有没有被吓到?但仔细一看就会发现,其实大多数都是以井号(#)开头的注释信息行。有刘遄老师在,肯定是不会让大家去“死啃”这些内容的。

[root@linuxprobe ~]# cat /etc/samba/smb.conf

This is the main Samba configuration file. For detailed information about the

# options listed here, refer to the smb.conf(5) manual page. Samba has a huge
# number of configurable options, most of which are not shown in this example.
#
# The Official Samba 3.2.x HOWTO and Reference Guide contains step-by-step
# guides for installing, configuring, and using Samba:
# http://www.samba.org/samba/docs/Samba-HOWTO-Collection.pdf
#
# The Samba-3 by Example guide has working examples for smb.conf. This guide is
# generated daily: http://www.samba.org/samba/docs/Samba-Guide.pdf
#
# In this file, lines starting with a semicolon (;) or a hash (#) are
# comments and are ignored. This file uses hashes to denote commentary and
# semicolons for parts of the file you may wish to configure.
#
# Note: Run the "testparm" command after modifying this file to check for basic
# syntax errors.
#
………………省略部分输出信息………………

由于在Samba服务程序的主配置文件中,注释信息行实在太多,不便于分析里面的重要参数,因此先把主配置文件改个名字,然后使用cat命令读入主配置文件,再在grep命令后面添加-v参数(反向选择),分别去掉所有以井号(#)和分号(;)开头的注释信息行,对于剩余的空白行可以使用^$参数来表示并进行反选过滤,最后把过滤后的可用参数信息通过重定向符覆盖写入到原始文件名称中。执行过滤后剩下的Samba服务程序的参数并不复杂,为了更方便读者查阅参数的功能,表12-1罗列了这些参数以及相应的注释说明。

表12-1 Samba服务程序中的参数以及作用

[global] #全局参数。 workgroup = MYGROUP #工作组名称 server string = Samba Server Version %v #服务器介绍信息,参数%v为显示SMB版本号 log file = /var/log/samba/log.%m #定义日志文件的存放位置与名称,参数%m为来访的主机名 max log size = 50 #定义日志文件的最大容量为50KB security = user #安全验证的方式,总共有4种

share:来访主机无需验证口令;比较方便,但安全性很差

#user:需验证来访主机提供的口令后才可以访问;提升了安全性
#server:使用独立的远程主机验证来访主机提供的口令(集中管理账户)
#domain:使用域控制器进行身份验证
passdb backend = tdbsam #定义用户后台的类型,共有3种
#smbpasswd:使用smbpasswd命令为系统用户设置Samba服务程序的密码
#tdbsam:创建数据库文件并使用pdbedit命令建立Samba服务程序的用户
#ldapsam:基于LDAP服务进行账户验证
load printers = yes #设置在Samba服务启动时是否共享打印机设备
cups options = raw  #打印机的选项
[homes]     #共享参数
comment = Home Directories  #描述信息
browseable = no #指定共享信息是否在“网上邻居”中可见
writable = yes  #定义是否可以执行写入操作,与“read only”相反
[printers]      #打印机共享参数
comment = All Printers  
path = /var/spool/samba #共享文件的实际路径(重要)。
browseable = no 
guest ok = no   #是否所有人可见,等同于"public"参数。
writable = no   
printable = yes 
[root@linuxprobe ~]# mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root@linuxprobe ~]# cat /etc/samba/smb.conf.bak | grep -v "#" | grep -v ";" | grep -v "^$" > /etc/samba/smb.conf
[root@linuxprobe ~]# cat /etc/samba/smb.conf