李守中

Linux 上的 NFS

Table of Contents

1 Linux NFS 服务端

1.1 安装服务

使用 RH 系发行版:

  1. sudo dnf install nfs-utils 安装 nfs-utils 软件包
  2. sudo systemctl enable --now nfs-server 开机自动启动服务器
  3. sudo firewall-cmd --add-service={rpc-bind,mountd,nfs} --permanent 开放端口
  4. sudo firewall-cmd --reload 重载防火墙使添加的服务生效

使用 Debian 系发行版:

  1. sudo apt install nfs-kernel-server 安装 NFS 服务端
  2. sudo systemctl enable --now nfs-server.service 启动 NFS Server 并设置开机启动

硬盘挂载方式为 /etc/systemd/system/mnt-hgst3ta.mount 服务:

[Install]
WantedBy=multi-user.target

[Mount]
Options=defaults
Type=ext4
What=/dev/disk/by-uuid/cd2949ed-9e14-4a34-a703-6dcbd761e9d3
Where=/mnt/hgst3ta

[Unit]
Description=Mount 2nd HGST 3T disk under /mnt/hgst3ta.

挂载硬盘到 /mnt/hgst3ta 后 /mnt/hgst3ta 的权限为 drwxr-xr-x. 2 root root 4.0K ...

sudo chmod 777 /mnt/hgst3ta 对所有用户开放所有权限。

配置文件 /etc/exports 的内容为 /mnt/hgst3ta (rw,sync,root_squash)

执行 sudo exportfs -ra 命令根据 /etc/exports 文件重新载入共享配置。

exportfs 常用的选项有:

  • -r: 打开或取消所有目录共享。它使 /var/lib/nfs/xtab 和 /etc/exports 同步,将 /etc/exports 中已删除的条目从 /var/lib/nfs/xtab 中删除,将内核共享表中任何不再有效的条目移除
  • -a: 根据将其它选项传递给 exportfs 导致所有目录被导出或取消导出。如果没有指定其他选项, exportfs 会导出 /etc/exports 中指定的所有文件系统
  • -o <file_systems>: 指定导出没有在 /etc/exports 中列出的目录。格式必须与在 /etc/exports 中的相同。这个选项通常用来测试导出的文件系统,然后再将其永久添加到导出的文件系统列表中
  • -i: 忽略 /etc/exports,用命令行给出的选项定义导出的文件系统
  • -u: 取消导出所有共享目录
    • exportfs -ua 命令挂起所有的 NFS 共享,同时保持所有 NFS 服务
    • exportfs -r 可新启用 NFS 共享
  • -v: 在执行 exportfs 命令时,详细显示要导出或取消导出的文件系统

1.2 exports 常用配置参数

/etc/exports 管理 NFS 的共享配置,每个条目都有以下结构:

export host(options)
export host1(options1) host2(options2) host3(options3)
export *(options)

/mnt/hgst8ta *(rw,async,root_squash,no_subtree_check)
/mnt/hgst3ta *(rw,async,root_squash,no_subtree_check)

配置的选项有:

  • ro: read-only 远程主机无法更改文件系统中共享的数据
  • rw: read-write 远程主机可以更改文件系统中共享的数据
  • sync: 同步写入。在数据写入磁盘前,NFS 服务器不返回写入成功
  • async: 异步写入。在数据写入磁盘前,NFS 服务器返回写入成功
  • wdelay: 如果 NFS 服务器预期另外一个写入请求即将发生,则 NFS 服务器会延迟写入磁盘。它减少了必须使用独立写入命令访问磁盘的次数,从而减少写入开销。要禁用此选项,指定 no_wdelay 选项,该选项仅在同时指定默认同步选项时才可用
  • root_squash 挂载 NFS 目录的用户如果是 root 那么这个用户的权限将被压缩成为匿名用户,相应的 UID 与 GID 都会变成 nobody 系统账号的身份
  • no_root_squash 挂载 NFS 目录的用户如果是 root 那么对于这个共享目录来说,挂载方具有 root 权限 这个功能会削弱服务器安全性,不建议用
  • all_squash 挂载 NFS 目录的所有用户均被压缩成为匿名用户,即以 nobody 用户的身份登录
  • anonuidanongid 明确指定 NFS 服务器上用户的 uid 和 gid,挂载方的用户必须拥有相同的 uid 和 gid 才能正常访问 NFS 目录 export host(anonuid=uid,anongid=gid)
  • secure 限制挂载方只能从小于 1024 的 tcp/ip 端口连接 NFS 服务器 (默认设置),改为 insecure 允许客户端从大于 1024 的 tcp/ip 端口连接服务器
  • subtree_check 若输出目录是一个子目录,则 NFS 服务器将检查其父目录的权限
  • no_subtree_check 即使输出目录是一个子目录,NFS 服务器也不检查其父目录的权限

