Linux---NFS文件系统

网络文件系统 (NFS) :linux系统的存储分离;是 Unix 系统和网络附加存储文件管理器常用的网络文件系统

NFS原理:

NFS包括两部分,服务端及客户端

由于NFS服务功能很多,会有很多端口,这些端口还有可能不固定,那么客户端就无法与服务器进行通信,因为程序间通信必须通过端口(tcp/udp都是端到端通信),那么就需要一个中间的桥接机制,RPC进程即充当这样一个角色,RPC的端口是一定的(111),当NFS启动时,会向RPC进行注册, 那么客户端PRC就能与服务器RPC进行通信, 从而进行文件的传输。

当客户端用户打开一个文件或目录时,内核会判断,该文件是本地文件还是远程共享目录文件,如果是远程文件则通过RPC进程访问远程NFS服务端的共享目录,如果是本地文件,则直接打开。

一、安装部署
[服务端212]
[root@localhost ~]# yum install nfs-utils -y              ##安装服务
[root@localhost ~]# systemctl start nfs              ##开启服务
[root@localhost ~]# systemctl enable nfs-server.service       ##服务开机自启动
[root@localhost ~]# firewall-cmd --permanent --add-service=nfs
[root@localhost ~]# firewall-cmd --permanent --add-service=rpc-bind    
[root@localhost ~]# firewall-cmd --permanent --add-service=mountd
[root@localhost ~]# firewall-cmd --reload
[root@localhost ~]# vim /etc/exports         ###nfs配置文件
                    ###共享的目录    共享给谁(共享方式:数据同步)
[root@localhost ~]# exportfs -rv      ##-rv刷新服务、列出服务内容;-r刷新,服务内容不可看;-v 列出服务内容但不刷新
[root@localhost ~]# showmount -e 172.25.254.212          ###查看212主机的共享目录内容
Export list for 172.25.254.212:
/hello   *
[客户端112]
[root@localhost ~]# yum install nfs-utils -y        ##安装服务
[root@localhost ~]# showmount -e 172.25.254.212      ###查看212主机的共享目录内容
Export list for 172.25.254.212:
/hello *
[root@localhost ~]# mount  172.25.254.212:/hello  /mnt/       ##挂载服务端共享目录到客户端/mnt上
[root@localhost ~]# df


[root@localhost ~]# cd /mnt/
[root@localhost mnt]# touch i
touch: cannot touch ‘i’: Read-only file system           ###不可写
[root@localhost mnt]# rm -fr file
rm: cannot remove ‘file’: Read-only file system     ###不可删除
二、手动挂载下参数改变
[root@localhost ~]# chmod 777 /hello/
1.所有人可读写
[root@localhost ~]# vim /etc/exports
       ###共享人可对共享目录读写
[root@localhost ~]# exportfs -rv       ##刷新服务
检测:
[root@localhost mnt]# touch i       ###可写
[root@localhost mnt]# ls
a  b  c  d  f  file  g  h  i
[root@localhost mnt]# rm -fr file    ##可删除
[root@localhost mnt]# ls
a  b  c  d  f  g  h  i
2.任何人挂载后,都用的是服务端的超级用户身份
[root@localhost ~]# vim /etc/exports
     ###挂载后用root身份
[root@localhost ~]# exportfs -rv
检测:
[root@localhost mnt]# ll  /mnt/i          
-rw-r--r-- 1 nfsnobody nfsnobody 0 Nov 13 21:17 /mnt/i      ###原挂载都为nfs匿名用户
[root@localhost mnt]# touch  j
[root@localhost mnt]# ll  /mnt/j
-rw-r--r-- 1 root root 0 Nov 13 21:20 /mnt/j      ###新建文件所有人、所有组都为root
3.指定用户身份、用户组身份,任何人挂载都用它
[root@localhost ~]# vim /etc/exports
    ###挂载后用户身份id、组id
[root@localhost ~]# exportfs -rv
检测:
[root@localhost mnt]# ll /mnt/k
-rw-r--r-- 1 student student 0 Nov 13 21:26 /mnt/k        ###新建文件所有人、所有组为student
4.添加12只可读
[root@localhost ~]# vim /etc/exports
      ##指定共享用户12,则其他不可访问
[root@localhost ~]# exportfs -rv
检测:
[root@foundation12 ~]# yum  install nfs-utils  -y
[root@foundation12 ~]# mount  172.25.254.212:/hello  /mnt
[root@foundation12 mnt]# touch k              ###12主机只可读
touch: cannot touch ‘k’: Permission denied

[root@foundation12 mnt]# rm -f b
rm: cannot remove ‘b’: Read-only file system
三、客户端自动挂载
手动挂载:则共享目录占用两边资源,浪费资源
自动挂载:使用时自动挂,不用时自动卸载
[root@localhost ~]# yum  install  autofs -y         ###自动挂载服务
[root@localhost ~]# systemctl start autofs          ###开启服务
[root@localhost ~]# systemctl enable autofs
1. 更改自动卸载等待时间(初始300s)
[root@localhost ~]# vim /etc/sysconfig/autofs      ##自动挂载配置文件
              ##更改等待时间为10s
[root@localhost ~]# systemctl restart autofs.service

检测:
[root@localhost ~]# cd /net                 ##切到默认挂载目录的上级目录
[root@localhost net]# cd 172.25.254.212                ##切到自动挂载目录
[root@localhost 172.25.254.212]# ls
hello
[root@localhost 172.25.254.212]# df            ##查看,已自动挂载

[root@localhost hello]# cd              ##退出使用,等待10s
[root@localhost ~]# df           ##查看,已自动卸载

2.更改自动挂载目录为/nfsdir/hello  (原本:/net/172.25.254.212/)
[root@localhost ~]# vim /etc/auto.master         ##自动挂载配置文件
         ##最终挂载点的上层目录     子策略文件(自定义,原不存在)
[root@localhost ~]# vim /etc/auto.hello        ##编辑子策略文件
      ##最终挂载点    参数(只读)     挂载设备
[root@localhost ~]# systemctl restart autofs.service

检测:
[root@localhost ~]# cd /nfsdir             ##切到设置的挂载目录的上级目录
[root@localhost nfsdir]# cd hello      ##切到设置的挂载目录
[root@localhost hello]# df              ##查看,已自动挂载到设置目录
[root@localhost hello]# cd       ##退出使用,等待10s
[root@localhost ~]# df        ##已自动卸载

3.更改自动挂载可读写
[root@localhost ~]# vim /etc/auto.hello       ##编辑子策略文件
hello   -rw   172.25.254.212:/hello         ##最终挂载点    参数(可读写)     挂载设备
[root@localhost ~]# systemctl restart autofs.service

检测:

[root@localhost ~]# cd /nfsdir/hello

[root@localhost ~]# touch  test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值