Linux Server setting

MacOS nfs client is easy to have problems, both the server and client needs some special treatments.

Hereby is the file /etc/exports for MacOS client:

# /etc/exports: the access control list for filesystems which may be exported
#		to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#
# For Linux client
/mnt/hdd	192.168.1.77(rw,async,no_subtree_check,no_root_squash)
# For MacOS client
/mnt/hdd	192.168.1.201(rw,async,insecure,no_subtree_check,all_squash,anonuid=1000)

The last line is the one decide how the client can response with the server and what permissions available.

  • Option inscure is a MUST have for MacOS client.
  • Option async no_subtree_check can improve the performance
  • Option all_squash + anonuid=1000 is the trick to resolve a special problem that MacOS client has no write permission even it has option rw already.

Why MacOS client has no write permission?

Here is how I mount the NFS share in MacOS:

sudo mount -t nfs -o resvport,async,nolocks,locallocks,soft,wsize=32768,rsize=32768 192.168.1.231:/mnt/hdd /Users/jazzi/nfs

As you can see from above command, the user used for mounting is jazzi which is the same as the owner of NFS shares on Linux Server, however the user id number is different even has same name.

$id -u
501
And on Linux Server
$id -u
1000

The User jazzi on MacOS has user id number 501, this is different from what we get from Linux Server as it is number 1000.

There are three solutions to resolve this permission problem:

  1. Set permission to NFS share files on Linux Server
  2. Create an account which has user id number of 501 on Linux Server and let it be the owner of the NFS shares
  3. Squash all coming account into somebody and give it the specific user id, the id number could be 0 which represents root or 1000 which is jazzi here

NFS Shares permissions

sudo chown jazzi:jellyfin /mnt/nfs
sudo find /mnt/nfs -type d -exec chmod 775 {} \;
sudo find /mnt/nfs -type f -exec chmod 664 {} \;

On MacOS client, if show out errors as below:

% sudo mount -t nfs -o rw,async,resvport servername:/export/our /System/Volumes/Data/mnt   
mount_nfs: can't mount with remote locks when server (servername) is not running rpc.statd: RPC prog. not avail
mount: /System/Volumes/Data/mnt failed with 74

Then it might be the problem of NFS version which you should specify it clearly as below on MacOS:

sudo nano /etc/nfs.conf
#
# nfs.conf: the NFS configuration file
nfs.client.mount.options = vers=4

And then check the NFS version being using on Server

sudo nfsstat -s

Or on NFS client

sudo nfsstat -c

How to auto mount NFS on MacOS

/System/Volumes/Data/mnt/     auto_nfs

Then create file /etc/auto_nfs which contains:

Music  -rw,async,resvport,nfc,vers=4  servername:/export/music

NFS performance is very slow

Normally on Mac the speed should be over 4M/s, if it’s lower than that, there are two ways to do on the Mac client.

1. Add mount options of nolocks and locallocks

Edit /etc/auto_master to add options nolocks, locallocks to the /net entry and remove option hidefromfinder.

If you want to fix every automount, edit /etc/autofs.conf and add these two options to AUTOMOUNTD_MNTOPTS= so these options will get applied on every mount.

Then reboot and see how is your world.

This solution comes from Browing NFS shares extremely slow and hang Finder which works for me.

2. Specify wsize and rsize on mount

sudo mount server:share /mnt nfs -U,w=8388608,-P,-r=8388608
when you mount with 
-w=65536 // writebufsize
,-4 // NFS V4
,-U // UDP 
,-P // Priv Ports 
,-r=65536 // readbufsize 

you will get about 4MB/sec.

Regarding the wsize and rsize you could try 8192(8K) or 32768(32K) too.

Above solution comes from morm on macosx.

