NFS 网络文件系统
NFS 服务端配置
服务端(CentOS/RHEL)
# 安装
yum install nfs-utils
# 配置共享目录
cat >> /etc/exports << 'EOF'
/data/shared 10.0.0.0/8(rw,sync,no_subtree_check,no_root_squash)
/data/backup 10.0.0.0/8(ro,sync,no_subtree_check)
EOF
# 启动服务
systemctl enable --now nfs-server
# 刷新配置
exportfs -rav
| 参数 | 说明 |
|---|---|
rw / ro | 读写 / 只读 |
sync | 同步写入(安全) |
no_root_squash | 客户端 root 保持 root 权限 |
root_squash | 客户端 root 映射为 nobody(默认,更安全) |
NFS 客户端挂载
# 临时挂载
mount -t nfs 10.0.1.10:/data/shared /mnt/shared
# 持久化挂载(/etc/fstab)
echo '10.0.1.10:/data/shared /mnt/shared nfs defaults,_netdev 0 0' >> /etc/fstab
# 自动挂载(autofs,按需挂载)
yum install autofs
echo '/mnt/auto /etc/auto.nfs' >> /etc/auto.master
echo 'shared -rw,soft 10.0.1.10:/data/shared' > /etc/auto.nfs
systemctl enable --now autofs
NFS 性能与可靠性
NFS 适合中小规模文件共享,但有局限:
- 单点故障:NFS 服务端宕机影响所有客户端
- 性能瓶颈:大量小文件 IO 性能差
- 无本地缓存:网络延迟直接影响读写
大规模场景推荐使用 CephFS 或云原生文件存储(EFS/NAS)。
常见面试问题
Q1: NFS mount 后客户端卡住怎么排查?
答案:
mount -o soft,timeo=10使用软挂载,超时后返回错误而非无限等待- 检查 NFS 服务端状态:
showmount -e <server> - 检查网络连通性:
telnet <server> 2049 - 检查防火墙:NFS 需要 111(rpcbind) + 2049(nfsd) 端口