访问网络文件的共享服务——CIFS

CIFS  NFS

网络文件系统是由网络附加存储服务器通过网络向多个主机提供的一种文件系统,而不是块设备(例如硬盘驱动器)提供的.客户端通过特殊的文件系统协议和格式访问远程存储。
linux中有两种主要协议可用访问网络文件系统:NFS和CIFS.NFS(Network File System)客人看作时linux和Unix及其类似操作系统的标准文件类型,CIFS(Common Internet File System)则是针对Microft Windows 系统的标准网络文件系统
访问网络共享的三个基本步骤
@1识别要访问的远程共享
@2确定挂载点(应该将共享目录挂载到的位置),并创建挂载点的空目录
@3通过相关的命令或者配置更改挂载的网络文件系统

CIFS通用网络文件系统

@CIFS是针对Microsoft Windows操作系统的本地网络文件系统,Linux系统可以挂载和访问CIFS文件共享,如同常见的网络文件系统一样,然而,由于CIFS是基于NTFS文件系统权限模型及其自身的身份验证系统来构建的,因而CIFS协议中的所有内容并不能很好地映射到Linux中
@samba-clicent RPM软件包中所包含的smbclient实用程序可用来识别有Windows或Samba文件服务器提供的CIFS共享,该实用程序工作起来犹如在Microsoft Windows中单击网络邻居然后mount命令挂载共享,访问CIFS共享的命令

@1.在服务端安装samba及其组件,并开启服务

 yum install samba samba-client samba-common -y
[root@cifs ~]# rpm -qa| grep samba*    查看安装的软件如果有这几个安装成功
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
samba-4.1.1-31.el7.x86_64
systemctl start smb    开启服务

@2.启动共享访问客户端及服务

[root@localhost ~]# smbclient -L //172.25.254.123 
Enter root's password: 
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

	Sharename       Type      Comment
	---------       ----      -------
	IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

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

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

@3添加smb用户,没有要创建用户,smbpasswd -a xaut,添加xaut并设置密码

pdbedit -L   显示用户
pdbedit -x   xxx 删除用户
pdbedit -a   xxx 添加用户只能添加系统存在的若没有
useradd      xxx

访问共享,输入这个可以看到共享信息,但是不能看到xaut用户加目录,因为我改了selinux所以图中可以查看xaut家目录

[root@localhost ~]# smbclient  //172.25.254.123/xaut  -U xaut
Enter xaut's password: 
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls                    查看当前目录   !ls查看未时目录登录
  .                                   D        0  Wed Aug 15 00:05:23 2018
  ..                                  D        0  Wed Aug 15 00:02:58 2018
  .bash_profile                       H      193  Wed Jan 29 07:45:18 2014
  anaconda-ks.cfg                     A     8619  Wed Aug 15 00:05:24 2018
  .mozilla                           DH        0  Thu Jul 10 18:29:32 2014
  .config                            DH        0  Thu Jul 10 19:06:52 2014
  .bashrc                             H      231  Wed Jan 29 07:45:18 2014
  .bash_logout                        H       18  Wed Jan 29 07:45:18 2014

		60458 blocks of size 8192. 56477 blocks available
smb: \> quit

@4挂载实现更多操作

@1临时挂载
mount //172.25.254.123/student /mnt/ -o username=student,password=123
@2永久挂载
vim /etc/fstab
//172.25.254.123/student    /mnt    cifs  defaults,username=student,password=123  0 0
vim /etc/rc.d/rc.local
mount  //172.25.254.123/student /mnt -o username=student,password=123
chmod 755 /etc/rc.d/rc.local

smb用户访问控制

[root@localhost ~]# vim /etc/samba/smb.conf
        host deny=172.25.254.123        拒绝123登录
        workgroup=xxx        作用域
        hosts allow = xx        允许xx登录
[root@localhost ~]# smbclient //172.25.254.123/ -U  xaut
Unknown parameter encountered: "host deny"    登录失败
Ignoring unknown parameter "host deny"
Enter xaut's password:

smb下共享目录

1改变selinux=Enforcing

[root@localhost xaut]# vim /etc/sysconfig/selinux
[root@localhost xaut]# reboot

2查看selinux

[root@localhost ~]# getenforce
Enforcing

3用户登录查看失败

[root@localhost ~]# smbclient //172.25.254.123/xaut -U  xaut
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
smb: \> quit

4.查看接口

[root@localhost ~]# getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off

5打开写读的功能

[root@localhost ~]# setsebool -P samba_enable_home_dirs on
[root@localhost ~]# setsebool -P samba_create_home_dirs on

6实现查看

