Samba

Samba

1.Samba简介

Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置"NetBIOS over TCP/IP"使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

。Samba监听的端口

TCPUDP
139137
445138

。Samba的进程

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

。Samba的用户

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

。Samba的主要功能

1.共享Linux磁盘给Win95/NT

2.共享Win95/NT磁盘给Linux机器

3.共享Linux打印机给win95/NT

4.共享win95/NT打印机给Linux机器。

同时它的文件服务功能比NT系统还高,而且在Windows2000之前就提供了用户磁盘空间限制的功能。

。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@100 ~]# 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$]"
Processing section "[share]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

2.部署samba

1.Samba的安装
[root@100 ~]# yum -y install samba-*

2.创建共享目录

[root@100 ~]# mkdir /samba
[root@100 ~]# touch /samba/aa
[root@100 ~]# ls /samba/
aa

3.编辑配置文件

[root@100 ~]# vim /etc/samba/smb.conf
添加以下内容:
[share]              //共享的名称
        path = /samba           //共享的目录
        browseable = no         //不让对方看到
        valid users = tom       //有效的用户

[root@100 ~]# systemctl restart smb nmb            //重启服务
[root@100 ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      50      *:139                 *:*          //端口号已启动
LISTEN     0      128     *:111                 *:*                  
LISTEN     0      128     *:20048               *:*                  
LISTEN     0      64      *:35890               *:*                  
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      50      *:445                 *:*           //端口号已启动
LISTEN     0      64      *:2049                *:*                  
LISTEN     0      128     *:44643               *:*                  
LISTEN     0      50     :::139                :::*            
// 关闭防火墙和selinux
[root@100 ~]# systemctl stop firewalld
[root@100 ~]# setenforce 0

//给Tom用户设置Samba服务的密码
[root@100 ~]# id tom
uid=1011(tom) gid=1011(tom) 组=1011(tom)
[root@100 ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom.

4测试配置文件是否有语法错误
[root@100 ~]# 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$]"
Processing section "[share]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

3.Samba访问

。使用Windows访问:

1.输入ip访问:
在这里插入图片描述
2.输入用户名密码
在这里插入图片描述
效果如下:
在这里插入图片描述

3.在Tom里面新建一个hello的文件
在这里插入图片描述

4.去Tom的家目录查看此文件是否存在

[root@100 ~]# ls /home/tom/
hello.txt
[root@100 ~]# 

// 由于我们刚才在配置文件中设置的是不让对方看到共享目录share,如果想让对方看到此处编辑配置文件

[root@100 ~]# vim /etc/samba/smb.conf
[share]
        path = /samba
        browseable = yes          //将此处的no改为yes,或者不配置此项因为默认是可见的
        valid users = tom
[root@100 ~]# systemctl restart smb nmb   //重启一下服务

// 再次访问就可以看到共享的目录share
在这里插入图片描述
// 如果不想看到共享的目录还可以在配置文件中共享的名字后面加上$

[root@100 ~]# vim /etc/samba/smb.conf
[share$]
        path = /samba
        browseable = yes
        valid users = tom
// 再次访问也看不到共享的目录share了

。使用linux客户端访问

//在客户机安装samba-client包
[root@96 ~]# yum -y install samba-client
//交互式数据访问

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

// 查看服务端的共享资源
[root@96 ~]#  smbclient -L 192.168.100.100 -U tom
Enter SAMBA\tom's password: 
      (共享名)   (共享的类型)(说明信息)
	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	share$          Disk      
	IPC$            IPC       IPC Service (Samba 4.6.2)
	tom             Disk      Home Directories
Reconnecting with SMB1 for workgroup listing.

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

	Workgroup            Master
	---------            -------
	SAMBA                100

[root@96 ~]# smbclient //192.168.100.100/share -U tom
Enter SAMBA\tom's password: 
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Mon Jul  1 23:20:23 2019
  ..                                 DR        0  Mon Jul  1 23:15:57 2019
  aa                                  N        0  Mon Jul  1 23:20:23 2019           //之前创建的aa文件

		17811456 blocks of size 1024. 15737164 blocks available
smb: \> 
//  由于是交互模式访问所以很多命令不能使用所以一般不推荐使用此方式,使用挂载访问更加方便。

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

[root@96 ~]# mount -t cifs  -o username=tom,password=1 //192.168.100.100/share /media
[root@96 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.2G   16G    7% /
devtmpfs                 898M     0  898M    0% /dev
tmpfs                    910M     0  910M    0% /dev/shm
tmpfs                    910M  9.6M  901M    2% /run
tmpfs                    910M     0  910M    0% /sys/fs/cgroup
/dev/sda1               1014M  146M  869M   15% /boot
tmpfs                    182M     0  182M    0% /run/user/0
/dev/sr0                 4.3G  4.3G     0  100% /mnt
//192.168.100.100/share   17G  2.0G   16G   12% /media                 //以挂载成功


[root@96 ~]# cd /media/
[root@96 media]# ls
aa
[root@96 media]# touch bb            //现在还没有写的权限
touch: 无法创建"bb": 权限不够

// 去到服务端修改配置问价给tom写的权限
[root@100 ~]# vim /etc/samba/smb.conf
[share]
        path = /samba
        browseable = yes
        valid users = tom
        write list = tom             // 添加此行
[root@100 samba]# setfacl -m tom:rwx .         //并且将此目录给Tom用户写权限
[root@100 ~]# systemctl restart smb nmb      // 重启服务

// 再次创建文件或修改文件都没问题了
[root@96 media]# touch bb
[root@96 media]# ls
aa  bb

// 设置开机自动挂载

[root@96 ~]# umount /media/     //先卸载之前挂载的
[root@96 ~]# vim /etc/fstab 
// 添加以下内容:
//192.168.100.100/share /media cifs defaults,username=tom,password=1 0 0
[root@96 ~]# mount -a
[root@96 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.2G   16G    7% /
devtmpfs                 898M     0  898M    0% /dev
tmpfs                    910M     0  910M    0% /dev/shm
tmpfs                    910M  9.6M  901M    2% /run
tmpfs                    910M     0  910M    0% /sys/fs/cgroup
/dev/sda1               1014M  146M  869M   15% /boot
tmpfs                    182M     0  182M    0% /run/user/0
/dev/sr0                 4.3G  4.3G     0  100% /mnt
//192.168.100.100/share   17G  2.0G   16G   12% /media           //已经挂载成功

4.示例

。配置用户认证共享:

//关闭防火墙和selinux
[root@100 ~]# systemctl stop firewalld
[root@100 ~]# vim /etc/selinux/config 
SELINUX=enforcing          //将此行改为SELINUX=ecforcing
[root@100 ~]# systemctl disable firewalld
[root@100 ~]# setenforce 0

//Samba安装
[root@100 ~]# yum -y install samba-*

//映射共享目录:
//创建用户sss:
[root@100 ~]# useradd -M sss

//为sss用户创建smb共享密码:
[root@100 ~]# smbpasswd -a sss
New SMB password:
Retype new SMB password:
Added user sss.
[root@100 ~]# 

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

//在全局配置中添加如下内容:
[global]
        workgroup = SAMBA
        security = user
        username map = /etc/samba/smbusers      //添加此行内容(路径必须是上一步写的文件的路径)

//创建一个共享目录dir
[root@100 ~]# mkdir /opt/dir
[root@100 ~]# chown -R sss.sss /opt/dir/
[root@100 ~]# ll /opt/dir/ -d
drwxr-xr-x. 2 sss sss 6 7月   2 21:58 /opt/dir/

//配置共享
[root@100 ~]# vim /etc/samba/smb.conf
添加内容:
[share]
        path = /opt/dir
        comment = sss
        browseable = yes
        guest ok = yes
        writable = yes
        write list = share
        public = yes

//启动smb服务:
[root@localhost ~]# systemctl start smb
 
//重启smb服务:
[root@localhost ~]# systemctl restart smb
 
//重新加载smb服务:
[root@localhost ~]# systemctl reload smb
 
//设置smb服务随系统启动而启动:
[root@localhost ~]# systemctl enable smb





//在客户机查看samba服务器有哪些共享资源
[root@96 media]# smbclient -L 192.168.100.100 -U share
Enter SAMBA\share's password: 

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	share           Disk      sss
	IPC$            IPC       IPC Service (Samba 4.6.2)
	sss             Disk      Home Directories
Reconnecting with SMB1 for workgroup listing.

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

	Workgroup            Master
	---------            -------
	SAMBA                100

//客户机关闭防火墙和selinux
[root@96 ~]# systemctl stop firewalld
[root@96 ~]# vim /etc/selinux/config 
SELINUX=enforcing          //将此行改为SELINUX=ecforcing
[root@96 ~]# systemctl disable firewalld
[root@96 ~]# setenforce 0

  //客户机临时挂载
[root@96 ~]# mount -t cifs //192.168.100.100/share /media -o username=share,password=1
//客户机永久挂载
[root@96 ~]# vim /etc/fstab 
//192.168.100.100/share /media cifs defaults,username=share,password=1 0 0
[root@96 ~]# mount -a
[root@96 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.2G   16G    7% /
devtmpfs                 898M     0  898M    0% /dev
tmpfs                    910M     0  910M    0% /dev/shm
tmpfs                    910M  9.6M  901M    2% /run
tmpfs                    910M     0  910M    0% /sys/fs/cgroup
/dev/sda1               1014M  146M  869M   15% /boot
/dev/sr0                 4.3G  4.3G     0  100% /mnt
tmpfs                    182M     0  182M    0% /run/user/0
//192.168.100.100/share   17G  2.0G   16G   12% /media

验证:
//客户机进入共享目录验证,(提示没有权限去服务端设置共享目录权限777)
[root@96 ~]# cd /media/
[root@96 media]# ls
aa  aba  bb
[root@96 media]# rm -rf *
[root@96 media]# touch haha
[root@96 media]# ls
haha

//服务端验证
[root@96 ~]# cd /opt/dir
[root@96 dir]# ls
haha
[root@96 dir]# 

。配置匿名共享:

//关闭防火墙和selinux
[root@100 ~]# systemctl stop firewalld
[root@100 ~]# vim /etc/selinux/config 
SELINUX=enforcing          //将此行改为SELINUX=ecforcing
[root@100 ~]# systemctl disable firewalld
[root@100 ~]# setenforce 0

//Samba安装
[root@100 ~]# yum -y install samba-*

//在全局配置中添加如下内容:
[global]
        workgroup = SAMBA
        security = user
        map to guest = Bad User       //添加此行内容
        passdb backend = tdbsam

//创建共享目录
[root@100 ~]# mkdir /abc
[root@100 ~]# chmod 777 /abc/

//配置共享
[root@100 ~]# vim /etc/samba/smb.conf
添加内容
[share]
        path = /opt/dir
        browseable = yes
        guest ok = yes
        writable = yes
        public = yes

//启动Samba服务
[root@100 ~]# systemctl start smb nmb




//客户机关闭防火墙和selinux
[root@96 ~]# systemctl stop firewalld
[root@96 ~]# vim /etc/selinux/config 
SELINUX=enforcing          //将此行改为SELINUX=ecforcing
[root@96 ~]# systemctl disable firewalld
[root@96 ~]# setenforce 0

//客户端上挂载
[root@96 media]# mkdir /abc    //创建挂载点
[root@96 ~]# mount -t cifs //192.168.100.100/share /abc -o username='Bad User'    //临时挂载
[root@96 ~]# vim /etc/fstab     //永久挂载
//192.168.100.100/share /abc cifs defaults,username=BadUser 0 0
[root@96 ~]# mount -a
[root@96 abc]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.2G   16G    7% /
devtmpfs                 898M     0  898M    0% /dev
tmpfs                    910M     0  910M    0% /dev/shm
tmpfs                    910M  9.6M  901M    2% /run
tmpfs                    910M     0  910M    0% /sys/fs/cgroup
/dev/sda1               1014M  146M  869M   15% /boot
/dev/sr0                 4.3G  4.3G     0  100% /mnt
tmpfs                    182M     0  182M    0% /run/user/0
//192.168.100.100/share   17G  2.0G   16G   12% /abc

//客户端进入共享目录进行验证:
[root@96 media]# cd /abc/
[root@96 abc]# ls
[root@96 abc]# touch a
[root@96 abc]# mkdir b
[root@96 abc]# ll
总用量 0
-rwxr-xr-x. 1 root root 0 7月   2 23:28 a
drwxr-xr-x. 2 root root 0 7月   2 23:28 b

//服务端进行验证:
[root@100 ~]# cd /abc/
[root@100 abc]# ll
总用量 0
-rwxr--r--. 1 nobody nobody 0 7月   2 23:28 a           //已经实现了匿名
drwxr-xr-x. 2 nobody nobody 6 7月   2 23:28 b

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值