1、概述
NFS(Network File System):是在 UNIX(Linux)系统中最流行的网络文件系统,其功能是通过网络让不同的机器(或操作系统)可以共享彼此的文件或目录。
2、相关依赖组件
在使用 NFS 之前,服务端(提供共享文件或目录的一方)和客户端(使用共享文件或目录的一方)都需要先安装 rpcbind 和 nfs-utils 依赖组件。
2.1、确认已安装rpcbind
rpm -qa | grep rpcbind
如果还没安装 rpcbind 依赖组件,请参考 CentOS-7离线安装rpcbind 。
2.2、确认已安装nfs-utils
rpm -qa | grep nfs-utils
如果还没安装 nfs-utils 依赖组件,请参考 CentOS-7离线安装nfs-utils 。
注意:在安装 nfs-utils 依赖组件之前,必须先安装 rpcbind 依赖组件;一般来说,CentOS-7 已默认安装 rpcbind 依赖组件,只需要 nfs-utils 依赖组件即可。
3、服务端操作
3.1、创建共享目录
如上图所示,(本例)在服务端(提供共享文件或目录的一方)的根目录“/”,创建了一个目录,用于和客户端共享。
mkdir 共享目录名称
3.2、编辑配置文件
vi /etc/exports
共享目录路径 *(rw,no_root_squash,insecure,sync)
注意:如果需要进行网段限制,“*”改为对应网段,例如:
共享目录路径 192.168.56.0/24(rw,no_root_squash,insecure,sync)
参数 | 说明 |
---|---|
rw | 共享目录的访问权限为可读可写(read-write),实际权限还和文件系统的 rwx 权限以及用户身份有关 |
ro | 共享目录的访问权限为只读(read-only) |
no_root_squash | 允许客户端以 root 身份访问共享目录 |
root_squash | 客户端的 root 身份会被切换成和 anonymous 匿名用户身份一样的权限 |
insecure | 允许 nfs 服务使用比 1024 更大的端口号 |
secure | nfs 服务只能使用 1024 以内的端口号 |
sync | 数据同步写入到内存与硬盘中 |
async | 数据先暂存在内存当中,不直接写入硬盘 |
3.3、确认rpcbind服务已开启
systemctl status rpcbind
命令 | 说明 |
---|---|
systemctl status rpcbind | 查看 rpcbind 服务运行状态 |
systemctl start rpcbind | 启动 rpcbind 服务 |
systemctl restart rpcbind | 重启 rpcbind 服务 |
systemctl enable rpcbind | 设置 rpcbind 服务开机自启动 |
3.4、重启nfs服务
systemctl restart nfs
命令 | 说明 |
---|---|
systemctl status nfs | 查看 nfs 服务运行状态 |
systemctl start nfs | 启动 nfs 服务 |
systemctl restart nfs | 重启 nfs 服务 |
systemctl enable nfs | 设置 nfs 服务开机自启动 |
3.5、开启防火墙
firewall-cmd --add-service=nfs --permanent
firewall-cmd --reload
3.6、相关监测命令
命令 | 说明 |
---|---|
nfsstat | 查看NFS的运行状态 |
rpcinfo | 查看rpc执行信息,可以用于检测rpc运行情况的工具,利用rpcinfo -p 可查看出RPC开启的端口所提供的程序 |
4、客户端操作
4.1、确认rpcbind服务已开启
systemctl status rpcbind
命令 | 说明 |
---|---|
systemctl status rpcbind | 查看 rpcbind 服务运行状态 |
systemctl start rpcbind | 启动 rpcbind 服务 |
systemctl restart rpcbind | 重启 rpcbind 服务 |
systemctl enable rpcbind | 设置 rpcbind 服务开机自启动 |
4.2、启动nfs服务
systemctl start nfs
命令 | 说明 |
---|---|
systemctl status nfs | 查看 nfs 服务运行状态 |
systemctl start nfs | 启动 nfs 服务 |
systemctl restart nfs | 重启 nfs 服务 |
systemctl enable nfs | 设置 nfs 服务开机自启动 |
4.3、创建共享目录挂载点
如上图所示,(本例)在客户端(使用共享文件或目录的一方)的“/root”目录下,创建了一个目录,用于挂载服务端的共享目录。
mkdir 共享目录挂载点名称
4.4、挂载服务端共享目录
mount -t nfs 服务端IP地址:/服务端共享目录路径 客户端挂载点路径
注意:如果(普通用户)挂载共享目录时报错“mount.nfs: failed to apply fstab options”,则需要使用 sudo 权限执行该挂载命令。
4.5、确认挂载情况
如上图所示,已实现服务器之间的文件(目录)共享。
df -h
4.6、解除挂载的命令
umount 客户端挂载点路径