samba

Samba

samba简介

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

在此之前我们已经了解了NFS,NFS与samba一样,也是在网络中实现文件共享的一种实现,但不幸的是,其不支持windows平台,而本章要提到的samba是能够在任何支持SMB协议的主机之间共享文件的一种实现,当然也包括windows。

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

SMB协议是C/S型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源

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              //删除用户帐号
 
 
[root@localhost ~]# yum -y install samba-*
[root@localhost ~]# useradd tom
[root@localhost ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom.

Samba安全级别

Samba服务器的安全级别有三个,分别是user,server,domain

安全级别 作用
user 基于本地的验证
server 由另一台指定的服务器对用户身份进行认证
domain 由域控进行身份验证
  • 以前的samba版本支持的安全级别有四个,分别是share,user,server,domain
  • share是用来设置匿名访问的,但现在的版本已经不支持share了,但是还是可以实现匿名访问的
    只是配置方式变了

samba配置文件

  • /etc/samba/smb.conf(主配置文件)
samba三大组成 作用
[global] 全局配置,此处的设置项对整个samba服务器都有效
[homes] 宿主目录共享设置,此处用来设置Linux用户的默认共享,对应用户的宿主目录。
当用户访问服务器中与自己用户名同名的共享目录时,通过验证后将会自动映射到该用户的宿主目录中
[printers] 打印机共享设置

常用配置文件参数

参数 作用
workgroup 表示设置工作组名称
server string 表示描述samba服务器
security 表示设置安全级别,其值可为share、user、server、domain
passdb backend 表示设置共享帐户文件的类型,其值可为tdbsam(tdb数据库文件)
ldapsam(LDAP目录认证)、smbpasswd(兼容旧版本samba密码文件)
comment 表示设置对应共享目录的注释,说明信息,即文件共享名
browseable 表示设置共享是否可见
writable 表示设置目录是否可写
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 表示设置是否为打印机
//测试配置文件是否有语法错误,以及显示最终生效的配置:使用testparm命令
[root@localhost ~]# testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Loaded services file OK.
Server role: ROLE_STANDALONE

映射共享目录

//创建用户yanchuang:
[root@yc1 ~]# useradd -M yanchuang

//为yanchuang用户创建smb共享密码:
[root@yc1 ~]# smbpasswd -a yanchuang
New SMB password:123456
Retype new SMB password:123456
Added user yanchuang.
 
//假设这里映射yanchuang用户为share用户,那么就要在/etc/samba/smbusers文件中添加如下内容
[root@yc1 ~]# echo 'yanchuang = share' > /etc/samba/smbusers
 
//在全局配置中添加如下内容:
[root@yc1 ~]# vim /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
        username map = /etc/samba/smbusers      //添加此行内容


//创建一个共享目录yanchuang
[root@cy1 ~]# mkdir /opt/yanchuang
[root@yc1 ~]# chown -R yanchuang.yanchuang /opt/yanchuang/
[root@yc1 ~]# ll /opt/
total 0
drwxr-xr-x. 2 yanchuang yanchuang 15 Apr 6 22:05 yanchuang


//配置共享
[root@yc1 ~]# cat >> /etc/samba/smb.conf <<EOF
> [yanchuang]
> comment = yanchuang
> path = /opt/yanchuang
> browseable = yes
> guest ok = yes
> writable = yes
> write list = share
> public = yes
> EOF

[root@yc1 ~]# tail -8 /etc/samba/smb.conf
[yanchuang]
comment = yanchuang
path = /opt/yanchuang
browseable = yes
guest ok = yes
writable = yes
write list = share
public = yes


 
//启动smb服务:
[root@yc1 ~]# systemctl start smb
 
//重启smb服务:
[root@yc1 ~]# systemctl restart smb
 
//重新加载smb服务:
[root@yc1 ~]# systemctl reload smb
 
//开启指定启动smb服务
[root@yc1 ~]# systemctl enable smb


//在客户机查看samba服务器有哪些共享资源
[root@yc2 ~]# smbclient -L 192.168.23.140 -U share
Enter SAMBA\share's password:
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.12.3]

        Sharename       Type      Comment
        ---------       ----      -------
        print$          Disk      Printer Drivers
        yanchuang       Disk      yanchuang
        IPC$            IPC       IPC Service (Samba 4.12.3)
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.12.3]

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

        Workgroup            Master
        ---------            -------

