**环境配置:** | IP地址 | 机器角色 | 操作系统 | hostname | | :------------: | :------------: | :------------: | :------------: | | 192.168.1.31 | NFS服务端 | CentOS 7.9 | localhost.localdomain | | 192.168.1.22 | NFS客户端 | CentOS 7.9 | node1 | *注意:一台机器不要同时做 NFS 的服务端和 NFS 的客户端。如果同时作了 NFS 的服务端和客户端,那么在关机的时候,会一直夯住,可能十分钟之后甚至更久才能关闭成功。* *根据官网说明 [Chapter 8. Network File System (NFS) - Red Hat Customer Portal](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-nfs "Chapter 8. Network File System (NFS) - Red Hat Customer Portal"),CentOS 7.4 以后,支持 NFS v4.2 不需要 rpcbind 了,但是如果客户端只支持 NFC v3 则需要 rpcbind 这个服务。* ------------ ------------ # 一、NFS 工作原理简介 NFS 是Network File System的缩写,即网络文件系统。一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布。功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在类Unix系统间实现磁盘文件共享的一种方法。 NFS 的基本原则是“**容许不同的客户端及服务端通过一组RPC分享相同的文件系统**”,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。 **NFS在文件传送或信息传送过程中依赖于RPC协议。** RPC,远程过程调用 (Remote Procedure Call) 是能使客户端执行其他系统中程序的一种机制。NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络进行资料的分享,这是因为NFS使用了一些其它的传输协议。而这些传输协议用到这个RPC功能的。可以说NFS本身就是使用RPC的一个程序。或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS SERVER或者NFS CLIENT。这样SERVER和CLIENT才能通过RPC来实现PROGRAM PORT的对应。可以这么理解RPC和NFS的关系:NFS是一个文件系统,而RPC是负责负责信息的传输。 **启动 NFS SERVER 之前,首先要启动 RPC 服务**(CentOS 5.x 下为 portmap 服务,CentOS 6.x 和 **CentOS 7.x 下为 rpcbind 服务**,下同),否则 NFS SERVER 就无法向 RPC 服务注册了。 ![NFS工作原理简图.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/1347562793.png) 另外,**如果 RPC 服务重新启动,原来已经注册好的NFS端口数据就会丢失**,因此,此时 RPC 服务管理的NFS程序也需要重新启动以重新向RPC注册。 **要特别注意的是:一般修改NFS配置文件后,是不需要重启NFS的** - 直接在命令行执行 /etc/init.d/nfs reload 「针对CentOS 5.x 或 CentOS 6.x」 - 或 systemctl reload nfs.service 「针对CentOS 7.x」 - 或 exportfs -rv 即可使修改的 /etc/exports 生效 # 二、NFS服务搭建与配置 ## 1、检查防火墙和SELinux 检查防火墙服务 ```bash [root@localhost ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 日 2022-04-24 06:27:23 CST; 4min 9s ago Docs: man:firewalld(1) Main PID: 708 (firewalld) CGroup: /system.slice/firewalld.service └─708 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid 4月 24 06:27:22 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 4月 24 06:27:23 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. 4月 24 06:27:24 localhost.localdomain firewalld[708]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now. [root@localhost ~]# ``` 防火墙未关闭,使用如下命令进行关闭 ```bash systemctl stop firewalld systemctl disable firewalld ``` 检查SELinux ```bash [root@localhost ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@localhost ~]# ``` 未关闭禁用,使用如下命令关闭禁用: ```bash sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config ``` ## 2、安装 NFS 和 RPC **服务端、客户端都需要安装 NFS 和 RPC** NFS的安装是非常简单的,只需要两个软件包即可,而且在通常情况下,是作为系统的默认包安装的。 - nfs-utils-* :包括基本的NFS命令与监控程序 - rpcbind-* :支持安全NFS RPC服务的连接 如果当前系统中没有安装NFS所需的软件包,需要手工进行安装。nfs-utils 和rpcbind 两个包的安装文件在系统光盘中都会有。 查看系统是否已安装NFS ```bash rpm -qa | grep nfs rpm -qa | grep rpcbind ``` 安装nfs相关服务软件包(**服务端和客户端都要安装**) ```bash yum install -y nfs-utils rpcbind ``` 验证系统是否成功安装NFS ```bash [root@localhost ~]# rpm -qa | grep nfs libnfsidmap-0.25-19.el7.x86_64 nfs-utils-1.3.0-0.68.el7.2.x86_64 [root@localhost ~]# [root@localhost ~]# rpm -qa | grep rpcbind rpcbind-0.2.0-49.el7.x86_64 [root@localhost ~]# ``` 查看用户信息 ```bash [root@localhost ~]# tail /etc/passwd nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin #yum安装rpc服务时创建的 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin #yum安装rpc服务时创建的 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin #yum安装nfs服务时创建的 [root@localhost ~]# ``` ## 3、NFS服务端搭建 **配置exports** 创建共享存储文件夹 ```bash mkdir /nfs ``` 配置nfs 输入以下内容,格式为:nfs共享目录 nfs客户端地址1(param1, param2,...) nfs客户端地址2(param1, param2,...) 如: /nfs 192.168.1.22/24(rw,async,no_root_squash) 固定网段所有IP可挂载 ```bash echo "/nfs 192.168.1.*(rw,sync,no_root_squash)" >> /etc/exports ``` 固定网段固定IP区间可挂载 ```bash echo "/nfs 192.168.1.1/24(rw,sync,no_root_squash)" >> /etc/exports ``` 所有客户端可挂载 ```bash echo "/nfs *(rw,sync,no_root_squash)" >> /etc/exports ``` 启动服务,先启动rpc服务,再启动nfs服务 ```bash systemctl start rpcbind ``` 加入开机自启动 ```bash systemctl enable rpcbind systemctl enable nfs && systemctl restart nfs ``` 查看服务状态 ```bash systemctl status rpcbind systemctl status nfs ``` ![rpcbind和nfs状态.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/861172296.png) rpcinfo -p ,如果显示rpc 服务器注册的端口列表(端口:111),则启动成功。 ```bash [root@localhost ~]# rpcinfo -p 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 35073 status 100024 1 tcp 44864 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 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 3 udp 2049 nfs_acl 100021 1 udp 33828 nlockmgr 100021 3 udp 33828 nlockmgr 100021 4 udp 33828 nlockmgr 100021 1 tcp 44627 nlockmgr 100021 3 tcp 44627 nlockmgr 100021 4 tcp 44627 nlockmgr [root@localhost ~]# ``` **配置文件/etc/exports** *权限配置的说明:* - rw:可读写 - ro: 只读,但最终能不能读写,还是与文件系统的 rwx 及身份有关 - no_root_squash:当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 - root_squash: 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名(nobody)用户 - all_squash: 不论登入 NFS 的使用者身份为何, 均被映射为匿名用户,通常就是 nobody(nfsnobody) - insecure: 允许从客户端过来的非授权访问 - sync: 数据会同步写入到内存与硬盘中 - async: 数据会先暂存于内存当中,而非直接写入硬盘 - anonuid: 指定uid的值,此uid必须存在于/etc/passwd中 - anongid:指定gid的值 **exportfs的参数说明:** - -a:全部mount 或unmount /etc/exports中的内容 - -r: Reexport all directories: 重新导出所有目录 - -u:umount目录 - -v: verbose,输出详情 如: 使nfs配置生效exportfs -r **查看是否成功和可用的nfs地址:** 查看可用的nfs地址 ```bash [root@localhost ~]# showmount -e localhost Export list for localhost: /nfs * [root@localhost ~]# ``` **看有哪些参数生效** ```bash [root@localhost ~]# cat /var/lib/nfs/etab /nfs *(rw,sync,wdelay,hide,nocrossmnt,secure,no_root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,no_root_squash,no_all_squash) [root@localhost ~]# ``` **参数说明:** 1. ro:只读设置,这样 NFS 客户端只能读、不能写(默认设置); 2. rw:读写设置,NFS 客户端可读写; 3. sync:将数据同步写入磁盘中,效率低,但可以保证数据的一致性(默认设置); 4. async:将数据先保存在内存缓冲区中,必要时才写入磁盘;如果服务器重新启动,这种行为可能会导致数据损坏,但效率高; 5. root_squash:当客户端用 root 用户访问该共享文件夹时,将 root 用户映射成匿名用户(默认设置); 6. no_root_squash:客户端的 root 用户不映射。这样客户端的 root 用户与服务端的 root 用户具有相同的访问权限,这可能会带来严重的安全影响。没有充分的理由,不应该指定此选项; 7. all_squash:客户端所有普通用户及所属组都映射为匿名用户及匿名用户组;「推荐设置」 8. no_all_squash:客户端所有普通用户及所属组不映射(默认设置); 9. subtree_check:如果共享,如:/usr/bin之类的子目录时,强制NFS检查父目录的权限; 10. no_subtree_check:即使共享 NFS 服务端的子目录时,nfs服务端也不检查其父目录的权限,这样可以提高效率(默认设置) 11. secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置); 12. insecure:允许客户端从大于1024的tcp/ip端口连接服务器; 13. wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置); 14. no_wdelay:若有写操作则立即执行,当使用async时,无需此设置; 15. anonuid=xxx:将远程访问的所有用户主都映射为匿名用户主账户,并指定该匿名用户主为本地用户主(UID=xxx); 16. anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组为本地用户组(GID=xxx); ## 4、NFS客户端配置 **配置挂载** 创建挂载的文件夹 ```bash mkdir -p /nfs/data ``` 挂载nfs ```bash mount -t nfs 192.168.1.31:/nfs /nfs/data ``` *其中:* - mount:表示挂载命令 - -t:表示挂载选项 - nfs:挂载的协议 - 192.168.1.31:nfs服务器的ip地址 - /nfs:nfs服务器的共享目录 - /nfs/data:本机客户端要挂载的目录 查看挂载信息df -Th 或 cat /proc/mounts ```bash [root@node1 ~]# df -Th 文件系统 类型 容量 已用 可用 已用% 挂载点 devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs tmpfs 1.9G 67M 1.8G 4% /run tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 22G 3.2G 19G 15% / /dev/sda1 xfs 1014M 151M 864M 15% /boot tmpfs tmpfs 378M 0 378M 0% /run/user/0 192.168.1.31:/nfs nfs4 22G 1.6G 21G 8% /nfs/data [root@node1 ~]# ``` ## 5、测试挂载 可以进入客户端的/nfs/data目录,上传一个文件,然后去nfs服务器查看/nfs目录中是否有该文件,若有则共享成功。反之在nfs服务器操作/nfs目录,查看本机客户端的目录是否共享。 ```bash cat << EOF >>/nfs/data/welcome.txt welcome to use nfs EOF ``` ![nfs挂载测试.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/3758045281.png) 取消挂载 ```bash umount /nfs/data ``` **挂载方式** 除了上述通过mount -t nfs命令指定的方式进行目录挂载以外,还可以通过vim /etc/fstab文件进行开机自动挂载。 ```bash 192.168.1.31:/nfs /nfs/data nfs defaults 1 1 ``` *其中:* 1. 第一列 192.168.1.31:/nfs:(Device)磁盘设备文件或该设备的Label或者UUID,此处即为nfs服务器的地址和共享目录 2. 第二列/nfs/data:(Mount point)是设备的挂载点,即本机挂载目录 3. 第三列nfs:(Filesystem)是磁盘文件系统的格式,如ext2、nfs、vfat等。 4. 第四列defaults:(parameters)是文件系统的参数,defaults即具有rw,suid,dev,exec,auto,nouser,async等默认参数。 5. 第五列1:(Dump)能够被dump备份命令作用,一般是0或者1,0表示不用做dump备份,1表示每天进行dump操作,当然还有2,表示不定期进行dump操作。 6. 第六列1:是否检验扇区,0表示不要检验,1表示最早检验(根目录一般会设置),2表示1级别检验完成之后进行检验。 # 三、NFS 常用命令 服务端版本查看 ```bash nfsstat -s ``` 客户端版本查看 ```bash nfsstat -c ``` ```bash [root@node1 ~]# nfsstat --help Usage: nfsstat [OPTION]... -m, --mounts Show statistics on mounted NFS filesystems #挂载在服务端上的客户端版本查看 -c, --client Show NFS client statistics #客户端版本查看 -s, --server Show NFS server statistics #服务端版本查看 -2 Show NFS version 2 statistics -3 Show NFS version 3 statistics -4 Show NFS version 4 statistics -o [facility] Show statistics on particular facilities. nfs NFS protocol information rpc General RPC information net Network layer statistics fh Usage information on the server's file handle cache rc Usage information on the server's request reply cache all Select all of the above -v, --verbose, --all Same as '-o all' -r, --rpc Show RPC statistics -n, --nfs Show NFS statistics -Z[#], --sleep[=#] Collects stats until interrupted. Cumulative stats are then printed If # is provided, stats will be output every # seconds. -S, --since file Shows difference between current stats and those in 'file' -l, --list Prints stats in list format --version Show program version --help What you just did [root@node1 ~]# ``` 最后修改:2022 年 04 月 24 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