跳转至

nfs

1 安装

- 服务端、客户端均安装 `nfs-utils` 和 `rpcbind` 工具
- 服务端先启动 rpc 服务,再启动 nfs 服务
- 客户端启动 rpc 服务
  1. 安装 nfs 服务: dnf install nfs-utils ,会装一些依赖,包括 rpcbind 服务:
  2. 启动 nfs 服务:systemctl start nfs-server,同时会将 rpcbind 服务启动(其它内核可能是nfs)
  3. 检测 nfs 服务运行情况:rpcinfo -p

2 挂载

https://www.cnblogs.com/yanling-coder/p/13028552.html 1. -t nfs :指定 nfs,或者直接 -t nfs4, -o 指定参数,vers=n 指定 nfs 版本号。

mount -t nfs -o vers=3 127.0.0.1:/tmp/nfs1 /tmp/cli1

2.1 NFS 客户端挂载参数

  • 挂载时最好选择 hard +intr :为了防止整个系统被 NFS 锁死,因为 hard 连接的时候,即使服务端 NFS 没起来,客户端也是一直在尝试进行连接,导致 df -h 无输出,如果添加了 intr, 可以按 ctrl+c 中断
  • rsizewsize :性能优化参数,这个设置可以影响客户端和服务端传输数据的缓冲存储量,一般来说在局域网内,并且客户端和服务端都足够大内存可以设置大一点,比如 65535 或者 131072,提升缓冲区可提升 NFS 文件系统传输能力,但是也不要太大了,最好是在网络能传输的最大值,一般默认是:32768。NFS 服务端和客户端之间传输的数据块大小。也就是文件 io block 的大小。
  • 客户端挂载加参数命令格式为:mount -t nfs -o bg,hard,intr,rsize=131072,wsize=131072 10.0.0.7:/data /mnt
  • async :异步
  • sync :同步
  • ro :只读
  • rw :读写
  • auto :可以通过 mount -a 自动挂载,但是前提要写入/etc/fstab 文件
  • defaults :fstab 的缺省值包括:rw,suid,dev,exec,auto,nouser,async
  • exec :允许文件系统执行二进制文件,取消这个参数可以提升安全,因为即使被上传也不能被执行,适用于那种仓库存储
  • noatime :不更新文件系统上的 inode 访问时间,高并发环境推荐,可以显著提高性能
  • nodiratime :不更新文件系统上的目录节点访问时间,高并发环境推荐,可以显著提高性能
  • nosuid :不允许权限位生效,可以提高安全
  • noexec : 不允许文件系统执行二进制文件
  • remount :mount -o remount,rw (当系统出错进入救援模式时无法写系统,可利用 remount 参数)
  • 客户端的安全挂载命令:mount -t nfs -o nosuid,noexec,nodev,rw 192.168.50.1:/data /mnt
  • 客户端的性能挂载命令:mount -t nfs -o noatime,nodiratime,rw 192.168.50.1:/data /mnt
  • 客户端的安全加性能参数为:mount -t nfs -o noatime,nodiratime,nosuid,noexec,nodev,rw,hard,intr,rsize=131072,wsize=131072 192.168.50.1:/data /mnt
  • 本地文件系统挂载:mount /dev/sdb1 /mnt -o defaults,async,noatime,data=writeback,barrier=0
  • 如果卸载文件系统时,有用户或者进程在访问,导致卸载的时候提示设备忙,则可以使用 umount -lf /mnt

3 /etc/exports 配置

1. 可以在linux上通过`man exports`查看有哪些配置项。
2. 建议导出的共享目录是**整个文件系统**,而不是某个文件系统的子目录。因为导出子目录会产生一些其它问题。

/etc/exports 文件包含 NFS 服务器上可供 NFS 客户端访问的本地物理文件系统的列表。每一行表示一个导出点。每一行在路径名之后可能有一个或多个默认选项。以 # 开始的行作为注释行,不会被解析。 要对此文件应用更改,请运行 exportfs -rav 或重新启动 NFS 服务器。

3.1 语法格式

directory    Machine(option)[ Machine(option)[ Machine(option)[ ...]]]

/mnt 192.0.2.0/24(rw) trusty(rw,no_root_squash),每一行一个挂载点,且可以设置多个主机匹配规则,通过空格分隔。

3.2 客户端主机名称格式

  1. ipv4 (192.0.2.0/24)和 ipv6(2001:db8:9:e54::/64):ip 后面的掩码部分可选,如 192.0.2.0
  2. 主机名(hostname ):hostname 是在 /etc/hosts 配置的
  3. 域名(cs.foo.edu):域名可由 DNS 域名服务器设置
  4. 网络组(@group):
  5. 匿名网络(*):表示所有网络客户端均可访问
  6. 通配符:通配符 *? 可以用在域名中,如 *.cs.foo.edu 会匹配所有在域名 cs.foo.edu 下的主机

