关于cifs协议和samba的应用

cifs协议(共享文件)

通用Internet文件系统(CIFS),也称为服务器消息块(SMB),是一种网络协议,其最常见的用途是共享局域网(LAN)的文件。该协议允许客户端对文件进行操作,就好像他们在
本地计算机上。如读,写,创建,删除,重命名操作都支持,唯一的区别是,该文件不是在本地计算机上,实际上是在远程服务器上
CIFS 采用C/S模式,基本网络协议:TCP/IP和IPX/SPX

C/S 的交互模式:类似与三次握手;三个交互:
(1)协议选择:双方选择合适的协议进行交互;
(2)身份验证:按选定的协议登录server,由server对client进行身份验证;
(3)资源获取:认证通过后,server和client进行交互,进行文件读写等操作。

samba的作用

samba是一款软件 ,主要功能是提供cifs协议

1.samba的安装与启用

在服务端

server(ip:102)
yum  install  samba  samba-common  samba-client  -y
systemctl  start  smb
systemctl  stop  firewalld

2.samba的基本信息

端口:
[root@cifs-server ~]# ss  -antlupe |  grep  smb
tcp    LISTEN     0      50                     *:445                   *:*      users:(("smbd",6358,33)) ino:123952 sk:ffff880034214f00 <->
tcp    LISTEN     0      50                     *:139                   *:*      users:(("smbd",6358,34)) ino:123953 sk:ffff880034215680 <->
tcp    LISTEN     0      50                    :::445                  :::*      users:(("smbd",6358,31)) ino:123950 sk:ffff880033be6800 <->
tcp    LISTEN     0      50                    :::139                  :::*      users:(("smbd",6358,32)) ino:123951 sk:ffff880033be7000 <->

3.samba的基本配置信息

主配置文件:
/etc/samba/smb.conf 
workgroup = MXW      ##工作组的设定
server  string = HHHHHHHH    ##全局共享信息
hosts allow =  172.25.254.202  ##客户端的白名单
hosts deny =  172.25.254.202   ##客户端的黑名单

测试
在客户端:

desktop(ip:202)
yum install  samba-client  -y
在白名单中时
smbclient  -L  //172.25.254.102
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (HHHHHHHH)
Anonymous login successful   ##匿名用户访问
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]

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

    Workgroup            Master
    ---------            -------
在黑名单中时
[root@localhost ~]# smbclient  -L  //172.25.254.102
Enter root's password: 
Connection to 172.25.254.102 failed (Error NT_STATUS_CONNECTION_REFUSED)

4.本地用户的建立及访问

本地用户建立

在服务端

samba用户必须是本地用户

useradd  westos
useradd  mxw
[root@cifs-server ~]  smbpasswd   -a  mxw   ##添加samba用户
New SMB password:
Retype new SMB password:
Added user mxw.
[root@cifs-server ~]  pdbedit -L     ##列出samba用户
westos:1001:
mxw:1002:
pdbedit -x   westos    ##删除用户
vim  /etc/samba/smb.conf 
20#Security-Enhanced Linux (SELinux) Notes:
25#setsebool -P samba_enable_home_dirs on
setsebool -P samba_enable_home_dirs on  ##解除SELINUX连接的限制()

测试

在客户端

smbclient  -L  //172.25.254.102  -U mxw  ##用mxw用户身份连接到服务器端
Enter mxw's password:                  ##不输密码是匿名访问
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (HHHHHHHH)
    mxw             Disk      Home Directories
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]

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

    Workgroup            Master
    ---------            -------
smbclient  //172.25.254.102/mxw  -U mxw   
Enter mxw's password:                 ##输入密码
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ?                       ##显示这个环境下的命令
smb: \> ls
  .                                   D        0  Sun Feb 25 21:54:17 2018
  ..                                  D        0  Sun Feb 25 21:54:17 2018
  .bash_logout                        H       18  Wed Jan 29 07:45:18 2014
  .bash_profile                       H      193  Wed Jan 29 07:45:18 2014
  .bashrc                             H      231  Wed Jan 29 07:45:18 2014
  .mozilla                           DH        0  Thu Jul 10 18:29:32 2014
  .config                            DH        0  Thu Jul 10 19:06:52 2014

