samba

1. samba简介

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

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

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

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

Samba监听端口有:

TCPUDP
139137
445138
  tcp端口相对应的服务是smbd服务,其作用是提供对服务器中文件、打印资源的共享访问。
udp端口相对应的服务是nmbd服务,其作用是提供基于NetBIOS主机名称的解析。

samba进程:

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

samba用户:

帐号密码
都是系统用户/etc/passwdSamba服务自有密码文件通过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

2. samba访问

环境说明:

服务器IP客户机IP
192.168.19.128192.168.19.129
//交互式数据访问
smbclient -L HOST -U USERNAME
smbclient //SERVER/shared_name -U USERNAME

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

//查看samba服务器有哪些共享资源
[root@localhost ~]# smbclient -L 192.168.19.128 -U tom
Enter SAMBA\tom's password:
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2]

        Sharename       Type      Comment
        ---------       ----      -------
        print$          Disk      Printer Drivers
        IPC$            IPC       IPC Service (Samba 4.6.2)
        tom             Disk      Home Directories
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2]

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

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

//交互式访问某共享资源
[root@localhost ~]# smbclient //192.168.19.128/tom -U tom
Enter SAMBA\tom's password:
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> ls
  .                                   D        0  Sat Aug  4 13:52:14 2018
  ..                                  D        0  Sat Aug  4 12:59:42 2018
  .bash_logout                        H       18  Wed Mar  8 00:13:45 2017
  .bash_profile                       H      193  Wed Mar  8 00:13:45 2017
  .bashrc                             H      231  Wed Mar  8 00:13:45 2017
  aa                                  N        0  Sat Aug  4 13:52:14 2018

                17811456 blocks of size 1024. 16665456 blocks available
smb: \> quit    //quit退出


//基于挂载的方式访问
mount -t cifs //SERVER/shared_name /挂载到本地的什么目录 -o username=USERNAME,password=PASSWORD

[root@localhost ~]# mkdir /opt/smb
[root@localhost ~]# mount -t cifs //192.168.19.128/tom /opt/smb/ -o username=tom,password=redhat
[root@localhost ~]# df -h
文件系统               容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root   17G  1.1G   16G    6% /
devtmpfs               478M     0  478M    0% /dev
tmpfs                  489M     0  489M    0% /dev/shm
tmpfs                  489M  6.8M  482M    2% /run
tmpfs                  489M     0  489M    0% /sys/fs/cgroup
/dev/sda1             1014M  125M  890M   13% /boot
tmpfs                   98M     0   98M    0% /run/user/0
/dev/sr0               3.8G  3.8G     0  100% /mnt
//172.16.12.128/tom     17G  1.1G   16G    7% /opt/smb

实验

要求:

1.不论是匿名用户还是用户认证共享,均要在客户机验证结果

2.用户认证共享需要映射系统用户为一个虚拟用户

1.搭建匿名用户共享服务器

使用yum命令安装samba服务器:

[root@yh ~]# yum -y install samba-*
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务

在全局配置中添加如下内容:

[root@localhost ~]# 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      //添加此行内容

创建一个共享目录yh1

[root@yh ~]# mkdir /opt/yh1
[root@yh ~]# chmod 777 /opt/yh1/
[root@yh ~]# ll /opt/
总用量 0
drwxr-xr-x. 9 root root 254 314 10:56 myrepo
drwxr-xr-x. 6 root root  54 314 11:19 nginx-1.12.2
drwxr-xr-x. 3 yh   yh    26 416 12:25 yh
drwxrwxrwx. 2 root root   6 416 14:31 yh1

配置共享

[root@yh ~]# cat >> /etc/samba/smb.conf <<EOF
> [yh1]
> comment = yh1
> path = /opt/yh1
> browseable = yes
> guest ok = yes
> writable = yes
> public = yes
> EOF
[root@yh ~]# tail -7 /etc/samba/smb.conf
[yh1]
comment = yh1
path = /opt/yh1
browseable = yes
guest ok = yes
writable = yes

启动smb服务

[root@yh ~]# systemctl start smb

在客户机查看samba服务器有哪些共享资源

[root@yh1 ~]# smbclient -L 192.168.19.128 -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
	yh              Disk      yh
	IPC$            IPC       IPC Service (Samba 4.6.2)
	yh1             Disk      yh1
OS=[Windows 6.1] Server=[Samba 4.6.2]

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

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


创建/opt/smb
将samba服务器的共享资源yh1挂载到客户机本地

