当前位置: 首页 > news >正文

NFS服务小实验

实验1

  • 建立NFS服务器,使的客户端顺序共享数据

  • 第一步:服务端及客户端的准备工作

# 恢复快照
[root@server ~]# setenforce  0
​
[root@server ~]# systemctl stop  firewalld
​
[root@server ~]# yum  install  nfs-utils  -y  # 服务端及客户端都安装
  • 第二步:服务端建立共享文件目录,并设置权限

[root@server ~]# mkdir  /nfsfile
[root@server ~]# echo  "welcome to www.openlab.com" > /nfsfile/readme
[root@server ~]# chmod  -Rf  777  /nfsfile
  • 第三步:服务端编辑配置文件

[root@server ~]# vim  /etc/exports  # 添加以下内容
/nfsfile        192.168.48.131/24(rw,sync,all_squash)
  • 第四步:服务端先启动rpc服务,在启动nfs服务

[root@server ~]# systemctl start  rpcbind
[root@server ~]# systemctl start  nfs-server
[root@server ~]# systemctl enable  rpcbind
[root@server ~]# systemctl enable  nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
# 注意:若systemctl start  rpcbind后报错,显示启动失败,应先停止服务,在重启服务
[root@server ~]# systemctl stop rpcbind
[root@server ~]# systemctl start  rpcbind
  • 第五步:客户端使用本地挂载方式访问远程共享目录

    • 先查询有哪些远程共享目录可用

    • [root@node1 ~]# showmount  -e  192.168.48.130  # 别忘了安装nfs-utils
      # -e 表示显示服务器共享信息列表
    • 新建本地挂载目录

    • [root@node1 ~]# mkdir  /nfs1  # 新建本地挂载目录
      ​
      # 使用mount远程挂载命令挂载服务器的目录到客户端本地目录,-t nfs 表示使用的文件系统类型
      [root@node1 ~]# mount -t nfs  192.168.48.130:/nfsfile  /nfs1
      [root@node1 ~]# cd  /nfs1   # 通过本地目录访问远程共享数据
      [root@node1 nfs1]# ls
      readme
      [root@node1 nfs1]# cat  readme 
      welcome to www.openlab.com
      [root@node1 nfs1]# cd   ~
  • 第六步:客户端使用开机挂载实现永久挂载

[root@node1 ~]# vim  /etc/fstab   # 编辑开机挂载配置文件,最后一行添加如下内容
192.168.48.130:/nfsfile /nfs1   nfs     defaults        0       0
​
[root@node1 ~]# mount  -a   # 测试
[root@node1 nfs1]# df  -h   # 查看挂载信息
文件系统                    容量  已用  可用 已用% 挂载点
devtmpfs                    4.0M     0  4.0M    0% /dev
tmpfs                       714M     0  714M    0% /dev/shm
tmpfs                       286M  4.2M  282M    2% /run
tmpfs                       4.0M     0  4.0M    0% /sys/fs/cgroup
/dev/mapper/openeuler-root   27G  1.6G   24G    7% /
tmpfs                       714M     0  714M    0% /tmp
/dev/sda1                   974M  151M  756M   17% /boot
192.168.48.130:/nfsfile      27G  1.6G   24G    7% /nfs1
​

NFS账户映射

实验2:
  • 基于上例,将/nfsfile权限修改后,查看客户端是否可以访问成功,是否可以修改数据

# 定位服务端
[root@server ~]# cat  /etc/exports   # 确认是否为rw可读可写的权限
[root@server ~]# cd /nfsfile
[root@server nfsfile]# ll     # 查看原有权限
总用量 4
-rwxrwxrwx 1 root   root   27  6月  1 14:43 readme
-rw-r--r-- 1 nobody nobody  0  6月  1 14:56 test1.txt
[root@server nfsfile]# cd  ~   
[root@server ~]# chmod  -Rf  444  /nfsfile  # 设置新权限,没有wx
[root@server ~]# ll  -d  /nfsfile/
dr--r--r-- 2 root root 37  6月  1 14:56 /nfsfile/
[root@server ~]# ll    /nfsfile/
总用量 4
-r--r--r-- 1 root   root   27  6月  1 14:43 readme
-r--r--r-- 1 nobody nobody  0  6月  1 14:56 test1.txt
​
​
# 定位客户端
[root@node1 ~]# cd  /nfs1   
-bash: cd: /nfs1: 权限不够
​
# 显示无权限,即使服务端exports中有rw权限最后还是以文件系统权限为准
实验3
  • 客户端访问服务端,默认使用服务端的账户映射为nobody,uid与gid均为65534,其值可以修改

