Skip to content

挂载网络文件系统(NFS)

NFS 文件系统挂载

Liunx 挂载配置

RedHat-官方文档/Autofs 红帽官方推荐使用

1. 安装 autofs

确保本地已经安装了 autofs,如果未安装,可以使用以下命令安装:

sudo apt update && sudo apt install autofs -y

2. 编辑 autofs 配置

autofs 的配置文件通常位于 /etc/auto.master。在文件末尾添加一个条目,指定挂载点和映射文件,例如:

sudo nano /etc/auto.master
/mnt/nfs /etc/auto.nfs --timeout=60 # 这种配置只能挂载到 /mnt/nfs 目录下

/- /etc/auto.direct.nfs --timeout=60 # 挂载到文件系统任意目录

# 下文使用此种方式,上面两种参考官方文档即可
/- /etc/auto.direct.nfs   --timeout=300,-fstype=nfs4,soft,timeo=50,retrans=2 # 挂载到任意目录
  • /mnt/nfs: 本地挂载点的根目录。
  • /etc/auto.nfs: 定义 NFS 挂载配置的映射文件。
  • --timeout=60: 设定 60 秒的空闲时间后自动卸载。

3. 创建映射文件

/etc/auto.nfs 中添加如下内容:

/mnt/nfs  192.168.x.x:/home/nfsshare  # 不提供-fstype 参数使用 auto.master文件中默认的
/mnt/nfs -fstype=nfs,rw,sync 192.168.x.x:/home/nfsshare # 提供则不再使用默认值
  • user: 本地挂载目录名称。
  • -fstype=nfs,rw,sync: 指定挂载参数,rw 为读写,sync 为同步写入。
  • 192.168.x.x:/home/nfsshare: NFS 服务器的 IP 和导出的目录。

192.168.x.x 替换为 NFS 服务器的实际 IP 地址。

4. 创建挂载点

autofs 按需挂载,因此你需要创建挂载点的根目录,例如:

sudo mkdir -p /mnt/nfs

挂载后,/mnt/nfs 就是实际挂载点。

5. 启动 autofs 服务

重启或启动 autofs 服务使配置生效:

sudo systemctl restart autofs

6. 验证挂载

尝试访问挂载点 /mnt/nfs/autofs 会自动挂载 NFS 目录:

ls /mnt/nfs/

如果配置正确,NFS 目录内容将显示在本地挂载点。

注意事项

  • 确保 NFS 服务器的 /etc/exports 文件中正确配置了导出路径和权限。
  • 确认本地网络连接以及 NFS 服务状态。
  • autofs 支持自动卸载,可以通过增加超时时间或频繁访问避免频繁挂载和卸载。

如果遇到问题,可查看日志获取详细信息:

journalctl -u autofs

Windows 挂载配置

微软官方文档-mount windwows 可以不依赖第三方软件进行挂载

1. 开启系统NFS功能

打开程序和功能 (按下win 键 搜索 程序和功能

|550

注册表写入 匿名用户ID

在注册表

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default

添加 AnonymousUid、AnonymousGid 两项

参考: https://blog.csdn.net/zywvvd/article/details/113753323

如果不这么做,会没有权限访问对应目录

开启utf8支持

参考: https://blog.csdn.net/baidu_41909866/article/details/116305972

如果不开启 此项,对于中文文件夹会显示乱码

2. 挂载

使用 cmd 执行:

mount -o fileaccess=7 \\192.168.0.10\home\user\test1\common *

Windwos 不支持 挂载到独立的目录,只能以硬盘盘符的形式存在,如需自定义盘符 可 将 * 替换为 Z:

如果挂载失效 或不再挂载 在 磁盘上右键 卸载即可