环境信息
一台Cent OS(release 7.8.2003),一台Ubuntu(22.04.5 LTS)
服务端配置
1. 安装 NFS 服务包
- CentOS
yum install -y nfs-utils rpcbind
- Ubuntu
apt update && apt install -y nfs-kernel-server rpcbind
2. 创建共享目录
mkdir -p /data/nfs_share # 自定义共享目录
chmod -R 777 /data/nfs_share # 赋予权限(生产环境按需调整)
chown -R nobody:nobody /data/nfs_share # NFS默认匿名用户
3. 配置 NFS 共享权限
- 编辑 NFS 配置文件/etc/exports
vim /etc/exports
# 格式:共享目录 客户端地址(权限参数)
/data/nfs_share 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)
# 参数说明
rw:读写权限;ro:只读。
sync:同步写入(数据实时写入磁盘,推荐)。
no_root_squash:客户端 root 用户映射为服务端 root(慎用,生产环境建议root_squash)。
no_all_squash:保留客户端用户权限。
客户端地址可填具体 IP(如192.168.1.100)、网段(如192.168.1.0/24)或*(所有客户端)。
4. 生效配置并启动服务
- CentOS
# 重新加载exports配置
exportfs -r
# 启动并开机自启服务
# CentOS 7/8
systemctl start rpcbind nfs-server
systemctl enable rpcbind nfs-server
# 查看共享状态
exportfs -v
- Ubuntu
# 重新加载exports配置
exportfs -r
# 启动并开机自启服务
# Ubuntu/Debian
systemctl start rpcbind nfs-kernel-server
systemctl enable rpcbind nfs-kernel-server
# 查看共享状态
exportfs -v
5. 开放防火墙(按需)
- CentOS
firewall-cmd --add-service=nfs --permanent
firewall-cmd --add-service=rpc-bind --permanent
firewall-cmd --add-service=mountd --permanent
firewall-cmd --reload
- Ubuntu
ufw allow nfs
ufw allow rpc-bind
ufw allow mountd
ufw reload
客户端配置
1. 安装 NFS 客户端工具
- CentOS
yum install -y nfs-utils rpcbind
- Ubuntu
apt install -y nfs-common rpcbind
2. 创建挂载点
mkdir -p /data/nfs_mount # 自定义挂载目录
3. 挂载 NFS 共享目录
mount -t nfs 服务端IP:/data/nfs_share /data/nfs_mount
4. 验证挂载
df -h # 查看挂载状态
touch /mnt/nfs_mount/test.txt # 测试写入权限
5. 设置开机自动挂载
- 编辑/etc/fstab文件
vim /etc/fstab
服务端IP:/data/nfs_share /data/nfs_mount nfs defaults 0 0
6. 测试挂载并生效
mount -a # 验证配置无错误