Ubuntu 挂载 NFS 共享文件夹步骤
一 客户端快速挂载
- 安装 NFS 客户端工具(若尚未安装):sudo apt update && sudo apt install -y nfs-common。
- 创建本地挂载点:sudo mkdir -p /mnt/nfs。
- 执行挂载:sudo mount -t nfs <NFS服务器IP>:<共享路径> /mnt/nfs。示例:sudo mount -t nfs 192.168.1.100:/shared_folder /mnt/nfs。
- 验证:df -h 应能看到类似 “192.168.1.100:/shared_folder on /mnt/nfs” 的条目。
- 卸载:sudo umount /mnt/nfs。
二 开机自动挂载
- 编辑 /etc/fstab,在末尾添加一行:
<NFS服务器IP>:<共享路径> /mnt/nfs nfs defaults 0 0
示例:192.168.1.100:/shared_folder /mnt/nfs nfs defaults 0 0。
- 测试 fstab 配置是否正确:sudo mount -a(无报错即正常)。
- 如需网络就绪后再挂载,可改用 systemd 挂载单元(推荐用于桌面/笔记本或网络不稳定的环境)。
三 常用挂载选项与性能调优
- 常用选项(可组合使用):
- rw/ro:读写/只读。
- sync:同步写入,数据安全但性能略低;async:异步写入,性能更高但有数据丢失风险。
- no_subtree_check:关闭子树检查,提升稳定性与性能。
- 用户映射:root_squash(默认,将 root 映射为匿名)、no_root_squash(保留 root,慎用)、all_squash(全部映射为匿名),常与 anonuid/anongid 指定映射到的 UID/GID。
- 示例(在 mount 命令中使用 -o 指定):
sudo mount -t nfs -o rw,sync,no_subtree_check 192.168.1.100:/shared /mnt/nfs。
四 服务端要点与防火墙(如你同时负责 NFS 服务器)
- 安装并共享目录:
- 安装:sudo apt install -y nfs-kernel-server。
- 共享示例:编辑 /etc/exports,添加
/srv/nfs/shared 192.168.0.0/24(rw,sync,no_subtree_check)。
- 使配置生效并重启服务:sudo exportfs -a && sudo systemctl restart nfs-kernel-server && sudo systemctl enable nfs-kernel-server。
- UFW 防火墙放行(示例仅放行内网 192.168.0.0/24):
sudo ufw allow from 192.168.0.0/24 to any port nfs
sudo ufw allow from 192.168.0.0/24 to any port 111
sudo ufw enable。
五 排错清单
- 服务器导出检查:在服务器上执行 showmount -e,应能看到导出的目录与允许的网络。
- 连通与端口:确认客户端能访问服务器的 NFS(2049) 与 rpcbind(111) 端口(如使用 UFW 按上节放行)。
- 权限与映射:检查 /etc/exports 的客户端网段、rw/ro、以及 root_squash/all_squash 与 UID/GID 映射是否与客户端用户一致。
- 挂载点状态:确保挂载点目录存在且未被占用;必要时用 mount/df -h 查看是否已挂载或占用。
- 系统日志:在服务器端查看 /var/log/syslog 中与 NFS 相关的错误提示以定位问题。