NFS循序渐进

第一节、环境详情

  • 主机详情
主机 ip 操作系统
nfs-server 192.168.2.120 CentOS Linux release 7.4.1708
nfs-client 192.168.2.121 CentOS Linux release 7.4.1708
  • 关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i 's/SELINUX=disabled/SELINUX=disabled/' /etc/selinux/config

背景:解决多个服务器之间数据共享

第二节、安装软件

  • 客户端和服务端都要安装

2.1服务端

软件包nfs-utils用来提供NFS共享服务及相关工具,而软件包rpcbind用来提供RPC协议的支持,这两个包在RHEL7(centos7)系统中一般都是默认安装的:

[root@nfs-server ~]# rpm  -q  nfs-utils  rpcbind
nfs-utils-1.3.0-0.48.el7.x86_64
rpcbind-0.2.0-42.el7.x86_64
//如果未安装
[root@nfs-server ~]# yum -y install nfs-utils  rpcbind

2.2客户端

软件包nfs-utils用来提供NFS共享服务及相关工具,而软件包rpcbind用来提供RPC协议的支持,这两个包在RHEL7(centos7)系统中一般都是默认安装的:

[root@nfs-client ~]# rpm  -q  nfs-utils  rpcbind
nfs-utils-1.3.0-0.48.el7.x86_64
rpcbind-0.2.0-42.el7.x86_64
//如果未安装
[root@nfs-client ~]# yum -y install nfs-utils  rpcbind

第三节、服务端配置共享

  • 需要作为NFS共享发布的有/data/app、/data/web这两个目录:
[root@nfs-server ~]# mkdir /data/app -p
[root@nfs-server ~]# mkdir /data/web -p
[root@nfs-server ~]# ls -ld /data/app /data/web
drwxr-xr-x. 2 root root 6 6月   1 20:06 /data/app
drwxr-xr-x. 2 root root 6 6月   1 20:06 /data/web
//写入测试数据
[root@nfs-server ~]# echo app > /data/app/app.txt
[root@nfs-server ~]# echo web > /data/web/web.txt

默认情况下,来自NFS客户端的root用户会被自动降权为普通用户,若要保留其root权限,注意应添加no_root_squash控制参数(没有该参数,默认root会被自动降级为普通账户);另外,限制只读的参数为ro、可读可写为rw,相关配置操作如下所示:

[root@nfs-server ~]# vim  /etc/exports
/data/app 192.168.2.121(rw,no_root_squash)
/data/web 192.168.2.121(ro)

  • 启动NFS共享相关服务,确认共享列表
    依次启动rpcbiind、nfs服务:
[root@nfs-server ~]# systemctl restart rpcbind  ;  systemctl enable rpcbind
[root@nfs-server ~]# systemctl restart nfs ;  systemctl enable nfs

  • 使用showmount命令查看本机发布的NFS共享列表
[root@nfs-server ~]# showmount  -e  localhost
Export list for localhost:
/data/web 192.168.2.121
/data/app 192.168.2.121
[root@nfs-server ~]# showmount  -e  192.168.2.120
Export list for 192.168.2.120:
/data/web 192.168.2.121
/data/app 192.168.2.121

第四节、客户端挂载NFS

  • 启用NFS共享支持服务
    客户机访问NFS共享也需要rpcbind服务的支持,需确保此服务已开启
[root@nfs-client ~]# systemctl restart rpcbind  ;  systemctl enable rpcbind
  • 查看服务器提供的NFS共享列表
[root@nfs-client ~]# showmount  -e 192.168.2.120
Export list for 192.168.2.120:
/data/web 192.168.2.121
/data/app 192.168.2.121
  • 客户机挂载
[root@nfs-client ~]# mkdir /data/app -p
[root@nfs-client ~]# mkdir /data/web -p
//临时挂载
[root@nfs-client ~]# mount  -t  nfs   192.168.2.120:/data/web    /data/web
[root@nfs-client ~]# mount  -t  nfs   192.168.2.120:/data/app    /data/app
//永久挂载
[root@nfs-client ~]# vim /etc/fstab
192.168.2.120:/data/web /data/web nfs defaults,_rnetdev 1  1
192.168.2.120:/data/app /data/app nfs defaults,_rnetdev 1  1
备注:第1个1表示备份文件系统,第2个1表示从/分区的顺序开始fsck磁盘检测,0表示不检测。
_rnetdev  表示主机无法挂载直接跳过,避免无法挂载主机无法启动
[root@nfs-client ~]# mount -a
[root@nfs-client ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
192.168.2.120:/data/web   50G   13G   38G   25% /data/web
192.168.2.120:/data/app   50G   13G   38G   25% /data/app
  • 验证/data/app可读可写权限
[root@nfs-client ~]# cd /data/app/
[root@nfs-client app]# 
[root@nfs-client app]# ls
app.txt
[root@nfs-client app]# cat app.txt 
app
[root@nfs-client app]# echo 'NFS write test' > test.txt
[root@nfs-client app]# ls
app.txt  test.txt
[root@nfs-client app]# cat test.txt 
NFS write test
//nfs-server主机查看文件多了文件test.txt
[root@nfs-server ~]# cd /data/app/
[root@nfs-server app]# ls
app.txt  test.txt
[root@nfs-server app]# cat test.txt 
NFS write test

  • 验证/data/web可读权限
[root@nfs-client app]# cd /data/web/
[root@nfs-client web]# ls
web.txt
[root@nfs-client web]# cat web.txt 
web
[root@nfs-client web]# echo 'test' > test.txt
-bash: test.txt: 只读文件系统
//nfs-server主机写入文件
[root@nfs-server app]# cd /data/web/
[root@nfs-server web]# echo 'nfs-server write data test' > test.txt
[root@nfs-server web]# ls
test.txt  web.txt
[root@nfs-server web]# cat test.txt 
nfs-server write data test
//客户机nfs-client查看
[root@nfs-client web]# ls
test.txt  web.txt
[root@nfs-client web]# cat test.txt 
nfs-server write data test

备注:

当在服务器运行df -h 卡死的时候,很有可能是nfs的原因。

1、在客户端找到挂载的服务器的ip及挂载目录

[root@nfs-client web]# cat /etc/fstab

2、进入服务器查看/etc/export查看服务器都挂载了那些客户端或通过 showmount -e 服务端IP 来查看客户端挂载的目录。

3、重启nfs

[root@nfs-server web]# systemctl restart nfs

4、现在客户端就可以操作了,先卸载之前的挂载

umount   /data/app
umount   /data/web

5、重新挂载

mount -t nfs 服务器IP:/服务器目录 客户端挂载目录

FAQ

Centos 7下挂载nfs 提示mount.nfs: an incorrect mount option was specified

mount -t nfs -o nfsvers=3,vers=3 10.10.200.227:/home/nfs /mnt/

Linux umount设备时出现device is busy解决方法

#/u06为挂载目录
[root@DB-Server u06]# fuser -m  /u06
/u06:                10584
[root@DB-Server u06]# kill -9 10584
[root@DB-Server ~]#  umount /dev/VolGroup03/LogVol00
posted @ 2023-05-16 11:42  jluo123  阅读(9)  评论(0编辑  收藏  举报