Linux 安装 NFS

摘要

  • 本文介绍 linux 下 nfs 文件系统的安装与使用

  • 本文基于CentOS8(x86_64)

NFS 简介

  • NFS(网络文件系统,Network File System)是一个通过网络共享文件的协议,允许不同服务器或客户端像挂载本地磁盘一样访问远端的共享文件目录。

准备一台服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 设置 hostname
hostnamectl set-hostname nfs-server

# 查看网卡
ip a

# 设置IP 分配方式,manual: 手动指定IP,auto: DHCP自动获取,disabled: 关闭IP
nmcli con mod enp0s5 ipv4.method manual
# 设置 IP
nmcli con mod enp0s5 ipv4.addresses 10.211.55.88/24
# 设置 gateway
nmcli con mod enp0s5 ipv4.gateway 10.211.55.1
# 设置 dns
nmcli con mod enp0s5 ipv4.dns "10.211.55.1,8.8.8.8"
# 重新启动网卡
nmcli con up enp0s5

NFS 安装:服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 安装 nfs-utils 和 rpcbind
dnf install rpcbind nfs-utils -y

# 创建 nfs-server 存储目录
mkdir -p /nfs-server/data

# 配置 nfs-server,可以添加多个目录
echo "/nfs-server/data *(insecure,rw,sync,no_root_squash,no_all_squash)" >> /etc/exports
## 配置项说明
# * :允许所有客户端访问,也可以指定具体的客户端 IP 地址,比如:192.168.0.0/24(rw,sync,no_root_squash,no_all_squash)
# insecure :允许任意端口(兼容性好,支持k8s),默认值是 secure:只允许 1024 以下端口,适合传统服务器
# rw :读写权限,ro:只读权限
# sync :同步模式,强一致性,async:异步模式,性能高,但存在丢数据风险
# no_root_squash :允许 root 用户访问,默认情况下,root 用户访问时会被转换成 nobody 用户
# no_all_squash :允许所有用户访问,默认情况下,所有用户访问时会被转换成 nobody 用户,默认就是这个,可以省略。

# 启动 rpcbind、nfs 服务
systemctl start rpcbind && systemctl start nfs-server
# 开机自启动
systemctl enable rpcbind && systemctl enable nfs-server

# 修改配置文件 /etc/exports,需要重载 nfs 配置
systemctl reload nfs-server

# 查看 nfs 服务状态
systemctl status nfs-server

# 查看 nfs 服务监听的端口,默认为 2049
$ rpcinfo -p | grep nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl

# 检查导出的共享目录
$ exportfs -v
/nfs-server/data
<world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,insecure,no_root_squash,no_all_squash)

开放端口

  • 查看当前使用的端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ rpcinfo -p 10.211.55.88
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 36578 status
100024 1 tcp 46973 status
100005 1 udp 20048 mountd
100005 1 tcp 20048 mountd
100005 2 udp 20048 mountd
100005 2 tcp 20048 mountd
100005 3 udp 20048 mountd
100005 3 tcp 20048 mountd
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl
100021 1 udp 36529 nlockmgr
100021 3 udp 36529 nlockmgr
100021 4 udp 36529 nlockmgr
100021 1 tcp 45917 nlockmgr
100021 3 tcp 45917 nlockmgr
100021 4 tcp 45917 nlockmgr
Program (编号) 服务名称 用途 端口 说明
100000 portmapper RPC 服务注册表 111/tcp, 111/udp 必须开放,客户端通过它查找其它服务端口
100024 status (rpc.statd) 文件锁定状态管理 36578/udp, 46973/tcp 常配合 nlockmgr 使用,非必需
100005 mountd 挂载服务 20048/tcp, 20048/udp 挂载时必需
100003 nfs NFS 主服务 2049/tcp 核心服务,必需
100227 nfs_acl NFS 的 ACL 权限控制 2049/tcp 通常与 nfs 一起
100021 nlockmgr 文件锁管理 36529/udp, 45917/tcp Stateful 应用涉及文件锁时需要
  • 最小端口要求(基本挂载用)

场景 需要开放的端口
基本 NFS 挂载读写 2049/tcp + 111/tcp/udp
容器或 K8s 挂载 同上,一般还推荐 20048/tcp/udp
  • 开放必须端口

1
2
3
4
5
6
firewall-cmd --permanent --add-port=2049/tcp
firewall-cmd --permanent --add-port=111/tcp
firewall-cmd --permanent --add-port=111/udp
firewall-cmd --permanent --add-port=20048/tcp
firewall-cmd --permanent --add-port=20048/udp
firewall-cmd --reload

NFS 安装:客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 安装 nfs-utils 和 rpcbind,安装后不需要启动 nfs-server 服务,只需要启动 rpcbind 服务,所有客户端都要运行
sudo dnf install rpcbind nfs-utils -y
sudo systemctl start rpcbind && systemctl enable rpcbind

# 查看服务器的共享资源列表
# showmount -e <nfs-server-ip>
$ showmount -e 10.211.55.88
Export list for 10.211.55.88:
/nfs-server/data *

# 创建 客户端挂载目录
mkdir -p /nfs/data
# 挂载
mount -t nfs 10.211.55.88:/nfs-server/data /nfs/data

# 查看挂载点
$ mount | grep nfs/data
10.211.55.88:/nfs-server/data on /nfs/data type nfs4 (rw,relatime,vers=4.2,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.211.55.11,local_lock=none,addr=10.211.55.88)
# 查看挂载点信息,这种方式更友好
$ df -t nfs4
Filesystem 1K-blocks Used Available Use% Mounted on
10.211.55.88:/nfs-server/data 42872832 3630080 39242752 9% /nfs/data

# 配置自动挂载
echo "10.211.55.88:/nfs-server/data /nfs/data nfs defaults 0 0" >> /etc/fstab
# 根据 /etc/fstab 文件的配置,尝试挂载所有未挂载的文件系统。修改 /etc/fstab 后执行 mount -a 立即生效
mount -a
  • 客户端挂载的目录不能删除,需要先卸载

1
2
3
4
5
6
7
# -f 强制卸载
umount -f /nfs/data

vim /etc/fstab
# 删除或注释掉对应的 NFS 挂载行

rm -rf /nfs/data