如何在服务器上架设samba服务端?

  1. 首先需要先通过Yum软件仓库来安装Samba服务程序

    [root]# 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!
    
  2. 安装完毕后打开Samba服务程序的主配置文件

    [root]# 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.
    #
    ………………省略部分输出信息………………
    
  3. 由于在Samba服务程序的主配置文件中,注释信息行实在太多,不便于分析里面的重要参数,因此先把主配置文件改个名字,然后使用cat命令读入主配置文件,再在grep命令后面添加-v参数(反向选择),分别去掉所有以井号(#)和分号(;)开头的注释信息行,对于剩余的空白行可以使用^$参数来表示并进行反选过滤,最后把过滤后的可用参数信息通过重定向符覆盖写入到原始文件名称中。

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]# mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root]# cat /etc/samba/smb.conf.bak | grep -v "#" | grep -v ";" | grep -v "^$" > /etc/samba/smb.conf
[root]# cat /etc/samba/smb.conf

第1步:创建用于访问共享资源的账户信息。在RHEL 7系统中,Samba服务程序默认使用的是用户口令认证模式(user)。这种认证模式可以确保仅让有密码且受信任的用户访问共享资源,而且验证过程也十分简单。不过,只有建立账户信息数据库之后,才能使用用户口令认证模式。另外,Samba服务程序的数据库要求账户必须在当前系统中已经存在,否则日后创建文件时将导致文件的权限属性混乱不堪,由此引发错误。

pdbedit命令用于管理SMB服务程序的账户信息数据库,格式为“pdbedit [选项] 账户”。在第一次把账户信息写入到数据库时需要使用-a参数,以后在执行修改密码、删除账户等操作时就不再需要该参数了。pdbedit命令中使用的参数以及作用如表12-3所示。

表12-3 用于pdbedit命令的参数以及作用

参数作用
-a 用户名建立Samba用户
-x 用户名删除Samba用户
-L列出用户列表
-Lv列出用户详细信息的列表
[root@]# id linuxprobe
uid=1000(linuxprobe) gid=1000(linuxprobe) groups=1000(linuxprobe)
[root@]# pdbedit -a -u linuxprobe
new password:此处输入该账户在Samba服务数据库中的密码
retype new password:再次输入密码进行确认
Unix username: linuxprobe
NT username: 
Account Flags: [U ]
User SID: S-1-5-21-507407404-3243012849-3065158664-1000
Primary Group SID: S-1-5-21-507407404-3243012849-3065158664-513
Full Name: linuxprobe
Home Directory: \\localhost\linuxprobe
HomeDir Drive: 
Logon Script: 
Profile Path: \\localhost\linuxprobe\profile
Domain: LOCALHOST
Account desc: 
Workstations: 
Munged dial: 
Logon time: 0
Logoff time: Wed, 06 Feb 2036 10:06:39 EST
Kickoff time: Wed, 06 Feb 2036 10:06:39 EST
Password last set: Mon, 13 Mar 2017 04:22:25 EDT
Password can change: Mon, 13 Mar 2017 04:22:25 EDT
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
第2步:创建用于共享资源的文件目录。在创建时,不仅要考虑到文件读写权限的问题,而且由于/home目录是系统中普通用户的家目录,因此还需要考虑应用于该目录的SELinux安全上下文所带来的限制。在前面对Samba服务程序配置文件中的注释信息进行过滤时,这些过滤的信息中就有关于SELinux安全上下文策略的说明,我们只需按照过滤信息中有关SELinux安全上下文策略中的说明中给的值进行修改即可。修改完毕后执行restorecon命令,让应用于目录的新SELinux安全上下文立即生效。

[root@]# mkdir /home/database
[root@]# chown -Rf linuxprobe:linuxprobe /home/database
[root@]# semanage fcontext -a -t samba_share_t /home/database
[root@]# restorecon -Rv /home/database
restorecon reset /home/database context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:samba_share_t:s0
第3步:设置SELinux服务与策略,使其允许通过Samba服务程序访问普通用户家目录。执行getsebool命令,筛选出所有与Samba服务程序相关的SELinux域策略,根据策略的名称(和经验)选择出正确的策略条目进行开启即可:

[root@]# getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off
[root@]# setsebool -P samba_enable_home_dirs on

第4步:在Samba服务程序的主配置文件中,根据表12-2所提到的格式写入共享信息。在原始的配置文件中,[homes]参数为来访用户的家目录共享信息,[printers]参数为共享的打印机设备。这两项如果在今后的工作中不需要,可以像刘遄老师一样手动删除,这没有任何问题。

[root@]# vim /etc/samba/smb.conf 
[global]
 workgroup = MYGROUP
 server string = Samba Server Version %v
 log file = /var/log/samba/log.%m
 max log size = 50
 security = user
 passdb backend = tdbsam
 load printers = yes
 cups options = raw
[database]
 comment = Do not arbitrarily modify the database file
 path = /home/database
 public = no
 writable = yes

第5步:Samba服务程序的配置工作基本完毕。接下来重启smb服务(Samba服务程序在Linux系统中的名字为smb)并清空iptables防火墙,然后就可以检验配置效果了。

[root@]# systemctl restart smb
[root@]# systemctl enable smb
 ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
[root@]# iptables -F
[root@]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]

最后:挂载