[root@localhost ~]# mount  //172.25.254.102/mxw  /mnt/  -o username=mxw,password=mxw
umount   /mnt
vim  /etc/fstab    ##永久挂载
//172.25.254.102/mxw  /mnt/  cifs  defaults,username=mxw,password=mxw  0  0 
mount  -a

//172.25.254.102/mxw  10473900 3182684   7291216  31% /mnt

在客户端 :

[root@localhost ~]# cd  /mnt
[root@localhost mnt]# touch  file
[root@localhost mnt]# ls
file

在服务器端:

[root@cifs-server ~]# cd  /home/mxw
[root@cifs-server mxw]# ls
file

5.自定义共享目录

当目录是用户建立时
在服务器端

mkdir   /mxwa  ##新建目录
vim  /etc/samba/smb.conf     ##查看配置文件的说明
 40 # Set SELinux labels only on files and directories you have created. Use the
 41 # chcon command to temporarily change a label:
 42 # chcon -t samba_share_t /path/to/directory

semanage  fcontext  -a  -t samba_share_t  '/mxwa(/.*)?'  ##解除SELINUX的文件访问限制 (设置该目录的SELINUX标签)
[root@cifs-server ~]# restorecon  -FvvR  /mxwa   ##重新加载SELINUX的标签
restorecon reset /mxwa context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
[root@cifs-server ~]# ls   -Zd  /mxwa
drwxr-xr-x. root root system_u:object_r:samba_share_t:s0 /mxwa
systemctl   restart   smb.service 
vim  /etc/samba/smb.conf   ##编辑
        [Mx]     ##Sharename  网络共享名 
        comment = HHHH   ##全局共享信息
        path = /mxwa     ##共享文件的绝对路径

测试 :
客户端

[root@localhost ~]# smbclient -L  //172.25.254.102  -U mxw    ##用户访问
Enter mxw's password: 
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    Mx              Disk      HHHH
    IPC$            IPC       IPC Service (HHHHHHHH)
    mxw             Disk      Home Directories
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]

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

    Workgroup            Master
    ---------            -------
[root@localhost ~]# smbclient  //172.25.254.102/Mx  -U mxw
Enter mxw's password: 
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Sun Feb 25 23:44:16 2018
  ..                                  D        0  Sun Feb 25 23:44:16 2018

        40913 blocks of size 262144. 28480 blocks available

在服务器端

[root@cifs-server ~]# cd /mxwa
[root@cifs-server mxwa]# touch  file{1..3}
[root@cifs-server mxwa]# ls
file1  file2  file3

在客户端

[root@localhost ~]# smbclient  //172.25.254.102/Mx  -U mxw
Enter mxw's password: 
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Mon Feb 26 00:04:25 2018
  ..                                  D        0  Sun Feb 25 23:44:16 2018
  file1                               N        0  Mon Feb 26 00:04:25 2018
  file2                               N        0  Mon Feb 26 00:04:25 2018
  file3                               N        0  Mon Feb 26 00:04:25 2018

        40913 blocks of size 262144. 28480 blocks available

匿名用户登陆权限开放

vim  /etc/samba/smb.conf
125         map  to guest = bad user   ##将所有非本地用户映射为guest (注意空格)  (匿名用户可以用guest名称挂载)
321         [Mx]
322         comment = HHHH
323         path = /mxwa
324         guest ok = yes             ##允许匿名用户登陆

[root@localhost ~]# mount  //172.25.254.102/Mx  /mnt  -o username=guest   ##

当目录系统建立时: (系统的SELINUX的标签不能改动)
服务器端

vim  /etc/samba/smb.conf
         [SYSTEMDATA]
         comment = HHHH
         path = /mnt

setsebool -P samba_export_all_ro on   ##开启后,samba 的SELINUX文件的安全上下不受控制
[root@cifs-server ~]# cd /mnt
[root@cifs-server mnt]# ls
[root@cifs-server mnt]# touch  file{1..5}
[root@cifs-server mnt]# ls
file1  file2  file3  file4  file5

客户端

