# NFS 共享服务端与客户端 ## 场景 多台 Linux 服务器要共享一个目录:日志汇总、备份落地、应用上传目录。 ## 前置条件 - 服务端有独立的数据盘 - 客户端和服务端网段明确 - 客户端能访问服务端的 2049 ## 配置 ### 服务端 ```bash dnf install -y nfs-utils mkdir -p /data/share chown nobody:nobody /data/share cat >>/etc/exports <<'EOF' /data/share 10.0.0.0/24(rw,sync,no_subtree_check) EOF exportfs -ra systemctl enable --now nfs-server firewall-cmd --permanent --add-service=nfs firewall-cmd --permanent --add-service=rpc-bind firewall-cmd --reload ``` ### 客户端 ```bash dnf install -y nfs-utils mkdir -p /mnt/share mount -t nfs 10.0.0.5:/data/share /mnt/share # 开机自动挂载 echo '10.0.0.5:/data/share /mnt/share nfs defaults,_netdev 0 0' >> /etc/fstab mount -a ``` ## 验证 ```bash showmount -e 10.0.0.5 mount | grep share touch /mnt/share/test && rm /mnt/share/test df -h /mnt/share ``` ## 回滚 客户端: ```bash umount /mnt/share sed -i '/\/mnt\/share/d' /etc/fstab ``` 服务端: ```bash sed -i '/\/data\/share/d' /etc/exports exportfs -ra ``` ## 坑 - **NFS 服务端一挂,客户端访问该目录的进程会卡死**,要加 `hard`/`soft` 和 `timeo` 参数。 - `no_root_squash` 省事但危险,客户端 root 能改服务端文件属主。 - 防火墙只放 nfs 不够,还要 rpc-bind(新版本 nfsv4 只用 2049)。 - fstab 不写 `_netdev`,开机会因为网络没起来而卡住。 - 小文件多的场景 NFS 性能很差,考虑本地盘 + rsync。 - 客户端和服务端 UID/GID 不一致,权限会错乱。