[root@localhost ~]# smbclient //172.25.254.123/xaut -U  xaut
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Mon Aug 13 22:13:52 2018
  ..                                  D        0  Mon Aug 13 22:07:56 2018
  .bash_profile                       H      193  Wed Jan 29 07:45:18 2014
  .mozilla                           DH        0  Thu Jul 10 18:29:32 2014
  file1                               N        0  Mon Aug 13 22:13:52 2018
  file2                               N        0  Mon Aug 13 22:13:52 2018
  .config                            DH        0  Thu Jul 10 19:06:52 2014
  .bashrc                             H      231  Wed Jan 29 07:45:18 2014
  .bash_logout                        H       18  Wed Jan 29 07:45:18 2014

        60458 blocks of size 8192. 56477 blocks available
smb: \> ^C

7新建xaut目录

[root@localhost ~]# mkdir /xaut
[root@localhost ~]# ls -ld /xaut/
drwxr-xr-x. 2 root root 6 Aug 13 22:51 /xaut/
[root@localhost ~]# touch /xaut/xautfile{1..3}

8更改配置文件

        [linux]        共享目录时对方看到的目录名称
        comment= dir from xaut    提示根目录
        path=/xaut        指定建立的路径

9重启服务xaut登录查看共享目录

[root@localhost ~]# systemctl restart smb.service
[root@localhost ~]# smbclient -L //172.25.254.123/ -U  xaut
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    linux           Disk      dir from xaut
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
    xaut            Disk      Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

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

    Workgroup            Master
    ---------            -------
进入目录
[root@localhost ~]# smbclient  //172.25.254.123/linux -U  xaut
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls                ls出错
NT_STATUS_ACCESS_DENIED listing \*
smb: \> quit
设置安全上下文
[root@localhost ~]# setenforce 0
重新登录
[root@localhost ~]# smbclient  //172.25.254.123/linux -U  xaut
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls                ls可以列出目录下的文件信息
  .                                   D        0  Mon Aug 13 22:51:46 2018
  ..                                  D        0  Mon Aug 13 22:51:10 2018
  xautfile1                           N        0  Mon Aug 13 22:51:46 2018
  xautfile2                           N        0  Mon Aug 13 22:51:46 2018
  xautfile3                           N        0  Mon Aug 13 22:51:46 2018

        40913 blocks of size 262144. 28314 blocks available
smb: \> quit
更改目录的目录下的文件的安全上下文
[root@localhost ~]# semanage fcontext -a -t samba_share_t '/xaut(/.*)?'
[root@localhost ~]# restorecon -RvvF /xaut/        显示刷新过程
[root@localhost ~]# smbclient  //172.25.254.123/linux -U  xaut    登录也可以和setenforce 0效果相似
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Mon Aug 13 22:51:46 2018
  ..                                  D        0  Mon Aug 13 22:51:10 2018
  xautfile1                           N        0  Mon Aug 13 22:51:46 2018
  xautfile2                           N        0  Mon Aug 13 22:51:46 2018
  xautfile3                           N        0  Mon Aug 13 22:51:46 2018

        40913 blocks of size 262144. 28277 blocks available
smb: \> quit
[root@localhost ~]# systemctl restart smb.service
新建mnt的共享
[root@localhost ~]# vim /etc/samba/smb.conf
    [mnt]
        comment=dir from /mnt
        path=/mnt
[root@localhost ~]# systemctl restart smb.service
[root@localhost ~]# ls -Zd /mnt/        查看mnt的权限
drwxr-xr-x. root root system_u:object_r:mnt_t:s0       /mnt/
[root@localhost ~]# smbclient  -L //172.25.254.123/ -U  xaut
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    linux           Disk      dir from xaut
    mnt             Disk      dir from /mnt        存在mnt
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
    xaut            Disk      Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

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

    Workgroup            Master
    ---------            -------
[root@localhost ~]# smbclient   //172.25.254.123/mnt -U  xaut  进入mnt目录
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Thu May 11 20:23:52 2017
  ..                                  D        0  Mon Aug 13 22:51:10 2018

        40913 blocks of size 262144. 28308 blocks available
smb: \> quit
更改上下文
[root@localhost ~]# setsebool -P samba_export_all_ro on
删除原有的xaut目录
[root@localhost ~]# rm -rf /xaut/
[root@localhost ~]# mkdir /xaut
[root@localhost ~]# ls -Zd /xaut/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /xaut/
[root@localhost ~]# touch /xaut/file1
新建的目录本并为设置允许但因为端口开着所以直接就可以访问
[root@localhost ~]# smbclient    //172.25.254.123/linux -U  xaut
Enter xaut's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Mon Aug 13 23:08:37 2018
  ..                                  D        0  Mon Aug 13 23:08:14 2018
  file1                               N        0  Mon Aug 13 23:08:37 2018

        40913 blocks of size 262144. 28293 blocks available
