您的位置:首页 > 运维架构 > Linux

centos之samba共享服务

2019-04-03 08:48 417 查看

内容大纲:

samba简介

centos上部署samba服务

samba服务的配置详解

samba服务的共享资源部署

samba服务的共享资源权限设置

samba服务共享帐号映射

一、介绍
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。

SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。

SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

二、部署samba服务器

1、环境
平台:VMware workstation 14
系统:centos 7.4
远程终端:xshell

2、系统环境
(1)、关闭防火墙
systemctl stop firewalld
(2)、关闭selinux
setenforce 0 #临时关闭

3、配置本地yum源
cd /etc/yum.repos.d
vi local.repo
[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0

4、安装samba服务
yum install -y samba

如果报错:
mount /dev/cdrom /mnt

5、查看samba的配置文件
[root@lq1 ~]# cd /etc/samba/
[root@lq1 samba]# ls
lmhosts smb.conf smb.conf.example


说明:

smb.conf——是samba的配置文件

smb.conf.example——是samba的配置文件模版

6、修改samba配置文件
vi /etc/samba/smb.conf
#See smb.conf.example for a more detailed config file or
#read the smb.conf manpage.
#Run 'testparm' to verify the config is correct after
#you modified it.

[global] #全局设置
workgroup = SAMBA #samba服务的工作组
security = user #samba的安全级别,四种:user、share、server、domain

pass
4f0e
db backend = tdbsam   #定义samba用户后台的类型,3种:smbpasswd、tdbsam、ldapsam

printing = cups     #打印机所使用的类型
printcap name = cups  #打印机名称
load printers = yes   #是否在开启 samba server 时即共享打印机。
cups options = raw    #打印机的选项

[homes] #使用者本身的"家"目录,当使用者以samba使用者身份登入samba server 后,samba server 底下会看到自己的家目录,目录名称是使用者自己的帐号。
comment = Home Directories #描述信息
valid users = %S, %D%w%S #设定只有此名单内的用户才能访问共享资源(拒绝优先)(用户名/@组名)
browseable = No #是yes/否no在浏览资源中显示共享目录,若为否则必须指定共享路径才能存取
read only = No #是否只读
inherit acls = Yes #是否继承acl规则

[printers] #设置了samba服务器中打印共享资源的属性
comment = All Printers
path = /var/tmp #系统路径
printable = Yes #打印功能是否开启
create mask = 0600 #创建文件的默认的权限
browseable = No

[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = root
create mask = 0664
directory mask = 0775

三、配置共享资源

1、创建要共享的目录

mkdir /opt/aa

2、修改配置文件

vi /etc/samba/smb.conf

#See smb.conf.example for a more detailed config file or
#read the smb.conf manpage.
#Run 'testparm' to verify the config is correct after
#you modified it.

[global]
workgroup = SAMBA
security = user

passdb backend = tdbsam

printing = cups
printcap name = cups
load printers = yes
cups options = raw

[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No

[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = root
create mask = 0664
directory mask = 0775

[database] #定义共享名称
comment = this is test file #描述
path = /opt/aa #共享目录的绝对路径
public = yes #是否所有人可见

[点击并拖拽以移动]

2、启动samba服务,并设为开机自启动

systemctl start smb

systemctl enable smb

3、创建用于访问共享资源的账户信息。

在centos7版本中,samba服务程序默认使用的是用户口令认证模式,没有匿名模式。

这种认证模式可以去报仅让密码且受信任的用户访问共享资源,验证过程简单。

不过,只有建立账户信息数据库之后,才能使用用户口令认证模式。

此外,samba服务程序的数据库要求账户必须在当前系统中已经存在,否则日后创建文件时将导致文件的权限属性故障。

pdbedit 命令用于管理samba服务程序的账户信息数据库。

格式:

pdbedit [选项] 账户

pdbedit [选项] 账户

选项:

-a 用户名 :建立samba账户

-x 用户名 :删除samba帐户

-L :列出账户列表

-Lv :列出账户详细信息的列表

第一次把账户信息写入到数据库时需要使用“-a”参数,之后在修改密码、删除账户等操作都不需要了。

(1)创建系统用户

useradd jack

(2)将系统用户添加为samba用户

pdbedit -a jack

4、测试

使用windows系统去链接共享资源

微软按键+r

然后输入\192.168.80.100\database


出现如上图情况,说明samba服务共享资源成功

net use /d * //清除微软系统的共享访问缓存,让用户可以切换其它账号

四、配置共享资源的权限

1、修改samba配置文件

vi /etc/samba/smb.conf
#See smb.conf.example for a more detailed config file or
#read the smb.conf manpage.
#Run 'testparm' to verify the config is correct after
#you modified it.

[global]
workgroup = SAMBA
security = user

passdb backend = tdbsam

printing = cups
printcap name = cups
load printers = yes
cups options = raw

[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes

[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No

[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = root
create mask = 0664
directory mask = 0775

[database]
comment = this is test file
path = /opt/aa
public = yes
read only = yes #是否只读
writable = yes #是否不以只读方式共享当与read only发生冲突时,无视read only
valid users = jack,tom #设定只有此名单内的用户才能访问共享资源,可以通过 @test 加组名
write list = jack 若设定为只读时,则只有此设定的名单内的成员才可作写入动作(用户名/@组名)
directory mask = 0700 #建立目录时所给的权限
create mask = 0640 #建立文件时所给的权限
hosts deny = 192.168.80.6 #设定只有此网段/IP的用户不能访问共享资源
hosts allow = 192.168.80.0 173.11.0.0 #设定只有此网段/IP的用户才能访问共享资源

注:

权限比较:

write list = ... > read only = yes > writable = yes

注意一下系统目录本身的权限对用户是否有读写执行

2、重启服务

systemctl restart smb

3、测试

当使用jack用户创建文件夹时,报以下错误

其原因是因为/opt/aa没有写权限

更改一下权限:

chmod 777 /opt/aa

再次创建文件夹

此时,新建文件夹成功,说明系统目录的权限大于samba配置文件中设置的权限。

五、配置共享帐号映射

当需要大量的samba用户的时候,那么就需要大量的系统用户,这样就会造成用户帐号过度浪费。

为了解决这样的问题,就有了共享帐号映射。

用一个系统账号作为一个公共帐号,然后映射一些虚拟账号,这样的话,就可以多个帐号使用一个系统帐号。

1、创建帐号映射文件

vi /etc/samba/smbusers

其内容如下:

hunter = sunboy player

2、创建hunter系统用户,并将系统用户加入为samba用户

useradd hunter

pdbedit -a hunter

3、修改samba配置文件

vi /etc/samba/smb.conf

#See smb.conf.example for a more detailed config file or
#read the smb.conf manpage.
#Run 'testparm' to verify the config is correct after
#you modified it.

[global]
workgroup = SAMBA
security = user

passdb backend = tdbsam

printing = cups
printcap name = cups
load printers = yes
cups options = raw
username map = /etc/samba/smbusers   #添加此行内容

以下内容同上。

4、重启服务

systemctl restart smb

5、测试


从上图可以看出,当使用“player”帐号登录samba服务,实际使用的是hunter用户。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: