cifs协议(基于samba服务)

一、基本概念:

        CIFS是实现文件共享服务的一种文件系统,主要用于实现windows系统中的文件共享,linux系统中用的比较少linux系统中利用CIFS文件系统实现文件共享,需要安装samba服务。它使程序可以访问远程Internet计算机上的文件并要求此计算机提供服务。CIFS 使用客户/服务器模式。客户程序请求远在服务器上的服务器程序为它提供服务。服务器获得请求并返回响应。CIFS是公共的或开放的SMB协议版本,并由Microsoft使用。SMB协议在局域网上用于服务器文件访问和打印的协议。像SMB协议一样,CIFS在高层运行,而不像TCP/IP协议那样运行在底层。CIFS可以看做是应用程序协议如文件传输协议和超文本传输协议的一个实现。

二、CIFS 可以使您达到以下功能:

(1)访问服务器本地文件并读写这些文件
(2)与其它用户一起共享一些文件块
(3)在断线时自动恢复与网络的连接
(4)使用统一码(Unicode)文件名:文件名可以使用任何字符集,而不局限于为英语或西欧语言设计的字符集。

三、关于cifs协议:
1.CIFS是一种协议,和具体的OS关系不大,linux在安装samba后可以使用CIFS

2.CIFS采用C/S模式,基本网络协议:TCP/IP和IPX/SPX;

两种资源访问模式:

(1)share level security:所有用户的共享资源访问口令是相同的,主要在win9x中使用
(2)user level securyt:Win NT以后的OS只提供ULS,用于必须提供正确的U/P,并且每个用户权限可以是不同的。

3.C/S的交互模式:类似于三次握手;三个交互:

(1)协议选择:双方选择合适的协议进行交互;
(2)身份验证:按选定的协议登录server,由server对client进行身份验证;
(3)资源获取:认证通过后,server和client进行交互,进行文件读写等操作。

4.cifs 通过安全认证机制,极大的提高了samba的共享,使不同的用户都必须通过认证才能访问共享目录
samba的实验环境可以点击此

四、实验实操

samba的实验环境可以参考上篇samba文件共享以及用法(访问控制)[点击进入]

1.通过实验验证samba共享确实是不安全的

客户端:

[root@client ~]# mount -o username=tom,password=123 //172.25.254.117/smb  /mnt   # 以tom用户的身份挂载共享目录
[root@client ~]# df

在这里插入图片描述

[root@client ~]# cd /mnt
[root@client mnt]# ls
anaconda-ks.cfg  bashrc  hello1
[root@client mnt]# touch hello{2..4}   #超级用户建立文件成功
[root@client mnt]# ls
anaconda-ks.cfg  bashrc  hello1  hello2  hello3  hello4
[root@client mnt]# su - student   #切换为普通用户student
[student@client ~]$ cd /mnt
[student@client mnt]$ ls          #普通用户可以查看
anaconda-ks.cfg  bashrc  hello1  hello2  hello3  hello4
[student@client mnt]$ touch hello5    #普通用户建立成功
[student@client mnt]$ ls
anaconda-ks.cfg  bashrc  hello1  hello2  hello3  hello4  hello5  

        通过实验确实发现,共享目录挂载在客户端之后,客户端上的所有用户都是可以访问的,可以对共享目录进行读写操作,这显然是不合理的,事实上只有通过认证的用户才可以对共享目录进行操作,那么怎样可以实现呢?

2.客户端的安全性设置(多用户挂载)

在服务端添加用户:

[root@shareserver ~]# useradd westos   #新建用户westos
[root@shareserver ~]# pdbedit -L       #查看smb中的所有用户
student:1000:Student User
tom:1001:
[root@shareserver ~]# smbpasswd -a westos   #把本机用户westos添加到sam服务器上
New SMB password:
Retype new SMB password:
Added user westos.
[root@shareserver ~]# pdbedit -L      #添加成功
student:1000:Student User
westos:1002:
tom:1001:
[root@shareserver ~]#

在这里插入图片描述
在客户端:

[root@client ~]# umount /mnt                 # 卸载
[root@client ~]# yum install -y cifs-utils   # 安装cifs-utils
[root@client ~]# rpm -qa | grep samba        # 查看到samba客户端软件是4.2版本的
samba-client-4.1.1-31.el7.x86_64
samba-common-4.1.1-31.el7.x86_64
samba-libs-4.1.1-31.el7.x86_64

在这里插入图片描述
在这里插入图片描述

[root@client ~]# vim /root/smbpass  # 这里写的用户必须已经存在于samba服务器中
添加:
username=westos      # 指定用户为westos
password=12345       # westos用户的smb密码

在这里插入图片描述

[root@client ~]# chmod 600 /root/smbpass   # 更改权限
[root@client ~]# ls -l /root/smbpass
-rw-------. 1 root root 31 Nov 28 04:32 /root/smbpass
[root@client ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.227/smb   /mnt  # 以/root/smbpass文件里的用户作为认证来挂载
[root@client ~]# chmod 600 /root/smbpass   # 更改权限
[root@client ~]# ls -l /root/smbpass
-rw-------. 1 root root 31 Nov 28 04:32 /root/smbpass
[root@client ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.227/smb   /mnt  # 以/root/smbpass文件里的用户作为认证来挂载
 注解:
credentials=/root/smbpass  # 用于指定包含挂载用户信息文件,此用户一般是samba服务器
里对共享目录具有较低权限的用户(本文使用的westos用户),文件包含username,password
sec=ntlmssp   # 指定认证方式(kernel3.8之后是默认选项)
multiuser     # 关键选项,用于指定使用多用户挂载
[root@client ~]# df

在这里插入图片描述

[root@client ~]# cd /mnt
[root@client mnt]# ls
hello1  hello2  hello3
[root@client mnt]# touch file
[root@client mnt]# ls
file  hello1  hello2  hello3
[root@client mnt]# su - student  # 切换到student用户
Last login: Wed Nov 28 03:56:53 EST 2018 on pts/0
[student@client ~]$ cd /mnt
[student@client mnt]$ ls         # 发现不能查看
ls: reading directory .: Permission denied

在这里插入图片描述

    [student@client ~]$ cifscreds add -u tom 172.25.254.227 #客户端普通用户student通过samba的tom用户登陆认证,
                                                            此处的认证用户任意,只要在samba服务的共享里即可
    Password: 
    [student@client ~]$ ls
    [student@client ~]$ cd /mnt
    [student@client mnt]$ ls    ## 认证成功之后,可以查看
    file  hello1  hello2  hello3

在这里插入图片描述

永久挂载

[root@client mnt]# vim /etc/fstab        # 永久挂载
//172.25.254.227/smb /mnt cifs  defaults,credentials=/root/smbpass,sec=ntlmssp,multiuser  0 0
[root@client mnt]# reboot

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43275140/article/details/84589885