NFS Server /etc/exports Options

  • ro:共享目录只读;
  • rw:共享目录可读可写;
  • all_squash:所有访问用户都映射为匿名用户或用户组;
  • no_all_squash(默认):访问用户先与本机用户匹配,匹配失败后再映射为匿名用户或用户组;
  • root_squash(默认):将来访的root用户映射为匿名用户或用户组;
  • no_root_squash:来访的root用户保持root帐号权限;
  • anonuid=:指定匿名访问用户的本地用户UID,默认为nfsnobody(65534);
  • anongid=:指定匿名访问用户的本地用户组GID,默认为nfsnobody(65534);
  • secure(默认):限制客户端只能从小于1024的tcp/ip端口连接服务器;
  • insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
  • sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
  • async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
  • wdelay(默认):检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率;
  • no_wdelay:若有写操作则立即执行,应与sync配合使用;
  • subtree_check(默认) :若输出目录是一个子目录,则nfs服务器将检查其父目录的权限;
  • no_subtree_check :即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

NFS commands

exportfs

不重启nfs服务应用更新,相关选项如下:

  • -a 全部挂载或卸载 /etc/exports中的内容
  • -r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab
  • -u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
  • -v 在export的时候,将详细的信息输出到屏幕上。

nfsstat

查看NFS的运行状态。

rpcinfo

查看rpc服务注册情况。 相关选项:

  • -p 显示所有的端口与程序信息。

示例: rpcinfo -p localhost #列出本机的RPC注册状况。

showmount

查询nfs共享目录信息,相关选项如下:

  • -a 显示已经于客户端连接上的目录信息
  • -e IP或者hostname 显示此IP地址分享出来的目录

示例: showmount -e localhost #查询本机nfs共享目录情况 showmount -a localhost #查询本机共享目录连接情况

Options for client mounting

  • ro 以只读模式加载。
  • rw 以可读写模式加载。
  • sync 以同步方式执行文件系统的输入输出动作。
  • async 以非同步的方式执行文件系统的输入输出动作。
  • defaults 使用默认的选项。默认选项为rw、suid、dev、exec、anto nouser与async。
  • atime 每次存取都更新inode的存取时间,默认设置,取消选项为noatime。
  • noatime 每次存取时不更新inode的存取时间。
  • dev 可读文件系统上的字符或块设备,取消选项为nodev。
  • nodev 不读文件系统上的字符或块设备。
  • exec 可执行二进制文件,取消选项为noexec。
  • noexec 无法执行二进制文件。
  • auto 必须在/etc/fstab文件中指定此选项。执行-a参数时,会加载设置为auto的设备,取消选取为noauto。
  • noauto 无法使用-a参数来加载。
  • suid 启动set-user-identifier(设置用户ID)与set-group-identifer(设置组ID)设置位,取消选项为nosuid。
  • nosuid 关闭set-user-identifier(设置用户ID)与set-group-identifer(设置组ID)设置位。
  • user 普通用户可以执行加载操作。
  • nouser 普通用户无法执行加载操作,默认设置。
  • remount 重新加载设备。通常用于改变设备的设置状态。
  • rsize 读取数据缓冲大小,默认设置1024。
  • wsize 写入数据缓冲大小,默认设置1024。
  • fg 以前台形式执行挂载操作,默认设置。在挂载失败时会影响正常操作响应。
  • bg 以后台形式执行挂载操作。
  • hard 硬式挂载,默认设置。如果与服务器通讯失败,让试图访问它的操作被阻塞,直到服务器恢复为止。
  • soft 软式挂载。如果服务器通讯失败,让试图访问它的操作失败,返回一条出错消息。这项功能对于避免进程“挂”在无关紧要的安装操作上来说非常有用。
  • retrans=n 指定在以软方式安装的文件系统上,在返回一条出错消息之前重复发出请求的次数。
  • nointr 不允许用户中断,默认设置。
  • intr 允许用户中断被阻塞的操作(并且让它们返回一条出错消息)。
  • timeo=n 设置请求的超时时间(以十分之一秒为单位)。

注意automounter专用参数: fstype= 用于指定一个文件系统的类型(如果要挂载的文件系统不是NFS的话),mount是不能用的。