[root@yh1 ~]# mount -t cifs //192.168.19.128/yh1 /opt/smb/ -o username='Bad User'
[root@yh1 ~]# df -Th
文件系统              类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root xfs        17G  5.0G   13G   30% /
devtmpfs              devtmpfs  982M     0  982M    0% /dev
tmpfs                 tmpfs     993M     0  993M    0% /dev/shm
tmpfs                 tmpfs     993M  8.7M  984M    1% /run
tmpfs                 tmpfs     993M     0  993M    0% /sys/fs/cgroup
/dev/sda1             xfs      1014M  125M  890M   13% /boot
tmpfs                 tmpfs     199M     0  199M    0% /run/user/0
/dev/sr0              iso9660   3.8G  3.8G     0  100% /mnt
//192.168.19.128/yh   cifs       17G  5.7G   12G   34% /opt/smb
//192.168.19.128/yh1  cifs       17G  5.7G   12G   34% /opt/smb


在客户机上进入共享目录创建新文件

[root@yh1 ~]# cd /opt/smb/
[root@yh1 smb]# ls
[root@yh1 smb]# ls
[root@yh1 smb]# touch 1123
[root@yh1 smb]# mkdir 321
[root@yh1 smb]# ls
1123  321
[root@yh1 smb]# 

在服务器端上验证

[root@yh ~]# cd /opt/yh1/
[root@yh yh1]# ls
1123  321
[root@yh yh1]# 

2.搭建用户认证共享服务器

安装samba服务器

[root@yh ~]# yum -y install samba-*
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务

映射共享目录:

创建用户yh:

[root@yh ~]# useradd -M yh

为yh用户创建smb共享密码:

[root@yh ~]# smbpasswd -a yh
New SMB password:
Retype new SMB password:
Added user yh.

假设这里映射yh用户为share用户,那么就要在/etc/samba/smbusers文件中添加如下内容:

[root@yh ~]# echo 'yh = share' > /etc/samba/smbusers

在全局配置中添加如下内容:

[root@yh ~]# 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      //添加此行内容

创建一个共享目录yh

[root@yh ~]# mkdir /opt/yh
[root@yh ~]# chown -R yh.yh /opt/yh/
[root@yh ~]# ll /opt/
总用量 0
drwxr-xr-x. 9 root root 254 314 10:56 myrepo
drwxr-xr-x. 6 root root  54 314 11:19 nginx-1.12.2
drwxr-xr-x. 2 yh   yh     6 416 11:57 yh

配置共享

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

启动smb服务:

[root@yh ~]# systemctl start smb

重启smb服务:

[root@yh ~]# systemctl restart smb

重新加载smb服务:

root@yh ~]# systemctl reload smb

设置smb服务随系统启动而启动:

[root@yh ~]# systemctl enable smb

在客户机安装samba-client包

[root@yh1 ~]# yum -y install samba-client
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务

在客户机查看samba服务器有哪些共享资源

[root@yh1 ~]# smbclient -L 192.168.19.128 -U share
Enter SAMBA\share's password: 
Domain=[YH] OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	yh              Disk      yh
	IPC$            IPC       IPC Service (Samba 4.6.2)
Domain=[YH] OS=[Windows 6.1] Server=[Samba 4.6.2]

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

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

创建/opt/smb/
将samba服务器的共享资源yh挂载到客户机本地

[root@yh1 ~]# mkdir /opt/smb
[root@yh1 ~]# mount -t cifs //192.168.19.128/yh /opt/smb/ -o username=share,password=yh
[root@yh1 ~]# df -Th
文件系统              类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root xfs        17G  5.0G   13G   30% /
devtmpfs              devtmpfs  982M     0  982M    0% /dev
tmpfs                 tmpfs     993M     0  993M    0% /dev/shm
tmpfs                 tmpfs     993M  8.7M  984M    1% /run
tmpfs                 tmpfs     993M     0  993M    0% /sys/fs/cgroup
/dev/sda1             xfs      1014M  125M  890M   13% /boot
tmpfs                 tmpfs     199M     0  199M    0% /run/user/0
/dev/sr0              iso9660   3.8G  3.8G     0  100% /mnt
//192.168.19.128/yh   cifs       17G  5.7G   12G   34% /opt/smb

在客户机上进入共享目录创建新文件

[root@yh1 ~]# cd /opt/smb/
[root@yh1 smb]# ls
[root@yh1 smb]# touch a
[root@yh1 smb]# ls
a
[root@yh1 smb]# mkdir abc
[root@yh1 smb]# ls
a  abc

在服务端上验证

[root@yh ~]# cd /opt/yh/
[root@yh yh]# ls
a  abc

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值