12.1 SAMBA文件共享服务

2023-05-12 11:12 更新

上一章讲解的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服务程序中的参数以及作用

行数 参数 作用
1 # See smb.conf.example for a more detailed config file or 注释信息
2 # read the smb.conf manpage.
3 # Run 'testparm' to verify the config is correct after
4 # you modified it.
5 [global] 全局参数
6 workgroup = SAMBA 工作组名称
7
8 security = user 安全验证的方式,总共有4种
9
10 passdb backend = tdbsam 定义用户后台的类型,总共有3种
11
12 printing = cups 打印服务协议
13 printcap name = cups 打印服务名称
14 load printers = yes 是否加载打印机
15 cups options = raw 打印机的选项
16
17 [homes] 共享名称
18 comment = Home Directories 描述信息
19 valid users = %S, %D%w%S 可用账户
20 browseable = No 指定共享信息是否在“网上邻居”中可见
21 read only = No 是否只读
22 inherit acls = Yes 是否继承访问控制列表
23
24 [printers] 共享名称
25 comment = All Printers 描述信息
26 path = /var/tmp 共享路径
27 printable = Yes 是否可打印
28 create mask = 0600 文件权限
29 browseable = No 指定共享信息是否在“网上邻居”中可见
30
31 [print$] 共享名称
32 comment = Printer Drivers 描述信息
33 path = /var/lib/samba/drivers 共享路径
34 write list = @printadmin root 可写入文件的用户列表
35 force group = @printadmin 用户组列表
36 create mask = 0664 文件权限
37 directory mask = 0775 目录权限
    [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
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号