NFS服务相关端口定义
过会两年时间个人测试环境使用最多的虚拟化平台是PVE了,PVE中使用了独立的网络自管理系统软路由来保证内网拓扑,持久化资源则需要统一保存,虚拟机往往生命周期短,创建和销毁时间随使用场景而有长短。
在测试部署过程为共享资源使用了NFS,当然共享方案还有很多。以下记录了NFS相关的部分细节。
在Linux/Unix软件包查询网站https://pkgs.org 上以nfs关键字检索,在此网站上可以获取包的依赖关系、包集成文件。以下链接可直接跳转到:
可以定位到在 Debian/Ubuntu 系nfs服务端包名称为nfs-kernel-server
可以定位到在 RHEL/CentOS 系nfs服务端包名称为nfs-utils,本文件主要讨论RHEL下的NFS。
os_release | nfs packages version / name | config file location |
---|---|---|
RHEL/CentOS7 | nfs-utils-1.3.0 | /etc/sysconfig/nfs、/etc/nfs.conf |
RHEL/CentOS8 Stream | nfs-utils-2.3.3 | /etc/nfs.conf |
RHEL/CentOS9 Stream | nfs-utils-2.5.4 | /etc/nfs.conf |
除RHEL/CentOS7是两种配置文件方式共存外,在RHEL/CentOS8以后只有/etc/nfs.conf。
除111、2049端口是固定之外,其它端口可以通过配置参数加以固定。
参考:
防火墙设置分为以下几种情况:
NFSv3
NFSv4-only
x1# 启用NFSv3需要的防火墙规则
2firewall-cmd --permanent --add-service mountd
3firewall-cmd --permanent --add-service rpc-bind
4firewall-cmd --permanent --add-service nfs
5
6# 启用NFSv3需要的防火墙规则,同时指定了固定端口时,额外添加端口规则
7firewall-cmd --permanent --add-port=<lockd-tcp-port>/tcp
8firewall-cmd --permanent --add-port=<lockd-udp-port>/udp
9firewall-cmd --permanent --add-port=<statd-tcp-port>/tcp
10firewall-cmd --permanent --add-port=<statd-udp-port>/udp
11
12# 重启相关服务
13systemctl restart rpc-statd.service
14systemctl restart nfs-server.service
15
16# 备注:通过在nfs服务端执行rpcinfo -p可以得到
17# nlockmgr 对于于/etc/nfs.conf 中的[lockd]
18# rpc.statd 对应于/etc/nfs.conf中的[statd]
xxxxxxxxxx
71# 启用NFSv4需要的防火墙规则
2firewall-cmd --permanent --add-service nfs
3
4# 重启相关服务
5systemctl restart nfs-server
6
7
可通过ss -nlpt
,或 rpcinfo -q
验证
xxxxxxxxxx
421
2[root@CentOS9 ~]# ss -nlpt | grep -v nginx
3State Recv-Q Send-Q Local Address:Port Address:Port Process
4# 状态 收 发 服务端本地端口 远程端口 进程
5LISTEN 0 1024 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=643,fd=8))
6LISTEN 0 64 0.0.0.0:41001 0.0.0.0:*
7LISTEN 0 1024 0.0.0.0:41003 0.0.0.0:* users:(("rpc.mountd",pid=3113184,fd=5))
8LISTEN 0 1024 0.0.0.0:41004 0.0.0.0:* users:(("rpc.statd",pid=3111994,fd=9))
9LISTEN 0 64 0.0.0.0:2049 0.0.0.0:*
10LISTEN 0 1024 [::]:111 [::]:* users:(("rpcbind",pid=643,fd=11))
11LISTEN 0 64 [::]:41001 [::]:*
12LISTEN 0 1024 [::]:41003 [::]:* users:(("rpc.mountd",pid=3113184,fd=7))
13LISTEN 0 1024 [::]:41004 [::]:* users:(("rpc.statd",pid=3111994,fd=11))
14LISTEN 0 64 [::]:2049 [::]:*
15
16
17[root@CentOS9 ~]# rpcinfo -p
18 program vers proto port service
19 100000 4 tcp 111 portmapper
20 100000 3 tcp 111 portmapper
21 100000 2 tcp 111 portmapper
22 100000 4 udp 111 portmapper
23 100000 3 udp 111 portmapper
24 100000 2 udp 111 portmapper
25 100024 1 udp 41004 status
26 100024 1 tcp 41004 status
27 100005 1 udp 41003 mountd
28 100005 1 tcp 41003 mountd
29 100005 2 udp 41003 mountd
30 100005 2 tcp 41003 mountd
31 100005 3 udp 41003 mountd
32 100005 3 tcp 41003 mountd
33 100003 3 tcp 2049 nfs
34 100003 4 tcp 2049 nfs
35 100227 3 tcp 2049 nfs_acl
36 100021 1 udp 41001 nlockmgr
37 100021 3 udp 41001 nlockmgr
38 100021 4 udp 41001 nlockmgr
39 100021 1 tcp 41001 nlockmgr
40 100021 3 tcp 41001 nlockmgr
41 100021 4 tcp 41001 nlockmgr
42
xxxxxxxxxx
81[root@Server-83af1803-a240-4112-9d44-05d0b89ae66a ~]# cat /etc/services | grep NFS
2mountd 20048/tcp # NFS mount protocol
3mountd 20048/udp # NFS mount protocol
4nfsrdma 20049/tcp # Network File System (NFS) over RDMA
5nfsrdma 20049/udp # Network File System (NFS) over RDMA
6nfsrdma 20049/sctp # Network File System (NFS) over RDMA
7
8
如果在/etc/nfs.conf中设置的[lockd]端口不生效,可如下设置
在 /etc/modprobe.d/ lockd.conf
文件中指定锁定端口,此文件可能需要手动添加:
更新 /proc/sys/fs/nfs/nlm_tcpport
和 /proc/sys/fs/nfs/nlm_udpport
的当前值:
xxxxxxxxxx
51# sysctl -w fs.nfs.nlm_tcpport=<tcp-port>
2# sysctl -w fs.nfs.nlm_udpport=<udp-port>
3
4sysctl -w fs.nfs.nlm_tcpport=41001
5sysctl -w fs.nfs.nlm_udpport=41001
重启 rpc-statd
和 nfs-server
服务:
xxxxxxxxxx
21# systemctl restart rpc-statd.service
2# systemctl restart nfs-server.service
xxxxxxxxxx
41#LOCKD_TCPPORT=32803
2#LOCKD_UDPPORT=32769
3#MOUNTD_PORT=892
4#STATD_PORT=662
xxxxxxxxxx
101sed -i -e "1aLOCKD_TCPPORT=41001" -e "1aLOCKD_UDPPORT=41001" -e "1aMOUNTD_PORT=41003" -e "1aSTATD_PORT=41004" /etc/sysconfig/nfs
2
3# 修改效果检查
4cat /etc/sysconfig/nfs
5STATD_PORT=41004
6MOUNTD_PORT=41003
7LOCKD_UDPPORT=41002
8LOCKD_TCPPORT=41001
9
10# 由于LOCKD_TCPPPORT和LOCKD_UDPPORT是TCP和UDP,使用同一个端口号也可以,比如41001
xxxxxxxxxx
611# /etc/sysconfig/nfs
2# Note: For new values to take effect the nfs-config service
3# has to be restarted with the following command:
4# systemctl restart nfs-config
5#
6# Optional arguments passed to in-kernel lockd
7#LOCKDARG=
8# TCP port rpc.lockd should listen on.
9#LOCKD_TCPPORT=32803
10# UDP port rpc.lockd should listen on.
11#LOCKD_UDPPORT=32769
12#
13# Optional arguments passed to rpc.nfsd. See rpc.nfsd(8)
14RPCNFSDARGS=""
15# Number of nfs server processes to be started.
16# The default is 8.
17#RPCNFSDCOUNT=16
18#
19# Set V4 grace period in seconds
20#NFSD_V4_GRACE=90
21#
22# Set V4 lease period in seconds
23#NFSD_V4_LEASE=90
24#
25# Optional arguments passed to rpc.mountd. See rpc.mountd(8)
26RPCMOUNTDOPTS=""
27# Port rpc.mountd should listen on.
28#MOUNTD_PORT=892
29#
30# Optional arguments passed to rpc.statd. See rpc.statd(8)
31STATDARG=""
32# Port rpc.statd should listen on.
33#STATD_PORT=662
34# Outgoing port statd should used. The default is port
35# is random
36#STATD_OUTGOING_PORT=2020
37# Specify callout program
38#STATD_HA_CALLOUT="/usr/local/bin/foo"
39#
40#
41# Optional arguments passed to sm-notify. See sm-notify(8)
42SMNOTIFYARGS=""
43#
44# Optional arguments passed to rpc.idmapd. See rpc.idmapd(8)
45RPCIDMAPDARGS=""
46#
47# Optional arguments passed to rpc.gssd. See rpc.gssd(8)
48# Note: The rpc-gssd service will not start unless the
49# file /etc/krb5.keytab exists. If an alternate
50# keytab is needed, that separate keytab file
51# location may be defined in the rpc-gssd.service's
52# systemd unit file under the ConditionPathExists
53# parameter
54RPCGSSDARGS=""
55#
56# Enable usage of gssproxy. See gssproxy-mech(8).
57GSS_USE_PROXY="yes"
58#
59# Optional arguments passed to blkmapd. See blkmapd(8)
60BLKMAPDARGS=""
61
从nfs-utils进入2.x版本后,配置文件/etc/nfs.conf
参数语法采用ini语法,这与samba变得更加一致了。
xxxxxxxxxx
191sed -i -e '/\[mountd\]/a port=41003' /etc/nfs.conf
2sed -i -e '/\[statd\]/a port=41004' /etc/nfs.conf
3sed -i -e '/\[lockd\]/a port=41001' /etc/nfs.conf
4sed -i -e '/\[lockd\]/a udp-port=41001' /etc/nfs.conf
5
6
7## nfs-2.x将参数配置调整到了配置文件nfs.conf中,
8# 由于LOCKD_TCPPPORT和LOCKD_UDPPORT是TCP和UDP,使用同一个端口号也可以,比如41001
9cat /etc/nfs.conf
10[lockd]
11udp-port=41001
12port=41001
13...
14[mountd]
15port=41003
16...
17[statd]
18port=41004
19...
注意,如果lock
xxxxxxxxxx
11
配置文件可以从 https://pkgs.org 网站检索到指定的软件包并下载解压查看,不需要进入服务器或测试环境。
xxxxxxxxxx
1071#
2# This is a general configuration for the
3# NFS daemons and tools
4#
5[general]
6# pipefs-directory=/var/lib/nfs/rpc_pipefs
7#
8[exports]
9# rootdir=/export
10#
11[exportfs]
12# debug=0
13#
14[gssd]
15# verbosity=0
16# rpc-verbosity=0
17# use-memcache=0
18# use-machine-creds=1
19use-gss-proxy=0
20# avoid-dns=1
21# limit-to-legacy-enctypes=0
22# context-timeout=0
23# rpc-timeout=5
24# keytab-file=/etc/krb5.keytab
25# cred-cache-directory=
26# preferred-realm=
27# set-home=1
28# upcall-timeout=30
29# cancel-timed-out-upcalls=0
30#
31[lockd]
32udp-port=4100x
33port=4100x
34# port=0
35# udp-port=0
36#
37[exportd]
38# debug="all|auth|call|general|parse"
39# manage-gids=n
40# state-directory-path=/var/lib/nfs
41# threads=1
42# cache-use-ipaddr=n
43# ttl=1800
44[mountd]
45port=4100x
46# debug="all|auth|call|general|parse"
47# manage-gids=n
48# descriptors=0
49# port=0
50# threads=1
51# reverse-lookup=n
52# state-directory-path=/var/lib/nfs
53# ha-callout=
54# cache-use-ipaddr=n
55# ttl=1800
56#
57[nfsdcld]
58# debug=0
59# storagedir=/var/lib/nfs/nfsdcld
60#
61[nfsdcltrack]
62# debug=0
63# storagedir=/var/lib/nfs/nfsdcltrack
64#
65[nfsdcltrack]
66# debug=0
67# storagedir=/var/lib/nfs/nfsdcltrack
68#
69[nfsd]
70# debug=0
71# threads=8
72# host=
73# port=0
74# grace-time=90
75# lease-time=90
76# udp=n
77# tcp=y
78# vers2=n
79# vers3=y
80# vers4=y
81# vers4.0=y
82# vers4.1=y
83# vers4.2=y
84rdma=y
85rdma-port=20049
86
87[statd]
88port=4100x
89# debug=0
90# port=0
91# outgoing-port=0
92# name=
93# state-directory-path=/var/lib/nfs/statd
94# ha-callout=
95# no-notify=0
96#
97[sm-notify]
98# debug=0
99# force=0
100# retry-time=900
101# outgoing-port=
102# outgoing-addr=
103# lift-grace=y
104#
105[svcgssd]
106# principal=
107
xxxxxxxxxx
391# See smb.conf.example for a more detailed config file or
2# read the smb.conf manpage.
3# Run 'testparm' to verify the config is correct after
4# you modified it.
5# /etc/samba/smb.conf
6
7[global]
8 workgroup = SAMBA
9 security = user
10
11 passdb backend = tdbsam
12
13 printing = cups
14 printcap name = cups
15 load printers = yes
16 cups options = raw
17
18[homes]
19 comment = Home Directories
20 valid users = %S, %D%w%S
21 browseable = No
22 read only = No
23 inherit acls = Yes
24
25[printers]
26 comment = All Printers
27 path = /var/tmp
28 printable = Yes
29 create mask = 0600
30 browseable = No
31
32[print$]
33 comment = Printer Drivers
34 path = /var/lib/samba/drivers
35 write list = @printadmin root
36 force group = @printadmin
37 create mask = 0664
38 directory mask = 0775
39
xxxxxxxxxx
81[root@centos-8 ~]# vim /etc/nfs.conf
2[nfsd]
3 vers2=n
4 vers3=n
5 vers4=y
6 vers4.0=y
7 vers4.1=y
8 vers4.2=y
这部分列出了 Red Hat Enterprise Linux 支持 NFS 版本及其特性。
目前,Red Hat Enterprise Linux 9 支持以下 NFS 主版本:
rpcbind
服务,支持访问控制列表(ACL),并且使用有状态操作。红帽不再支持 NFS 版本 2(NFSv2)。
Red Hat Enterprise Linux 9 中的默认 NFS 版本为 4.2。NFS 客户端默认试图使用 NFSv4.2 挂载,并在服务器不支持 NFSv4.2 时回退到 NFSv4.1。之后挂载会返回 NFSv4.0,然后回退到 NFSv3。
以下是 Red Hat Enterprise Linux 9 中的 NFSv4.2 的功能:
服务器端复制
使用 copy_file_range()
系统调用,可以使 NFS 客户端高效地复制数据,而不浪费网络资源。
稀疏文件
使文件有一个或者多个 洞(hole),它们是不分配或者未初始化的数据块,只由 0 组成。NFSv4.2 中的 lseek()
操作支持 seek_hole()
和 seek_data()
,这使得应用程序能够在稀疏文件中映射漏洞的位置。
保留空间
允许存储服务器保留空闲空间,这会防止服务器耗尽空间。NFSv4.2 支持 allocate()
操作来保留空间,支持 deallocate()
操作来释放空间,支持 fallocate()
操作来预分配和释放文件中的空间。
标记的 NFS
强制实施数据访问权限,并为 NFS 文件系统上的各个文件在客户端和服务器之间启用 SELinux 标签。
布局增强
提供 layoutstats()
操作,它可让一些并行 NFS(pNFS)服务器收集更好的性能统计数据。
111端口是SUN公司的RPC(Remote Procedure Call,远程过程调用)服务所开放的端口,主要用于分布式系统中不同计算机的内部进程通信,RPC在多种网络服务中都是很重要的组件。常见的RPC服务有rpc.mountd、NFS、rpc.statd、rpc.csmd、rpc.ttybd、amd等等。在Microsoft的Windows中,同样也有RPC服务。
服务端需要启动一个NFS服务,服务端要想给客户端提供服务,需要借助RPC协议,RPC协议是由rpcbind服务所实现的;在centos 5或者之前的版本叫portmap服务,centos6及之后的版本叫rpcbind服务,这两个都是一个服务,最终实现了RPC协议的通信。
NFS服务默认不会监听任何端口(启动服务,但不会监听端口),最终监听端口,实现RPC通信的过程是由rpcbind服务产生的RPC协议实现的。
整个流程为:服务端的NFS服务监听一个端口通过RPC协议监听的端口,再去告诉客户端RPC协议,然后NFS客户端通过本机的RPC端口回传数据信息到服务端NFS监听的端口,最终实现通信
如下是文字流程图:
xxxxxxxxxx
101NIS:Network Information System
2NFS:基于ip认证
3 NFS:2049/tcp, 2019/udp
4NFS服务:
5 rpcinfo: 查询RPC信息
6 nfsd: 负责文件读写,工作在TCP/UDP的2049端口。
7 mountd: 负责客户端来源认证的进程,认证成功后接受客户端的挂载请求,工作在随机端口,即时向RPC注册。
8 idmapd: 负责用户ID映射。
9 quotad:负责限定客户端在本地能使用多大磁盘空间,工作在随机端口,即时向RPC注册。
10 nlockmgr: NFS的服务器端锁机制进程,当有一个客户端进程要访问服务器端处理文件之前就会先向NFS注册加锁,以免同时两个客户端进程同时处理一个文件导致文件崩溃;一个进程处理完成之后就会通知NFS释放锁。
使用[mountd]
参考文档 nfs_configure