目录
一、前言
NFS(Network File System) 是Linux/Unix系统中实现跨主机文件共享的核心协议,广泛应用于集群存储、数据同步等场景。本文基于RHEL 9,通过两台服务器(服务端 nfs-server
和客户端 nfs-client
),从零开始演示NFS的安装、配置、挂载及验证过程,涵盖常见问题解决和安全优化建议。
二、环境准备与网络配置
角色 | IP | 主机名 | 系统 |
---|---|---|---|
服务器 | 192.168.72.114 | nfs-server | RHEL 9 |
客户端 | 192.168.72.113 | nfs-client | RHEL 9 |
三、基础配置
1. 配置本地Yum仓库(服务端)
由于内网环境需手动挂载ISO镜像并配置仓库:
# 挂载ISO镜像
[root@nfs-server ~]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
# 创建本地仓库文件
[root@nfs-server ~]# vi /etc/yum.repos.d/dnf.repo
[root@nfs-server ~]# cat /etc/yum.repos.d/dnf.repo
[BaseOS]
name=BaseOS
baseurl=file:///mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///mnt/AppStream
gpgcheck=0
2. 同步仓库到客户端
# 使用scp传输仓库文件(需确保SSH服务已启动)
[root@nfs-server ~]# scp /etc/yum.repos.d/dnf.repo root@192.168.220.157:/etc/yum.repos.d/
The authenticity of host '192.168.220.157 (192.168.220.157)' can't be established.
ED25519 key fingerprint is SHA256:gjYume+iYY6V+DNR4fgifdVAKrC8sz0x4ZcnlpgIkrk.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.220.157' (ED25519) to the list of known hosts.
root@192.168.220.157's password:
dnf.repo 100% 125 286.1KB/s 00:00
3. 配置主机名与静态IP
先安装基本软件,bash-completion 用于tab补全,如果拥有软件则跳过此步骤。
[root@localhost ~]# dnf install net-tools wget curl bash-completion vim -y
- 服务端:
# 设置主机名
[root@nfs-server ~]# hostnamectl set-hostname nfs-server
# 配置静态IP
[root@nfs-server ~]# nmcli c modify ens160 \
ipv4.method manual \
ipv4.addresses 192.168.220.114/24 \
ipv4.gateway 192.168.220.2 \
connection.autoconnect yes
[root@nfs-server ~]# nmcli c up ens160
- 客户端:
# 设置主机名
[root@nfs-client ~]# hostnamectl set-hostname nfs-client
# 配置静态IP
[root@nfs-client ~]# nmcli c modify ens160 \
ipv4.method manual \
ipv4.addresses 192.168.220.113/24 \
ipv4.gateway 192.168.220.2 \
connection.autoconnect yes
[root@nfs-client ~]# nmcli c up ens160
4. 验证网络连通性
确保两台机器之间网络互通,禁用防火墙临时测试(生产环境请谨慎):
# 从客户端ping服务端
[root@nfs-client ~]# ping 192.168.220.114
# 从服务端ping客户端
[root@nfs-server ~]# ping 192.168.220.113
四、服务端配置
1. 安装NFS服务
RHEL 9默认使用NFSv4,安装 nfs-utils
包,安装前记得挂载:
# 挂载ISO镜像并安装nfs-utils
[root@nfs-server ~]# mount /dev/sr0 /mnt
[root@nfs-server ~]# dnf install nfs-utils -y
2. 创建共享目录
[root@nfs-server ~]# mkdir -p /nfs/data
[root@nfs-server ~]# chmod 777 /nfs/data # 临时放宽权限(生产环境需细化权限)
3. 配置NFS共享规则
编辑 /etc/exports
文件:
[root@nfs-server ~]# vi /etc/exports
/nfs/data 192.168.220.113(rw,sync,no_root_squash,no_subtree_check)
-
参数说明:
-
rw
:允许读写 -
sync
:同步写入磁盘 -
no_root_squash
:客户端root用户保留权限(慎用) -
no_subtree_check
:禁用子目录检查(提升性能)
-
4. 启动服务并配置防火墙
# 启动NFS服务
[root@nfs-server ~]# systemctl enable --now nfs-server
# 放行防火墙规则
[root@nfs-server ~]# firewall-cmd --permanent --add-service={nfs,rpc-bind,mountd}
[root@nfs-server ~]# firewall-cmd --reload
5. 验证共享配置
# 查看共享列表
[root@nfs-server ~]# showmount -e 192.168.220.114
Export list for 192.168.220.114:
/nfs/data 192.168.220.113
五、客户端配置
1. 安装NFS客户端工具
[root@nfs-client ~]# dnf install nfs-utils -y
2. 创建本地挂载点
[root@nfs-client ~]# mkdir -p /data/nfs
3. 手动挂载NFS共享
[root@nfs-client ~]# mount -t nfs -o vers=4 192.168.220.114:/nfs/data /data/nfs
4. 验证挂载状态
[root@nfs-client ~]# df -hT /data/nfs
Filesystem Type Size Used Avail Use% Mounted on
192.168.220.114:/nfs/data nfs4 45G 1.7G 43G 4% /data/nfs
六、功能测试
1. 客户端读取服务端文件
# 服务端写入测试文件
[root@nfs-server ~]# echo "Hello from NFS Server" > /nfs/data/test.txt
# 客户端读取文件
[root@nfs-client ~]# cat /data/nfs/test.txt
Hello from NFS Server
2. 客户端写入文件到服务端
# 客户端写入文件
[root@nfs-client ~]# echo "This is a client test" > /data/nfs/client_test.txt
# 服务端验证
[root@nfs-server ~]# ls /nfs/data
client_test.txt test.txt
七、安全加固与排错
1. SELinux策略调整
# 临时禁用SELinux(测试用)
[root@nfs-server ~]# setenforce 0
# 永久允许NFS读写(生产环境推荐)
[root@nfs-server ~]# setsebool -P nfs_export_all_rw 1
2. 权限问题排查
若客户端无法写入,检查服务端目录权限:
[root@nfs-server ~]# chmod 777 /nfs/data # 临时解决方案
# 生产环境建议使用ACL或专用用户/组管理权限
3. 防火墙与端口验证
# 检查NFS相关端口是否开放
[root@nfs-server ~]# ss -tnlp | grep -E '2049|111|20048'
4. 开机自动挂载
编辑客户端 /etc/fstab
:
192.168.220.114:/nfs/data /data/nfs nfs defaults,_netdev,vers=4 0 0
应用配置:
[root@nfs-client ~]# mount -a
八、总结
本文通过实战演示了RHEL 9上NFS服务的完整配置流程,涵盖网络配置、服务部署、权限管理及安全加固。NFS作为经典的网络文件共享方案,在跨服务器数据同步、分布式存储等场景中具有重要作用。实际部署时需结合业务需求细化权限策略,并做好日志监控与定期维护。
附录:常用命令速查表
功能 | 命令 |
---|---|
查看NFS共享列表 | showmount -e <IP> |
重新加载NFS配置 | exportfs -ra |
强制卸载NFS目录 | umount -f -l /data/nfs |
查看NFS连接状态 | nfsstat -m |
原创声明
本文为优快云独家原创内容,转载请注明出处。技术交流与问题反馈请私信作者!