Debian NFS Server 以及 MacOS Client

Server Side

安装必要依赖:

apt install nfs-kernel-server nfs-common

编辑配置文件: vi /etc/exports

/media/share *(ro,sync,no_root_squash,no_subtree_check,insecure)
字段
/media/share要共享的目录
*指定可访问的IP,
*表示全部来源
特定网段:192.168.123.0/24
特定IP:192.168.123.2
rw / ro读写
sync / async文件同步写入到内存与硬盘中 / 文件会先暂存于内存中
root_squash / no_root_squash- 当NFS客户端以root用户身份访问时,映射为NFS服务器的nfs nobody用户
- 当NFS客户端以root身份访问时,映射为NFS服务器的root用户
subtree_check / no_subtree_check是否强制检查父目录权限
secure / insecure是否允许客户端从大于1024的tcp/ip端口连接服务器(Finder出现“找不到服务器“的错误可以设置为insecure
anonuidanongid将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户和用户组

每次修改完 exportfs 文件,都需要应用并导出分享:exportfs -ra

流量默认从 2049 端口通过,注意放通防火墙规则

查看当前导出访问的目录情况:exportfs -v。想要了解更多可用选项,在终端输入man exports

Client Side(MacOS)

由于从 Catalina 开始,根文件系统现在是只读的。所以需要将挂载点新建在用户可写的文件系统/系统/卷/数据:mkdir -p /Users/wayjam/nfs

然后使用挂载命令: mount -t nfs 192.168.123.2:/media/share /Users/wayjam/nfs

然后发现挂载失败:

nfs  mount -t nfs  192.168.123.2:/media/share /Users/wayjam/nfs
mount_nfs: can't mount with remote locks when server (192.168.123.2) is not running rpc.statd: RPC prog. not avail nfs
mount: /Users/wayjam/nfs failed with 74

使用一些调试命令看了情况都正常

  • rpcinfo -p 192.168.123.2 查看 RPC 连接情况
  • showmount -e 192.168.123.2 显示挂载情况
  • nfsstat -m 查看 NFS 情况

后来查到是没有指定 NFS 版本,编辑配置sudo vi /etc/nfs.conf,加入

nfs.client.mount.options = vers=4

然后再使用上面的挂载命令就可以挂载成功了。或者说在每次挂载的时候指定 NFS 版本:

 mount -t nfs -o soft,nfsvers=4  192.168.123.2:/media/share /Users/wayjam/nfs

如果要使用 FInder 的“连接到服务器”来挂载,则需要按这种格式来指定 NFS 版本:

nfs://vers=4,192.168.123.2:/media/share

自动挂载

创建挂载点:mkdir /Users/wayjam/nfs,然后修改 master 映射配置 sudo vi /etc/auto_master

/Users/wayjam/nfs auto_nfs

创建 /etc/auto_nfs 映射文件:

nas_data -rw,resvport,noowners,nfsv4,soft 192.168.123.2:/media/share

然后应用配置sudo automount -vc,另外/etc/autofs.conf还有一些挂载超时时间等配置可定制。

写入权限问题

有以下几种思路

  1. 配置 no_root_squash 并且使用 root 用户写入
  2. server 端增加与 client 端相同 uid 的用户,并给共享的目录增加读写权限
  3. 配置all_squash,并通过设置ACL规则的方式赋予 nfsnobody 用户读写执行权限 setfacl -m u:nfsnobody:rw /media/share
  4. 使用非 root 用户写入,server 端给 other 加写入权限

比如使用以下配置,所有用户皆映射到 root 用户,但这样有安全风险:

/media/share *(rw,sync,all_squash,anonuid=0,anongid=0,no_subtree_check,insecure)
Published At
Tagged with
This site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at https://wayjam.me.