samba

1. 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 表示设置是否为打印机

搭建用户认证共享:

//安装samba服务
[root@mf2 ~]# yum -y install samba samba-client

//创建用户tom,为tom用户创建smb共享密码:
[root@mf2 ~]# useradd tom
[root@mf2 ~]# ls /home
tom
[root@mf2 ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom.

//关闭防火墙
[root@mf2 ~]# systemctl stop firewalld

//假设这里映射wangqing用户为share用户,那么就要在/etc/samba/smbusers文件中添加如下内容:
[root@mf2 ~]# echo 'tom = share' > /etc/samba/smbusers

//在全局配置中添加如下内容:
[root@mf2 ~]# 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 //添加这一行

//创建一个共享目录tom
[root@mf2 ~]# mkdir /opt/tom
[root@mf2 ~]# chown -R tom.tom /opt/tom/
[root@mf2 ~]# ll /opt/
total 0
drwxr-xr-x. 2 tom  tom  6 Apr  5 20:41 tom

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

//重新加载smb服务,设置开机自动启动
[root@mf2 ~]# systemctl restart smb
[root@mf2 ~]# systemctl reload smb
[root@mf2 ~]# systemctl enable smb
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.

//在客户机查看samba服务器有哪些共享资源
[root@mf1 ~]# smbclient -L 192.168.186.131 -U tom
Enter SAMBA	om's password: 

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    tom             Disk      tom
    IPC$            IPC       IPC Service (Samba 4.12.3)
SMB1 disabled -- no workgroup available
[root@mf1 ~]# smbclient //192.168.186.131/tom -U tom
Enter SAMBA	om's password: 
Try "help" to get a list of possible commands.
smb: > ls
  .                                   D        0  Mon Apr  5 20:41:46 2021
  ..                                  D        0  Mon Apr  5 20:41:46 2021

        17811456 blocks of size 1024. 16058740 blocks available
smb: > 

//将samba服务器的共享资源tom挂载到客户机本地
[root@mf2 ~]# mount -t cifs //192.168.186.130/mufeng /opt/smb/ -o username=share,password=123
[root@mf2 ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  889M     0  889M   0% /dev
tmpfs                     904M     0  904M   0% /dev/shm
tmpfs                     904M  8.8M  895M   1% /run
tmpfs                     904M     0  904M   0% /sys/fs/cgroup
/dev/mapper/rhel-root      17G  1.7G   16G  10% /
/dev/sda1                1014M  163M  852M  17% /boot
tmpfs                     181M     0  181M   0% /run/user/0
//192.168.186.130/mufeng   17G  2.5G   15G  15% /opt/smb

//在客户机上进入共享目录创建新文件
[root@mf2 ~]# cd /opt/smb/
[root@mf2 smb]# ls
[root@mf2 smb]# touch mfa
[root@mf2 smb]# mkdir mfb
[root@mf2 smb]# ls
mfa  mfb

//到服务器上验证
[root@mf1 ~]# cd /opt/mufeng/
[root@mf1 mufeng]# ls
mfa  mfb

搭建用户认证共享服务器:

//关闭防火墙
[root@mf1 ~]# systemctl stop firewalld

//使用yum命令安装samba服务器:
[root@mf1 ~]# yum -y install samba-*

//在全局配置中添加如下内容:
[root@mf1 ~]# 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      //添加此行内容
 
//创建一个共享目录mufeng
[root@mf1 ~]# mkdir /opt/mufeng
[root@mf1 ~]# chmod 777 /opt/mufeng/
[root@mf1 ~]# ll /opt/
total 0
drwxrwxrwx. 2 root root    6 Apr  5 23:52 mufeng
       
//配置共享:
//在配置文件最后面加上配置中添加如下内容:
[root@mf1 ~]# vim /etc/samba/smb.conf
[mufeng]
comment = mufeng
path = /opt/mufeng
browseable = yes
guest ok = yes
writable = yes
public = yes


//启动smb服务:
[root@mf1 ~]# systemctl start smb

//关闭客户机防火墙
[root@mf2 ~]# systemctl stop firewalld

//在客户机安装samba-client包
[root@mf2 ~]# yum -y install samba-client

//在客户机查看samba服务器有哪些共享资源
[root@mf2 ~]# smbclient -L 192.168.186.130 -U 'Bad User'
Enter SAMBA
oot's password:        //这里直接敲回车键即可,不用输入密码
Enter SAMBABad User's password: 

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    mufeng           Disk      mufeng
    IPC$            IPC       IPC Service (Samba 4.11.2)
SMB1 disabled -- no workgroup available

//将samba服务器的共享资源mufeng挂载到客户机本地
[root@mf2 ~]# mount -t cifs //192.168.186.130/mufeng /opt/smb/ -o username='Bad User'
[root@mf2 ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  889M     0  889M   0% /dev
tmpfs                     904M     0  904M   0% /dev/shm
tmpfs                     904M  8.8M  895M   1% /run
tmpfs                     904M     0  904M   0% /sys/fs/cgroup
/dev/mapper/rhel-root      17G  1.7G   16G  10% /
/dev/sda1                1014M  163M  852M  17% /boot
tmpfs                     181M     0  181M   0% /run/user/0
//192.168.186.130/mufeng   17G  2.5G   15G  15% /opt/smb

//在客户机上进入共享目录创建新文件
[root@mf2 ~]# cd /opt/smb/
[root@mf2 smb]# ls
[root@mf2 smb]# touch 123
[root@mf2 smb]# touch abc
[root@mf2 smb]# ls
123  abc

//到服务器上验证
[root@mf1 ~]# cd /opt/smb/
[root@mf1 smb]# ls
123  abc
原文地址:https://www.cnblogs.com/mfdsg/p/14620005.html