3.3 选项

3.3.1 用户映射

默认情况下客户端以任何用户访问 nfs 服务端,用户会映射到 nfs 的匿名用户(nobody 用户),这个匿名用户可以通过 anonuid、anongid 设置。

选项 功能
root_squash 将客户端的 uid/gid 为 0(即 root 用户/组) 的映射到 anonymous 用户/组的 uid/gid
no_root_squash 不进行 uid/gid 为 0 (即 root)的映射,相当于 nfs 服务端还是使用 uid/gid 为 0 (root)的身份访问
all_squash 将客户端所有 uid/gid 映射到 anonymous 的 uid/gid(默认选项
no_all_squash 所有用户不进行 uid/gid 映射
anonuid 指定 anonymous 用户的 uid(默认是nobody用户)
anongid 指定 anonymous 组的 gid(默认是 nobody 组)
  • no_root_squash :如果对于访问 NFS server 共享目录用户是 root 的话,那这个客户端对这个共享目录具有 root 权限
  • root_squash :如果对于访问 NFS server 共享目录用户是 root 的话, 那被写入的数据的权限会被压缩成匿名用户,UID 和 GID 通常会变成 nfsnobody, 这个用户是可以改的,只是通常默认是这个。
  • all_squash :不管访问 NFS server 共享目录用户是 root 还是普通用户都会被压缩成匿名用户,UID 和 GID 通常会变成 nfsnobody, 这个用户是可以改的,只是通常默认是这个。

3.3.2 其它选项

选项 功能
rw 读写权限
ro 只读权限
secure 限制客户端只能从 0~1024 tcp/ip端口访问(默认选项)
insecure 取消端口号限制
fsid=num/root/uuid 用于标识每一个文件系统,可以是 32 位数字、root 或者uuid
async 数据异步写入,性能高,但有风险
sync 数据同步写入,性能较低,但数据不会丢失
no_wdelay 取消延迟提交功能;如果设置了 async 则失效。
wdelay 延迟提交写请求,以便于多个写请求合并提交,以提升性能;如果主要是不相关的小请求,性能反而会下降。
no_hide 不隐藏子目录,从而可以同时 export 目录和它的子目录。nfs4 默认开启,nfs2、nfs3 默认 hide 选项
hide 隐藏子目录,即不能共享了父目录后还共享它的子目录
no_subtree_check 不检查共享目录的父级目录是否有权限
subtree_check 检查共享目录的父级目录是否有权限,比如共享目录 /mnt/fs1, 当客户端请求到来时,还要检查父级目录 /mnt 是否也有权限
  • sync :同步客户端往服务端写数据,写成功后才返回 OK,安全不会丢数据,但是性能没 async 那么好。
  • async : 异步请求写入数据时,先返回请求,再将数据写入到内存,如果服务器中途断电,可能内存内的数据来不及向硬中写入,所以可能会丢数据,但是效率高,因为是往对方内中写数据,电商秒杀是异步,短时间内数据会稍微延迟。

3.4 配置示例

/               master(rw) trusty(rw,no_root_squash)
/projects       proj*.local.domain(rw)
/usr            *.local.domain(ro) @trusted(rw)
/home/joe       pc001(rw,all_squash,anonuid=150,anongid=100)
/pub            *(ro,insecure,all_squash)
/srv/www        -sync,rw server @trusted @external(ro)
/foo            2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
/build          buildhost[0-9].local.domain(rw)
  1. The first line exports the entire filesystem to machines master and trusty. In addition to write access, all uid squashing is turned off for host trusty.
  2. The second and third entry show examples for wildcard hostnames and netgroups (this is the entry @trusted).
  3. The fourth line shows the entry for the PC/NFS client discussed above.
  4. Line 5 exports the public FTP directory to every host in the world, executing all requests under the nobody account. The insecure option in this entry also allows clients with NFS implementations that don't use a reserved port for NFS.
  5. The sixth line exports a directory read-write to the machine 'server' as well as the @trusted netgroup, and read-only to netgroup @external, all three mounts with the sync option enabled.
  6. The seventh line exports a directory to both an IPv6 and an IPv4 subnet. The eighth line demonstrates a character class wildcard match.

4 exportfs 命令

-a 和 -v 不太清楚具体区别在哪。
  • 功能:维护导出的 NFS 文件系统表,主要使用 /etc/exports 文件
       /usr/sbin/exportfs [-avi] [-o options,..] [client:/path ..]
       /usr/sbin/exportfs -r [-v]
       /usr/sbin/exportfs [-av] -u [client:/path ..]
       /usr/sbin/exportfs [-v]
       /usr/sbin/exportfs -f
       /usr/sbin/exportfs -s

4.1 -d kind

4.2 -i

  • 功能:忽略/etc/exports 文件和/etc/exports.d 目录下的文件。仅使用默认选项和命令行上给定的选项。
  • 示例:
## exportfs -i
/tmp/nfs1       <world>

4.3 -a

  • 功能:导出或取消导出所有目录。
## exportfs -av
exporting 127.0.0.1:/tmp/nfs1

4.4 -r

  • 功能:重新导出所有目录
  • 示例
## exportfs -rv
exporting 127.0.0.1:/tmp/nfs1

4.5 -u

  • 功能:卸载一个或多个导出目录(客户端挂载点也会被卸载,如果又被导出,客户端又会被挂载,详见示例)
  • 示例:
## mount -t nfs 127.0.0.1:/tmp/nfs1 /tmp/cli1
## df -h|grep /tmp/cli1
127.0.0.1:/tmp/nfs1         17G  3.8G   14G  23% /tmp/cli1
## exportfs -vu 127.0.0.1:/tmp/nfs1
unexporting 127.0.0.1:/tmp/nfs1
## df -h|grep /tmp/cli1
## exportfs -av
exporting 127.0.0.1:/tmp/nfs1
## df -h|grep /tmp/cli1
127.0.0.1:/tmp/nfs1         17G  3.8G   14G  23% /tmp/cli1

4.6 -s

这个选项在显示导出列表信息时很像
  • 功能:显示/etc/exports 当前导出列表。
  • 示例:
## exportfs -s
/tmp/nfs1  *(sync,wdelay,hide,no_subtree_check,fsid=1,sec=sys,rw,secure,root_squash,no_all_squash)

4.7 -v

  • 功能:显示详细信息。导出或取消导出时,显示正在进行的操作;显示当前导出列表时,还显示导出选项列表。
  • 示例:
## 5 exportfs -v
/tmp/nfs1       <world>(sync,wdelay,hide,no_subtree_check,fsid=1,sec=sys,rw,secure,root_squash,no_all_squash)

5 showmount

  • 功能:显示 NFS 服务器的挂载信息

5.1 -e

  • 功能:显示 nfs 服务端的 exports 导出列表
  • 示例
## showmount -e 192.168.211.3
Export list for 192.168.211.3:
/tmp/nfs1 127.0.0.1

5.2 -a、-d

  • 功能:
    • -a 以 HostName:Directory 格式显示所有远程客户机挂载,其中 HostName是客户机的名称,Directory 是远程挂载的目录路径名
    • -d :仅列出客户机挂载的目录
  • 示例
## showmount -a 192.168.211.3
All mount points on 192.168.211.3:
127.0.0.1:/tmp/nfs1
## showmount -d 192.168.211.3
Directories on 192.168.211.3:
/tmp/nfs1

6 nfsstat

6.1 -m

  • 功能:打印有关每个已挂载 NFS 文件系统的信息。
  • 示例
## nfsstat -m
/tmp/cli1 from 127.0.0.1:/tmp/nfs1
 Flags: rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=127.0.0.1,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=127.0.0.1

6.2 -n

  • 功能:指定 nfs 版本,包括 2、3、4
  • 示例
## nfsstat -3 -c
Client rpc stats:
calls      retrans    authrefrsh
93         0          93      

Client nfs v3:
null             getattr          setattr          lookup           access           
2        12%     5        31%     0         0%     0         0%     0         0%     
readlink         read             write            create           mkdir            
0         0%     0         0%     0         0%     0         0%     0         0%     
symlink          mknod            remove           rmdir            rename           
0         0%     0         0%     0         0%     0         0%     0         0%     
link             readdir          readdirplus      fsstat           fsinfo           
0         0%     0         0%     0         0%     3        18%     4        25%     
pathconf         commit           
2        12%     0         0%    

6.3 -s、-c

默认服务端和客户端信息都会打印
  • 功能:
    • -s: 仅打印服务器端统计信息
    • -c : 仅打印客户端统计信息

6.4 -r

  • 功能:打印 rpc 统计信息
  • 示例
## nfsstat -r
Server rpc stats:
calls      badcalls   badfmt     badauth    badclnt
97         0          0          0          0       

Client rpc stats:
calls      retrans    authrefrsh
93         0          93     

6.5 -l

  • 功能:以列表形式打印信息
  • 示例
## nfsstat -l
nfs v3 server        total:       18 
------------- ------------- --------
nfs v3 server         null:        4 
nfs v3 server      getattr:        5 
nfs v3 server       fsstat:        3 
nfs v3 server       fsinfo:        4 
nfs v3 server     pathconf:        2 

7 NFS 实用命令

  • showmount -e 127.0.0.1 :显示可以挂载的文件系统
  • rpcinfo -p localhost 查看 nfs 注册的端口号
  • mount -t nfs 192.168.50.1:/data /mnt 挂载 nfs 文件系统
  • /etc/init.d/nfs reload 平滑重启服务
  • cat /proc/mounts |grep "root/nfs" 客户端查看挂载参数
  • cat /var/lib/nfs/etab 服务端查看 nfs 配置参数