smb: \> quit
[root@localhost ~]# ls -Zd /xaut/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /xaut/

匿名登录

yum install cifs-utils.x86_64 -y
   36  vim /root/smbauth
        username=student
        password=123
   37  mount //172.25.254.123/xaut /mnt -o multiuser,credentials=/root/smbauth,sec=ntlmssp
[root@server mnt]# su - xue
Last login: Wed Aug 15 04:21:51 EDT 2018 on pts/0
[xue@server ~]$ cd /mnt
[xue@server mnt]$ ls
ls: reading directory .: Permission denied
[xue@server mnt]$ cifscreds add -u student 172.25.254.123
Password:
[xue@server mnt]$ ls
filexue

[root@localhost mnt]# vim /etc/exports
[root@localhost mnt]# exportfs -rv
/xaut  *(ro,sync)
[root@sever xaut]# touch file1
touch: cannot touch ‘file1’: Read-only file system
/xaut  *(rw,sync)
/xaut  *(rw,sync,anonuid=1000,anongid=1001)
/xaut  *(rw,sync,no_root_squash)
/xaut  *(sync)  172.25.254.0/24*(rw,sync,no_root_squash)
[root@sever xaut]# touch file1
[root@sever xaut]# touch file2
[root@sever xaut]# touch file3
[root@sever xaut]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 14 05:07 file1
-rw-r--r--. 1 student   xue       0 Aug 14 05:09 file2
-rw-r--r--. 1 root      root      0 Aug 14 05:10 file3

自动挂载

[root@sever ~]# yum install autofs.x86_64  -y
[root@sever ~]# systemctl start autofs
cd /net/172.25.254.123/xaut
[root@sever xaut]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/vda1             10473900 3218960   7254940  31% /
devtmpfs                469344       0    469344   0% /dev
tmpfs                   484932      84    484848   1% /dev/shm
tmpfs                   484932   12800    472132   3% /run
tmpfs                   484932       0    484932   0% /sys/fs/cgroup
/dev/sr1               3654720 3654720         0 100% /run/media/root/RHEL-7.0 Server.x86_64
172.25.254.123:/xaut  10473984 3231232   7242752  31% /net/172.25.254.123/xaut
[root@sever ~]# vim /etc/sysconfig/autofs
更改刷新时间
[root@sever ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3218952   7254948  31% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932      84    484848   1% /dev/shm
tmpfs             484932   12800    472132   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/sr1         3654720 3654720         0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@sever ~]# systemctl restart autofs
[root@sever ~]# cd  /net
[root@sever net]# ls
[root@sever net]# cd 172.25.254.123
[root@sever 172.25.254.123]# ls
xaut
[root@sever 172.25.254.123]# cd xaut
[root@sever xaut]# ls
file1
[root@sever xaut]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/vda1             10473900 3218960   7254940  31% /
devtmpfs                469344       0    469344   0% /dev
tmpfs                   484932      84    484848   1% /dev/shm
tmpfs                   484932   12800    472132   3% /run
tmpfs                   484932       0    484932   0% /sys/fs/cgroup
/dev/sr1               3654720 3654720         0 100% /run/media/root/RHEL-7.0 Server.x86_64
172.25.254.123:/xaut  10473984 3231232   7242752  31% /net/172.25.254.123/xaut

[root@sever xaut]# vim /etc/auto.master
/opt/nfs        /etc/auto.nfs
[root@sever xaut]# vim /etc/auto.nfs
xaut  -ro 172.25.254.123:/xaut
[root@sever ~]# systemctl restart autofs
[root@sever ~]# cd /opt/
[root@sever opt]# ls
nfs  rh
[root@sever opt]# cd nfs
[root@sever nfs]# ls
[root@sever nfs]# cd xaut
[root@sever xaut]# pwd
/opt/nfs/xaut
[root@sever xaut]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/vda1             10473900 3218976   7254924  31% /
devtmpfs                469344       0    469344   0% /dev
tmpfs                   484932      84    484848   1% /dev/shm
tmpfs                   484932   12800    472132   3% /run
tmpfs                   484932       0    484932   0% /sys/fs/cgroup
/dev/sr1               3654720 3654720         0 100% /run/media/root/RHEL-7.0 Server.x86_64
172.25.254.123:/xaut  10473984 3231232   7242752  31% /opt/nfs/xaut

猜你喜欢

转载自blog.csdn.net/weixin_40460156/article/details/81698756
今日推荐