//在客户机上进入共享目录创建新文件
[root@yc2 smb]# ls
[root@yc2 smb]# touch a
[root@yc2 smb]# mkdir abc
[root@yc2 smb]# ls
a  abc

//到服务机上验证
[root@yc1 ~]# cd /opt/yanchuang/
[root@yc1 yanchuang]# ls
a  abc

搭建匿名用户共享服务器

环境准备

IP 主机 系统
192.168.23.140 yc1 (服务端) CentOS8
192.168.23.141 yc2 (客户端) CentOS8
关闭防火墙与SElinux
[root@yc1 ~]# systemctl stop firewalld
[root@yc1 ~]# setenforce 0

服务端

[root@yc1 ~]# yum -y install samba-*
[root@yc1 ~]# systemctl start smb
[root@yc1 ~]# systemctl enable --now smb

客户端

[root@yc2 ~]# yum -y install samba-*
[root@yc2 ~]# systemctl start smb
[root@yc2 ~]# systemctl enable smb

添加全局配置

[root@yc1~]# vim /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
        map to guest = Bad User      //添加此行内容

创建共享文件夹

[root@yc1 ~]# mkdir -p /opt/yc
[root@yc1 ~]# chmod 777 /opt/yc/
[root@yc1 ~]# ll /opt/
total 0
drwxr-xr-x. 2 yanchuang yanchuang 15 Apr 6 22:05 yanchuang
drwxrwxrwx. 2 root root 6 Apr  5 22:27 yc

配置共享

[root@yc1 ~]# cat >> /etc/samba/smb.conf <<EOF
> [yc]
> comment = yc
> path = /opt/yc
> browseable = yes
> guest ok = yes
> writable = yes
> public = yes
> EOF

[root@yc1 ~]# tail -8 /etc/samba/smb.conf
[yc]
comment = yc
path = /opt/yc
browseable = yes
guest ok = yes
writable = yes
public = yes

[root@yc1 ~]# systemctl restart smb  //重启服务

客户端验证

[root@yc2 ~]# smbclient -L 192.168.23.140 -U 'Bad User'
Enter Bad User's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.12.3]
  	Sharename       Type      Comment
  	yc              Disk      yc
	IPC$            IPC       IPC Service (Samba Server Version 4.12.3)
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.12.3]
	Server               Comment
	Workgroup            Master

[root@yc2 ~]# mkdir -p /opt/smb
[root@yc2 ~]# mount -t cifs //192.168.23.140/yc /opt/smb/ -o username='Bad User'
[root@yc2 ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   4.9G  3.0G  2.0G  60% /
devtmpfs                985M     0  985M   0% /dev
tmpfs                   994M  140K  994M   1% /dev/shm
tmpfs                   994M  8.9M  986M   1% /run
tmpfs                   994M     0  994M   0% /sys/fs/cgroup
/dev/sda1               197M  104M   94M  53% /boot
/dev/sr0                3.8G  3.8G     0  100% /mnt
//192.168.23.140/yc     4.9G  3.0G  2.0G  61% /opt/smb

在客户端上创建共享目录,文件

[root@system1 ~]# cd /opt/smb/
[root@yc2 smb]# ls
[root@yc2 smb]# touch test
[root@yc2 smb]# mkdir yanchuang
[root@yc2 smb]# ls
test yanchuang

到服务机上验证

[root@yc1 ~]# cd /opt/yc/
[root@yc1 yc]# ls
test yanchuang
posted @ 2021-04-06 00:05  吻如雪上霜  阅读(185)  评论(0编辑  收藏  举报