在 CentOS 系统上安装和配置 NFS(网络文件系统)服务,可以按照以下步骤进行:
NFS服务器端设置
1. 安装NFS服务

使用yum
命令安装nfsutils
包,这个包包含了 NFS 服务器所需的工具和守护进程。rpcbind
作为依赖项会自动安装。
- sudo yum install y nfsutils
2. 配置共享目录
在服务器上创建一个用于共享的目录,例如/data/share
,并设置适当的权限。
- sudo mkdir p /data/share
- sudo chmod 755 /data/share
编辑/etc/exports
文件,添加共享目录的配置,将/data/share
目录共享给所有 IP 地址,并设置读写权限。
- echo "/data/share *(rw,sync,no_root_squash,no_all_squash)" | sudo tee a /etc/exports
解释各参数含义:
参数 | 说明 |
rw | 读写权限 |
sync | 数据同步写入内存和硬盘 |
no_root_squash | 允许 root 用户保留 root 权限 |
no_all_squash | 禁止将所有用户映射为匿名用户 |
保存配置文件后,执行以下命令使配置立即生效。

- sudo exportfs r
3. 配置防火墙
如果系统开启了防火墙,需要允许相关端口通过,可以使用firewalld
管理防火墙规则。
- sudo firewallcmd permanent addservice=nfs addservice=mountednfs addservice=rpcbind
- sudo firewallcmd reload
或者手动开放 NFS 服务的固定端口:
- sudo firewallcmd zone=public addport=111/tcp addport=111/udp addport=2049/tcp addport=2049/udp permanent
- sudo firewallcmd reload
4. 启动并启用 NFS 服务
启动rpcbind
和nfs
服务,并设置它们开机自启。
- sudo systemctl start rpcbind
- sudo systemctl start nfs
- sudo systemctl enable rpcbind
- sudo systemctl enable nfs
检查 NFS 服务状态,确保服务正常运行。

- sudo systemctl status nfs
NFS客户端设置
1. 安装NFS客户端工具
在客户端机器上,同样需要安装nfsutils
包,不过,客户端只需安装rpcbind
服务,无需启动它。
- sudo yum install y nfsutils
2. 挂载远程 NFS 共享
查看服务器上已共享的目录:
- showmount e <NFS服务器IP>
在客户端创建挂载点目录,例如/mnt/share
,然后挂载远程共享目录。
- sudo mkdir p /mnt/share
- sudo mount t nfs <NFS服务器IP>:/data/share /mnt/share
验证挂载是否成功:
- df h | grep nfs
如果需要在系统启动时自动挂载 NFS 共享,可以将挂载信息添加到/etc/fstab
文件中。
- echo "<NFS服务器IP>:/data/share /mnt/share nfs defaults 0 0" | sudo tee a /etc/fstab
常见问题解答(FAQs)
1. 问题:NFS服务器无法挂载共享目录?
答案:可能原因包括:
/etc/exports
配置文件有误,检查语法和路径。
NFS 服务未启动或启动失败,使用systemctl status nfs
检查服务状态。
防火墙阻止了 NFS 服务的端口,确保防火墙配置正确。
共享目录的权限设置不正确,确保目录对 NFS 服务用户可访问。
2. 问题:NFS客户端无法挂载远程共享?
答案:可能原因包括:
NFS 服务器未正确配置或未启动 NFS 服务。
客户端与服务器之间的网络连接问题,检查网络连通性。
/etc/fstab
(如果使用了自动挂载)中的配置错误,确保路径和选项正确。
NFS 版本不兼容,尝试指定 NFS 版本(如vers=3
)。