配置NFS 共享

一: NFS 共享存储概述



1.1 什么是NFS 共享存储

1.1.1 定义

NFS 是一种基于TCP/IP 传输的网络文件系统协议。

1.1.2 特点

优点:

通过使用NFS 协议,客户机可以像访问本地目录一样访问远程服务器中的共享资源。

对于大多数负载均衡群集来说,使用NFS协议来共享数据存储是比较常见的做法,NFS也是NAS存储设备必然支持的一种协议。

缺点:

但是由于NFS没有用户认证机制,而且数据在网络上明文传输,所以安全性很差,一般只能在局域网中使用

1.1.3 使用要求

NFS 服务的实现依赖于 RPC (Remote Process Call,远端过程调用)机制,以完成远程到本地的映射过程。

所以需要安装 nfs-utils、rpcbind 软件包来提供 NFS共享服务,前者用于 NFS 共享发布和访问,后者用于 RPC 支持。

下载安装 nfs-utils , rpcbind 软件包

服务: nfs , rpcbind

1.1.4 配置文件与配置格式

NFS 的配置文件为/etc/exports

格式为∶

共享的目录位置 客户机地址(权限选项)

1.2 原理

image-20210706101350861



二:服务端配置 NFS 共享存储服务



2.1 安装 nfs-utils , rpcbind 软件包

[root@host102 ~]# rpm -q rpcbind nfs-utils 
rpcbind-0.2.0-42.el7.x86_64
nfs-utils-1.3.0-0.48.el7.x86_64

image-20210706103150348



2.2 设置共享目录

[root@host102 ~]# mkdir -p /opt/share
[root@host102 ~]# chmod 777 /opt/share

image-20210706103712163



2.3 修改nfs 的配置文件,发布共享目录

[root@host102 ~]# vim /etc/exports
/opt/share     192.168.23.0/24(rw,sync,no_root_squash)  192.168.1.0/24(r0)

image-20210706105850795



权限选项 说明
rw 允许读写
ro 只读
sync 表示同步写入到内存与硬盘中
async 异步,先将数据写入到内存,再将数据写入到硬盘
no_root_squash 表示当客户机以root 身份访问时赋予本地root 权限(默认)
root_squash 表示客户机用root 用户访问该共享目录时,将root用户映射成匿名用户
anonuid=xxxx 指定NFS 服务器/etc/passwd 文件中匿名用户的UID
anongid=xxxx 指定NFS 服务器 /etc/passwd 文件中匿名用户的GID



2.4 启动NFS 服务

启动NFS 服务时,要先启动 rpcbind ,再启动 nfs

[root@host102 ~]# systemctl start rpcbind
[root@host102 ~]# systemctl start nfs
[root@host102 ~]# systemctl enable rpcbind  nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@host102 ~]#

image-20210706112251520



[root@host102 ~]# netstat  -antp | grep ":111"
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd     

image-20210706112522392



2.5 发布NFS 共享目录

NFS共享,在启动时,就是自动发布,但是也可以使用 exportfs手动发布

[root@host102 ~]# exportfs -rv

image-20210706113111126

查看发布的共享:

[root@host102 ~]# showmount -e

image-20210706113329008



三:客户端挂载使用NFS

3.1 客户机安装 nfs-utils , rpcbind 软件包,并启动服务

[root@host13 ~]# rpm -q rpcbind nfs-utils
rpcbind-0.2.0-42.el7.x86_64
nfs-utils-1.3.0-0.48.el7.x86_64

image-20210706113746537



启动rpcbind 服务,并设置开机自启

[root@host13 ~]# systemctl start rpcbind
[root@host13 ~]# systemctl enable rpcbind



3.2 手动挂载NFS 共享目录

3.2.1先使用 showmount 查看 目标服务器共享了哪些目录

[root@host13 ~]# showmount -e 192.168.23.102

image-20210706114338876



3.2.2挂载

[root@host13 ~]# mkdir /myshare
[root@host13 ~]# mount 192.168.23.102:/opt/share   /myshare

image-20210706114626048



3.2.3查看挂载

[root@host13 ~]# df -hT

image-20210706114825233



3.3 自动挂载

[root@host13 ~]# vim /etc/fstab

image-20210706115248596

[root@host13 ~]# mount -a

3.4 强制卸载

如果服务器端NFS服务突然间停掉了,而客户端正在挂载使用时,在客户端就会出现执行 df -h命令卡死的现象。

这个时候直接使用umount 命令是无法直接卸载的,需要加上 -lf 选项才能卸载。

umount -lf /myshare

posted @ 2021-07-06 12:01  知己一语  阅读(622)  评论(0编辑  收藏  举报