目录
目录X
Linux(银河麒麟)安装 NFS 并实现目录共享
AI摘要(BLUF)
场景:A服务器:18.62.1.51(客户端)B服务器:18.62.1.53(服务端)共享目录:/data/upload保留:www-data 权限一、安装 NFS(A、B 两台都执行)1️⃣ 在线安装yuminstall-ynfs-utilsrpcbind2️⃣ 离线安装准备:nfs-utilsrpcbind相关依赖rpm安装:yumlocalinstall-y*.rpm二、统一 www-data 用户(非常重要)NFS 只认 UID/GID,不认用户名。1️⃣ A服务器查看 UID/GIDidwww-data例如:uid=1001(www-data)gid=1001(www-data)2️……
场景:
A服务器:18.62.1.51(客户端)
B服务器:18.62.1.53(服务端)
共享目录:/data/upload
保留:www-data 权限
一、安装 NFS(A、B 两台都执行)
1️⃣ 在线安装
yum install -y nfs-utils rpcbind
2️⃣ 离线安装
准备:
nfs-utils
rpcbind
相关依赖rpm
安装:
yum localinstall -y *.rpm
二、统一 www-data 用户(非常重要)
NFS 只认 UID/GID,不认用户名。
1️⃣ A服务器查看 UID/GID
id www-data
例如:
uid=1001(www-data) gid=1001(www-data)
2️⃣ B服务器创建相同用户
groupadd -g 1001 www-data
useradd -u 1001 -g 1001 www-data
三、B服务器(18.62.1.53)配置共享
1️⃣ 创建共享目录
mkdir -p /data/upload
chown -R www-data:www-data /data/upload
chmod -R 755 /data/upload
2️⃣ 配置共享
编辑:
vi /etc/exports
添加:
/data/upload 18.62.1.51(rw,sync,no_root_squash,no_subtree_check)
参数说明:
| 参数 | 作用 |
|---|---|
| rw | 可读写 |
| sync | 同步写入 |
| no_root_squash | 保留root权限 |
| no_subtree_check | 提升稳定性 |
四、启动 NFS 服务(B服务器)
systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind
systemctl start nfs-server
生效配置
exportfs -r
查看:
exportfs -v
五、防火墙配置(推荐,不关闭防火墙)
1️⃣ 固定 NFS 端口
编辑:
vi /etc/nfs.conf
添加:
[mountd]
port=20048
[nfsd]
port=2049
[statd]
port=662
outgoing-port=2020
[lockd]
port=32803
udp-port=32769
2️⃣ 重启 NFS
systemctl restart rpcbind
systemctl restart nfs-server
3️⃣ 放行防火墙端口
firewall-cmd --permanent --add-port=111/tcp
firewall-cmd --permanent --add-port=111/udp
firewall-cmd --permanent --add-port=2049/tcp
firewall-cmd --permanent --add-port=2049/udp
firewall-cmd --permanent --add-port=20048/tcp
firewall-cmd --permanent --add-port=20048/udp
firewall-cmd --permanent --add-port=32803/tcp
firewall-cmd --permanent --add-port=32769/udp
firewall-cmd --permanent --add-port=662/tcp
firewall-cmd --permanent --add-port=662/udp
firewall-cmd --reload
六、A服务器(18.62.1.51)挂载共享
1️⃣ 创建挂载目录
mkdir -p /data/upload
2️⃣ 挂载 NFS
推荐 NFSv4:
mount -t nfs4 18.62.1.53:/data/upload /data/upload
或:
mount -t nfs 18.62.1.53:/data/upload /data/upload
3️⃣ 验证
df -h
测试:
su -s /bin/bash www-data
touch /data/upload/test.txt
七、开机自动挂载(A服务器)
编辑:
vi /etc/fstab
添加:
18.62.1.53:/data/upload /data/upload nfs defaults 0 0
测试:
mount -a
八、常用排查命令
查看共享
客户端:
showmount -e 18.62.1.53
查看NFS端口
服务端:
rpcinfo -p
查看挂载
mount | grep nfs
查看服务状态
systemctl status nfs-server
systemctl status rpcbind
九、最终架构
B服务器(18.62.1.53)
└── /data/upload
└── www-data:www-data
A服务器(18.62.1.51)
└── /data/upload
└── NFS挂载目录
十、一句话总结
B服务器:
安装NFS → 配置exports → 启动服务 → 放行端口
A服务器:
安装NFS → 挂载共享 → 配置fstab
关键:
www-data 的 UID/GID 必须一致