cifs------网络文件系统(1)

SMB文件共享

SMB(Server Message Block)通信协议是微软(Microsoft)和英特尔(Intel)在1987年制定的协议,主要是作为Microsoft网络的通讯协议。SMB 是在会话层(session layer)和表示层(presentation layer)以及小部分应用层(application layer)的协议。

SMB使用了NetBIOS的应用程序接口 (Application Program Interface,简称API)。另外,它是一个开放性的协议,允许了协议扩展——使得它变得更大而且复杂;大约有65个最上层的作业,而每个作业都超过120个函数,甚至Windows NT也没有全部支持到,最近微软又把 SMB 改名为 CIFS(Common Internet File System),并且加入了许多新的特色。

SMB协议是基于TCP-NETBIOS下的,一般端口使用为139,445

运行环境:

主机:       ip 172.25.254.1    172.25.1.250        [[email protected] ~]#

虚拟机:    (服务器端): ip  172.25.1.1                  [[email protected] ~]#      

                    (客户端):  ip  172.25.1.2                 [[email protected] ~]#

Samba 服务的组成部分
Samba的支持文件:Samba-common
客户端应用程序:Samba-client
服务器应用程序:Samba
服务名称:smb nmb
主配置文件:/etc/samba/smb.conf

服务器端:

[[email protected] ~]# systemctl start smb           //开启smb服务
[[email protected] ~]# systemctl enable smb       //设置smb为开机自启
[[email protected] ~]# systemctl stop firewalld     //关闭防火墙
[[email protected] ~]# systemctl disable firewalld       //设置防火墙为开机不自启

[[email protected] ~]# netstat -antlp | grep smb          //查看smb的服务端口,进行共享所用的端口是 139 和 445

cifs------网络文件系统(1)

samba用户及密码的设置
// 因这里没有Samba密码服务器,所以必须在本地计算机上创建身份验证数据。

使用smbpasswd创建samba账户和密码
//smb用户必须是本地已经存在的用户

添加 smb 用户

smb 用户必须是本地用户
添加用户 student,westos

  1. smbpasswd 常用参数
    -a username: 添加用户为 samba 用户
    -d username:禁用 samba 用户 username
    -e username: 启用 samba 用户 username
    -x username: 删除 samba 用户 username

  2. pdbedit 常用参数
    –a username:新建 Samba 账户。
    –x username:删除 Samba 账户。
    –L:列出 Samba 用户列表,读取 passdb.tdb 数据库文件。
    –Lv:列出 Samba 用户列表的详细信息。
    –c “[D]” –u username:暂停该 Samba 用户的账号。
    –c “[]” –u username:恢复该 Samba 用户的账号。

[[email protected] ~]# useradd westos
[[email protected] ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)

[[email protected] ~]# id student
uid=1000(student) gid=1000(student) groups=1000(student)

[[email protected] ~]# smbpasswd -a student         //给samba添加用户student,并设置密码,为方便记忆,我在这里设置密码为redhat

[[email protected] ~]# smbpasswd -a westos          //同样,添加用户westos并设置密码,这里设置为redhat

[[email protected] ~]# pdbedit -L            //查看samba用户有哪些

cifs------网络文件系统(1)

[[email protected] ~]# pdbedit -x westos          //删除samba用户
[[email protected] ~]# pdbedit -L        //再次查看时只有一个用户
student:1000:
[[email protected] ~]# smbpasswd -a westos           再次添加,添加成功

cifs------网络文件系统(1)

客户端:

[[email protected] ~]# yum install -y samba-client         //下载所需服务

[[email protected] ~]# smbclient -L //172.25.1.1           //列出服务器端samba服务的文件系统 ,,,这里输密码时直接回车就好,因为这里没有具体指定用户

cifs------网络文件系统(1)

[[email protected] ~]# smbclient  //172.25.1.1/student -U student            //指定用户student登陆文件系统

cifs------网络文件系统(1)

//此时用ls命令访问的话可能出现被拒的现象,可能原因为: selinux 拒绝访问