如果服务器支持 NFSv3 则客户端使用 sudo showmount --exports <server-ip> 查看有哪些共享目录。

如果服务器支持 NFSv4 则客户端可以直接挂载根目录后进行查找:

mount <my-server>:/ /mnt/
ls /mnt/

1.3 仅开启 NFSv4

NFSv4 只需要 TCP/UDP 2049 一个端口,也不需要 portmap 这个包。

首先把 /etc/default/nfs-common 内容修改为:

# If you do not set values for the NEED_ options, they will be attempted
# autodetected; this should be sufficient for most people. Valid alternatives
# for the NEED_ options are "yes" and "no".

# Do you want to start the statd daemon? It is not needed for NFSv4.
NEED_STATD="no"

# Options for rpc.statd.
#   Should rpc.statd listen on a specific port? This is especially useful
#   when you have a port-based firewall. To use a fixed port, set this
#   this variable to a statd argument like: "--port 4000 --outgoing-port 4001".
#   For more information, see rpc.statd(8) or http://wiki.debian.org/SecuringNFS
STATDOPTS=

# Do you want to start the idmapd daemon? It is only needed for NFSv4.
NEED_IDMAPD="yes"

# Do you want to start the gssd daemon? It is required for Kerberos mounts.
NEED_GSSD=

然后把 /etc/default/nfs-kernel-server 内容修改为:

# Number of servers to start up
RPCNFSDCOUNT=8

# Runtime priority of server (see nice(1))
RPCNFSDPRIORITY=0

# Options for rpc.mountd.
# If you have a port-based firewall, you might want to set up
# a fixed port here using the --port option. For more information,
# see rpc.mountd(8) or http://wiki.debian.org/SecuringNFS
# To disable NFSv4 on the server, specify '--no-nfs-version 4' here
RPCMOUNTDOPTS="--manage-gids -N 2 -N 3"

# Do you want to start the svcgssd daemon? It is only required for Kerberos
# exports. Valid alternatives are "yes" and "no"; the default is "no".
NEED_SVCGSSD=""

# Options for rpc.svcgssd.
RPCSVCGSSDOPTS=""

RPCNFSDOPTS="-N 2 -N 3"

NFSv4 中的 rpcbind 已经没有用了,但它依旧会随 nfs-server.service 启动。最后需要停掉它们:

sudo systemctl mask rpcbind.socket
sudo systemctl mask rpcbind.service

2 Linux NFS 客户端

使用 RH 系发行版: sudo dnf install nfs-utils 安装 nfs-utils 软件包。

使用 Debian 系发行版: sudo apt install nfs-common 安装 NFS 客户端与服务端。

注: 网上教程也有说用 sudo apt install nfs-client 只安装客户端就行。但是自从 Debian 11 开始的软件库里不再有 nfs-clientsudo apt install nfs-client 会被自动定向到安装 nfs-common

sudo showmount 192.168.1.10 -e 查看服务器共享了哪些文件夹。

在客户机上用 sudo mount -t nfs -o nfsvers=4 192.168.1.10:/mnt/hgst3ta ~/NetworkStorage/hgst3ta/ 挂载。

注: man nfs 可以查看所有的挂载选项。

sudo umount 192.168.1.10:/mnt/hgst3ta 卸载。

可以创建 ~/.config/systemd/user/mnt-r6-FreeBSDa-hgst3ta.service 在用户登入时执行挂载的 .service 文件。内容如下:

[Unit]
Description=Mount r6 vm100 2nd HGST 8T disk under %h/NetworkStorage/hgst3ta/.

[Service]
ExecStart=sudo mount.nfs4 192.168.1.10:/mnt/hgst3ta %h/NetworkStorage/hgst3ta/
ExecStop=sudo umount 192.168.1.10:/mnt/hgst3ta
RemainAfterExit=yes

[Install]
WantedBy=default.target

systemctl --user enable mnt-r6-FreeBSDa-hgst3ta.service 在用户登入后自动执行。



Last Update: 2023-06-17 Sat 14:22

Contact: [email protected]     Generated by: Emacs 27.1 (Org mode 9.3)

若正文中无特殊说明,本站内容遵循: 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议