设为首页 收藏本站
查看: 1060|回复: 0

[经验分享] centos之samba共享服务

[复制链接]
YunVN网友  发表于 2019-4-17 16:14:16 |阅读模式
  内容大纲:
  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  #临时关闭
https://s1.运维网.com/images/blog/201904/02/9e551e286e60323942783805597bf502.png
  3、配置本地yum源
cd  /etc/yum.repos.d
vi local.repo
[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0
https://s1.运维网.com/images/blog/201904/02/d4c441952e255f2ae9ac6d3843005c33.png
  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

    passdb 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
https://s1.运维网.com/images/blog/201904/02/c601cf10a6010b3f3ac4c28ce559d899.png
  4、测试
  使用windows系统去链接共享资源
  微软按键+r
  然后输入\192.168.80.100\database


https://s1.运维网.com/images/blog/201904/02/2cbbc88967c8fdf2294aab61bf5345fb.png
出现如上图情况,说明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用户创建文件夹时,报以下错误
https://s1.运维网.com/images/blog/201904/02/2cf2701efaad24967234ca0c60b725d3.png
  其原因是因为/opt/aa没有写权限
  更改一下权限:
  chmod 777 /opt/aa

  再次创建文件夹
https://s1.运维网.com/images/blog/201904/02/4d8c15531cd7d5a4ae55d1516ec276f3.png
  此时,新建文件夹成功,说明系统目录的权限大于samba配置文件中设置的权限。
  五、配置共享帐号映射
  当需要大量的samba用户的时候,那么就需要大量的系统用户,这样就会造成用户帐号过度浪费。
  为了解决这样的问题,就有了共享帐号映射。
  用一个系统账号作为一个公共帐号,然后映射一些虚拟账号,这样的话,就可以多个帐号使用一个系统帐号。
  1、创建帐号映射文件
  vi /etc/samba/smbusers
  其内容如下:
  hunter = sunboy player
  2、创建hunter系统用户,并将系统用户加入为samba用户
  useradd hunter
  pdbedit -a hunter
https://s1.运维网.com/images/blog/201904/03/ebb692bd04c704f73d92cf1d6fff21e8.png
  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、测试


https://s1.运维网.com/images/blog/201904/03/074a148a251fbb3ff514ac48f841b170.png
从上图可以看出,当使用“player”帐号登录samba服务,实际使用的是hunter用户。





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-828322-1-1.html 上篇帖子: CentOS7 设置系统时间 下篇帖子: centos上使用 yum 安装 apache 、 php 、 mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表