# 定位服务端
[root@server ~]# chmod  -Rf  777  /nfsfile   # 先将上例的权限修改回原样
[root@server ~]# ll  -d  /nfsfile/
drwxrwxrwx 2 root root 37  6月  1 14:56 /nfsfile/
​
[root@server ~]# vim  /etc/exports   # 修改映射方式为
/nfsfile        192.168.48.131/24(rw,sync,root_squash)
​
[root@server ~]# systemctl restart  rpcbind  
[root@server ~]# systemctl restart  nfs-server
​
exporting 192.168.48.131/24:/nfsfile
​
# 定位客户端
[root@node1 ~]# cd  /nfs1
[root@node1 nfs1]# touch  test2.txt
[root@node1 nfs1]# ll
总用量 4
-rwxrwxrwx 1 root   root   27  6月  1 14:43 readme
-rwxrwxrwx 1 nobody nobody  0  6月  1 14:56 test1.txt
-rw-r--r-- 1 nobody nobody  0  6月  1 15:15 test2.txt  # root映射为nobody
[root@node1 nfs1]# su  fox
[fox@node1 nfs1]$ touch  test3.txt
[fox@node1 nfs1]$ ll
总用量 4
-rwxrwxrwx 1 root   root   27  6月  1 14:43 readme
-rwxrwxrwx 1 nobody nobody  0  6月  1 14:56 test1.txt
-rw-r--r-- 1 nobody nobody  0  6月  1 15:15 test2.txt
-rw-rw-r-- 1 fox    fox     0  6月  1 15:16 test3.txt  # fox映射为fox
[fox@node1 nfs1]$ su  root
密码: 
​
# 定位服务端
[root@server ~]# vim  /etc/exports
/nfsfile    192.168.48.131/24(rw,sync,all_squash,anonuid=2000,anongid=2000)
​
[root@server ~]# systemctl restart  rpcbind  
[root@server ~]# systemctl restart  nfs-server
​
# 定位客户端
[root@node1 nfs1]# touch  test4.txt
[root@node1 nfs1]# ll
总用量 4
-rwxrwxrwx 1 root   root   27  6月  1 14:43 readme
-rwxrwxrwx 1 nobody nobody  0  6月  1 14:56 test1.txt
-rw-r--r-- 1 nobody nobody  0  6月  1 15:15 test2.txt
-rw-rw-r-- 1 fox    fox     0  6月  1 15:16 test3.txt
-rw-r--r-- 1   2000   2000  0  6月  1 15:19 test4.txt  # 自定义udi及gid
[root@node1 nfs1]# cd  ~

autofs自动挂载服务

产生原因

  • 在一般NFS文件系统的使用过程中,如果客户端要使用服务端所提供的文件系统,可以在 /etc/fstab中设置开机时自动挂载,也可以在登录系统后手动利用mount来挂载。

  • 由于网络的问题,NFS服务器与客户端的连接不会一直存在,当我们挂载了NFS服务器之后,任何一方脱机都可能造成另外一方等待超时,如果在资源挂载后长期不使用,也会造成服务器硬件资源的浪费。

  • 为了解决这样的问题,就出现了下面的想法:

    • 当客户端在有使用NFS文件系统的需求时才让系统自动挂载。

    • 当NFS文件系统使用完毕后(autofs默认自动卸载时间为300s即5分钟),让NFS自动卸载。

  • autofs 自动挂载服务可以帮我们解决这一问题,该服务是运行在客户端的一种 Linux 系统守护进程,在用户需要使用该文件系统时才去动态挂载,从而节约了网络资源和服务器的硬件资源。

安装

[root@node1 ~]# yum install autofs -y   # 客户端安装

配置文件分析

  • 配置文件路径:/etc/auto.master

  • 作用:处于生产环境中的 Linux 服务器,一般会同时管理许多设备的挂载操作。如果把这些设备挂载信息都写入到 autofs 服务的主配置文件中,无疑会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容,则需要将挂载的设备独立配置形成子文件,主配置文件只存储配置挂载设置的文件名

  • /etc/auto.master文件内容格式:挂载父目录 子配置文件

    • 挂载目录:不需要事先存在,因为autofs会主动建立该目录

    • 子配置文件:文件名可自定义

    • 例:/nfs /etc/auto.nfs

  • 子配置文件由自己创建,内容格式:

    • 本地挂载子目录 [-挂载参数] 服务器地址:目录

    • 例:testmnt 192.168.48.130:/data

    • 挂载参数:

相关文章:

  • 电子电气架构 --- 下一代汽车电子电气架构中的连接性
  • Llamaindex Rag 报错
  • 利用Qt绘图随机生成带多种干扰信息的数字图片
  • 编译原理 期末速成
  • JMeter 教程:监控性能指标 - 第三方插件安装(PerfMon)
  • Jmeter(三) - 测试计划(Test Plan)的元件
  • OpenSSL详解
  • 【学习笔记】机器学习(Machine Learning) | 第七章|神经网络(4)
  • Web前端开发:JavaScript的使用
  • Claude 4 系列 Opus 4 与 Sonnet 4正式发布:Claude 4新特性都有哪些?
  • 树 Part 10
  • nginx 的反向代理 负载均衡 动静分离 重写
  • 利用条件编译实现RTT可控的调试输出
  • 精准核验,实时响应-身份证实名认证接口-身份证二要素核验
  • TCP为什么是三次握手,而不是二次?
  • Solana 数据实时访问的三大工具对比:哪种最适合你的应用?
  • PHP实现签名类
  • 外卖跑腿小程序评价系统框架搭建
  • 嵌入式鸿蒙openharmony应用开发环境搭建与工程创建实现
  • android studio第一次编译apk,用时6分钟
  • 滨江区住房和城乡建设局网站/郑州网站优化顾问
  • 网站建设 cn/怎么注册网址
  • 开源快速网站搭建平台/app推广代理去哪里找
  • 南山做网站多少钱/最新推广赚钱的app
  • 网站建设报告实训步骤/seo专业技术培训
  • 怎样用xampp做网站/智推教育seo课程