samba服务

1.samba简介

  • Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。
  • SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。

Samba监听端口有:

TCP UDP
139 445
137 138
  • tcp端口对应的服务是smbd服务,作用是提供对服务器中文件、打印资源的共享访问。
  • udp端相对应的服务是nmbd服务,作用是提供基于NetBIOS主机名称的解析。

samba进程:

进程 对应
nmbd 对应netbios
smbd 对应cifs协议
winbindd + ldap 对应Windows AD活动目录

samba用户:

帐号 密码
都是系统用户/etc/passwd Samba服务自有密码文件通过smbpasswd -a USERNAME命令设置
smbpasswd命令:
    -a Sys_User     //添加系统用户为samba用户并为其设置密码
    -d              //禁用用户帐号
    -e              //启用用户帐号
    -x              //删除用户帐号

Samba安全级别:

  • Samba服务器的安全级别有三个,分别是user,server,domain
安全级别 作用
user 基于本地的验证
server 由另一台指定的服务器对用户身份进行认证
domain 由域控进行身份验证

share在以前的版本支持,作用是支持匿名访问,现在的版本也可以匿名访问.

samba配置文件和常用参数:

  • /etc/samba/smb.conf(主配置文件)
samba三大组成 作用
[global] 全局配置,此处的设置项对整个samba服务器都有效
[homes] 宿主目录共享设置,此处用来设置Linux用户的默认共享,对应用户的宿主目录。当用户访问服务器中与自己用户名同名的共享目录时,通过验证后将会自动映射到该用户的宿主目录中
[printers] 打印机共享设置
[global] 全局参数
workgroup = MYGROUP 工作组名称
server string 表示描述samba服务器
security = user #安全验证的方式,总共有4种
share:来访主机无需验证口令;比较方便,但安全性很差
user:需验证来访主机提供的口令后才可以访问;提升了安全性
server:使用独立的远程主机验证来访主机提供的口令(集中管理账户)
domain:使用域控制器进行身份验证
passdb backend = tdbsam 定义用户后台的类型,共有3种
smbpasswd:使用smbpasswd命令为系统用户设置Samba服务程序的密码
tdbsam:创建数据库文件并使用pdbedit命令建立Samba服务程序的用户
ldapsam:基于LDAP服务进行账户验证
comment 表示设置对应共享目录的注释,说明信息,即文件共享名
browseable 表示设置目录是否可写
path 表示共享目录的路径
guest ok 表示设置是否所有人均可访问共享目录
public 表示设置是否允许匿名用户访问
write list 表示设置允许写的用户和组,组要用@表示,例如 write list = root,@root
valid users 设置可以访问的用户和组,例如 valid users = root,@root
hosts deny 设置拒绝哪台主机访问,例如 hosts deny = 192.168.72.1
hosts allow 设置允许哪台主机访问,例如 hosts allow = 192.168.72.2

printable 表示设置是否为打印机

配置匿名用户共享

创建共享目录:
[[email protected] ~]# mkdir /dd
[[email protected] ~]# chmod 777 /dd
[[email protected] ~]# vim /etc/samba/smb.conf
[global]
        workgroup = SAMBA
        security = user
        map to guest =Bad User -----添加的内容
 配置共享目录:
 [[email protected] ~]# vim /etc/samba/smb.conf
[dd]
        comment = This is a directory
        path = /dd
        guest ok = yes
        public = yes
        browseable = yes
        writable = yes
 启动smb服务:
[[email protected] ~]# systemctl enable smb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[[email protected] ~]# systemctl restart smb
在客户端上查看samba服务器的共享资源
[[email protected] ~]# smbclient -L 192.168.23.132 -U 'Bad User'
Enter SAMBA\Bad User's password: 
OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	dd              Disk      This is a directory
	IPC$            IPC       IPC Service (Samba 4.6.2)
OS=[Windows 6.1] Server=[Samba 4.6.2]

	Server               Comment
	---------            -------

	Workgroup            Master
	---------            -------
将samba服务器的共享目录挂载到客户机本地
[[email protected] ~]# mount -t cifs //192.168.23.132/dd /xx/ -o username='Bas User'
[[email protected] ~]# df -h
文件系统               容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root   17G  1.6G   16G    9% /
...
//192.168.23.132/dd     17G  1.1G   16G    7% /xx
[[email protected] ~]# cd /xx/
[[email protected] xx]# touch xx
[[email protected] xx]# ls
xx
在服务器上查看
[[email protected] ~]# cd /dd
[[email protected] dd]# ll
-rw-r--r--. 1 nobody nobody 0 1月  15 2019 xx

配置用户认证共享

创建用户和smb共享密码
[[email protected] dd]# useradd -M aa
[[email protected] dd]# cd
[[email protected] ~]# smbpasswd -a aa
New SMB password:
Retype new SMB password:
Added user aa.
配置映射用户
[[email protected] ~]# vim /etc/samba/xj
[[email protected] ~]# cat /etc/samba/xj
aa = xj
配置共享目录
[[email protected] ~]# vim /etc/samba/smb.conf

[global]
        workgroup = SAMBA
        security = user
        username  map = /etc/samba/xj -----添加的内容,删除map to guest ='Bad User'

[dd]
        comment = This is a directory
        path = /dd
        guest ok = yes
        public = yes
        browseable = yes
        writable = yes
        write list = xj
重启服务并在客户端查看smb共享资源
[[email protected] ~]# systemctl restart smb

[[email protected] ~]# smbclient -L 192.168.23.132 -U xj
Enter SAMBA\xj's password: 
Domain=[XJ] OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	dd              Disk      This is a directory
	IPC$            IPC       IPC Service (Samba 4.6.2)
	aa              Disk      Home Directories
Domain=[XJ] OS=[Windows 6.1] Server=[Samba 4.6.2]

	Server               Comment
	---------            -------

	Workgroup            Master
	---------            -------
	
将共享资源挂载到本地

[[email protected] ~]# mount -t cifs //192.168.23.132/dd /ss/ -o username=xj,password=1
[[email protected] ~]# df -h
文件系统               容量  已用  可用 已用% 挂载点
....
//192.168.23.132/dd     17G  1.1G   16G    7% /ss

将挂载写入配置文件
[[email protected] ~]# vim /etc/fstab 
//192.168.23.132/dd /ss cifs defaults,username=xj,password=1 0 0 

在客户端查看
[[email protected] ~]# cd /ss/
[[email protected] ss]# ll
总用量 0
-rw-r--r--. 1 1000 1000 0 1月  15 18:22 ll  ----拥有主为aa

[[email protected] dd]# id aa
uid=1000(aa) gid=1000(aa) 组=1000(aa)

在服务端验证
[[email protected] ~]# cd /dd/
[[email protected] dd]# ll
总用量 0
-rw-r--r--. 1 aa aa 0 1月  15 2019 ll
                                               
  • 用windows验证,新建文本文件windows.txt
    samba服务
在服务器上验证
[[email protected] dd]# ll
总用量 0
-rw-r--r--. 1 aa aa 0 1月  15 2019 ll
-rwxr--r--. 1 aa aa 0 1月  15 10:36 Windows.txt