smbclient(选项)(参数)
                       -L:显示服务器端所分享出来的所有资源
                       -U <用户名称>:指定用户名称

服务器端:

[[email protected] ~]# getsebool -a | grep samba            //查看布尔值允许本地目录作为 cifs 文件共享的相关主目录

cifs------网络文件系统(1)

其中:

samba_enable_home_dirs               //布尔值允许本地Linux主目录作为CIFS文件共享导出至其他系统
use_samba_ home_dirs                   //布尔值允许挂载远程CIFS文件共享并将其用作本地Linux主目录
samba_share_t                                  //smb共目录的selinux安全上下文
samba_export_all _ro 和 samba_export_all_rw           //共享系统目录时要打开

[[email protected] ~]# setsebool -P samba_enable_home_dirs on
此时,客户端访问就不会再被拒了

客户端:

[[email protected] ~]# smbclient //172.25.1.1/student -U student             //ls可以正常访问

cifs------网络文件系统(1)

smb挂载

1、文件的上传:

只能上传当前所在目录下的文件,上传的文件在服务端的 student 用户的家目录

客户端:

首先找到客户端所需上传的文件所在目录,并切换到相应目录,再进行登陆;例:上传目录为/etc,上传文件为 /etc/passwd

[[email protected] ~]# cd /etc

[[email protected] etc]# smbclient //172.25.1.1/student -U student         //输入之前设置的密码

smb: \> !ls               //   !ls  即可列出本地文件及目录

                    ......    //前面太多故省略

hosts.allow         profile.d           yum.conf
hosts.deny         protocols           yum.repos.d
httpd             python
idmapd.conf         qemu-ga

smb: \> put passwd         //上传文件passwd成功

cifs------网络文件系统(1)

服务器端:

在服务器端查看,客户端已将文件上传至服务器端 的用户家目录内

[[email protected] ~]# cd /home/student/

[[email protected] student]# ls

cifs------网络文件系统(1)

//////////////////////////这种登陆方式登陆,不能执行新建文件、目录等操作///////////////////////////////

2、临时挂载(手动挂载)

客户端:

[[email protected] etc]# mount //172.25.1.1/student /mnt/ -o username=student,password=redhat         //挂载成功

cifs------网络文件系统(1)

这样服务器端的用户家目录就挂载到了客户端的/mnt下面去

cifs------网络文件系统(1)

cifs------网络文件系统(1)

客户端执行

[[email protected] mnt]# touch file{1..5}                  //这种方式可以新建/删除目录、文件等
[[email protected] mnt]# ls
file1  file2  file3  file4  file5  passwd

cifs------网络文件系统(1)

[[email protected] mnt]# cd ..
[[email protected] /]# umount /mnt/          //取消挂载

此时,服务器端:

cifs------网络文件系统(1)

3、永久挂载(只需挂在一次)

方式一:如果服务没开,会影响系统启动,易损坏系统

客户端

[[email protected] /]# vim /etc/fstab

//172.25.1.1/student /mnt cifs defaults,username=student,password=redhat 0 0

cifs------网络文件系统(1)

[[email protected] /]# mount -a               //df查看是否挂载成功

cifs------网络文件系统(1)

方式二:在所有服务启动后,初始化的时候开始执行,因此不会影响系统启动(推荐使用)

前提是将第一种方法注释或者删除 :  

         1.    编辑/etc/fstab         //删掉刚加上的行

         2.   [[email protected] /]# umount /mnt/        //取消挂载

        3.    [[email protected] /]# df          //查看是否还挂载,确保不再挂载

客户端

[[email protected] /]# vim /etc/rc.d/rc.local

mount //172.25.2.1/student /mnt -o username=student,password=redhat

cifs------网络文件系统(1)

[[email protected] /]# chmod +x /etc/rc.d/rc.local           //给予执行权限

[[email protected] /]# reboot

[[email protected] /]# df             //查看,此时挂载成功

//172.25.1.1/student   17811456 1229488  16581968   7% /mnt

cifs------网络文件系统(1)