[root@localhost ~]# smbclient  //172.25.254.102/SYSTEMDATA  -U mxw
Enter mxw's password: 
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Thu Mar 13 07:51:26 2014
  ..                                  D        0  Sun Feb 25 23:44:16 2018

        40913 blocks of size 262144. 28480 blocks available
[root@localhost ~]# smbclient  //172.25.254.102/SYSTEMDATA  -U mxw
Enter mxw's password: 
Domain=[MXW] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Mon Feb 26 01:52:36 2018
  ..                                  D        0  Sun Feb 25 23:44:16 2018
  file1                               N        0  Mon Feb 26 01:52:36 2018
  file2                               N        0  Mon Feb 26 01:52:36 2018
  file3                               N        0  Mon Feb 26 01:52:36 2018
  file4                               N        0  Mon Feb 26 01:52:36 2018
  file5                               N        0  Mon Feb 26 01:52:36 2018

        40913 blocks of size 262144. 28481 blocks available

6.权限控制

[root@cifs-server ~]# getsebool  -a  |  grep samba

samba_export_all_ro --> on
samba_export_all_rw --> off

[root@cifs-server ~]# setsebool   -P  samba_export_all_rw   on   ##开启samba在客户端的写功能
vim  /etc/samba/smb.conf
        [Mx]
        comment = HHHH
        path = /mxwa
        guest ok = yes

        writable = yes|no            ##是否开启写权力                      
        write  list  = mxw           ##写权力对mxw用户开放
        write  list  = +mxw          ##写权力对mxw组用户开放
        admin  users = mxw           ##设定mxw用户为当前共享的root
        valid  users  =mxw           ##设定当前共享的有效用户
        browseable = no              ##当前共享是否隐藏

测试:
客户端

[root@localhost ~]# cd  /mnt
[root@localhost mnt]# touch  ddd  sss
[root@localhost mnt]# ls
ddd  file  sss  sssss
[root@localhost ~]# mount  //172.25.254.102/Mx  /mnt/  -o username=mxw,password=mxw   
[root@localhost ~]# mount  //172.25.254.102/Mx  /mnt/  -o username=westos,password=westos  
mount: //172.25.254.102/Mx is write-protected, mounting read-only
mount: cannot mount //172.25.254.102/Mx read-only ##westos不是有效用户
[root@localhost mnt]# touch lpl
[root@localhost mnt]# ll
total 0
-rw-r--r-- 1 root 1002 0 Feb 26 03:49 lpl    ##(admin  users = mxw)

服务端

[root@cifs-server ~]# id  mxw
uid=1002(mxw) gid=1002(mxw) groups=1002(mxw)
[root@cifs-server ~]# cd /home/mxw
[root@cifs-server mxw]# ls
ddd  file  sss  sssss

7.samba的多用户挂载 ()

在客户端

yum  install  cifs-utils   -y
[root@localhost ~]# vim   /root/sambpasswd   ##编辑用户名和密码信息

[root@localhost ~]# chmod  600  /root/sambpasswd  ##设置权限只对root用户可读写
[root@localhost ~]# mount  -o credentials=/root/sambpasswd,sec=ntlmssp,multiuser //172.25.254.102/Mx    /mnt
credentials=/root/sambpasswd     ##挂载时的用户认证(指定到读这个文件)
sec=ntlmssp                      ##支持其他用户认证
multiuser                        ##其他用户的人认证方式

测试

[root@localhost ~]# ls  /mnt
lpl
[root@localhost ~]# su  - student        ##切换到student
Last login: Thu May 11 20:23:54 EDT 2017 on pts/0
[student@localhost ~]$ ls  /mnt
ls: cannot access /mnt: Permission denied
[student@localhost ~]$ cifscreds add -u mxw  172.25.254.102       ##对student添加用户认证
Password:            ##输入mxw用户密码
[student@localhost ~]$ ls  /mnt
lpl
[student@localhost mnt]$ ll
total 0
-rw-r--r-- 1 root 1002 0 Feb 26 03:49 lpl         ##建立的lpl所用的用户身份是samba用户mxw

以上关于SELINUX的限制解除看配置文件的SELINUX

猜你喜欢

转载自blog.csdn.net/qq_41661056/article/details/79396879