:::info

NFS 是 Network FileSystem 的缩写,顾名思义就是网络文件存储系统,它最早是由 Sun 公司发展出来的,也是 FreeBSD 支持的文件系统中的一个,它允许网络中的计算机之间通过 TCP/IP 网络共享资源。通过 NFS,我们本地 NFS 的客户端应用可以透明地读写位于服务端 NFS 服务器上的文件,就像访问本地文件一样方便。简单的理解,NFS 就是可以透过网络,让不同的主机、不同的操作系统可以共享存储的服务。

:::

NFS 在文件传送或信息传送过程中依赖于 RPC(Remote Procedure Call) 协议,即远程过程调用, NFS 的各项功能都必须要向 RPC 来注册,如此一来 RPC 才能了解 NFS 这个服务的各项功能 Port、PID、NFS 在服务器所监听的 IP 等,而客户端才能够透过 RPC 的询问找到正确对应的端口,所以,NFS 必须要有 RPC 存在时才能成功的提供服务,简单的理解二者关系:NFS是 一个文件存储系统,而 RPC 是负责信息的传输。
通过上边简要的介绍,我们知道 NFS 服务需要依赖 RPC 服务,所以这里 NFS 服务端需要安装 rpcbind 和 nfs-utils,客户端只需要安装 nfs-utils。

官网:https://nfs.sourceforge.net/

1、确认是否已经安装

  1. rpm -qa nfs-utils rpcbind

2、在线安装

有网的条件下可以通过网络快速安装

  1. #服务端
  2. yum install -y nfs-utils rpcbind
  3. #客户端
  4. yum install -y nfs-utils

3、离线安装

公司中很多情况下是内网环境,需要使用离线安装方式

  1. # 下载离线包
  2. ## rpcbind
  3. libtirpc-0.2.4-0.16.el7.x86_64.rpm
  4. rpcbind-0.2.0-49.el7.x86_64.rpm
  5. ## nfs-utils(依赖比较多)
  6. nfs-utils-1.3.0-0.68.el7.x86_64.rpm
  7. tcp_wrappers-7.6-77.el7.x86_64
  8. ...

安装

  1. #安装路径下的所有rpm包
  2. rpm -Uvh *.rpm --nodeps --force
  3. #安装特定的rpm包
  4. rpm -hvi dejagnu-1.4.2-10.noarch.rpm

4、配置检查是否安装成功

  1. # 1.在服务端创建一个共享目录 /data/share ,作为客户端挂载的远端入口,然后设置权限。
  2. mkdir -p /data/share
  3. chmod 755 /data/share
  4. # 2.修改 NFS 配置文件 /etc/exports
  5. vim /etc/exports
  6. #配置单个ip
  7. /data/share 192.168.0.130(rw,sync,insecure,no_subtree_check,no_root_squash)
  8. /data/share 192.168.0.131(rw,sync,insecure,no_subtree_check,no_root_squash)
  9. #配置ip段
  10. /data/share 192.168.0.130/139(rw,sync,insecure,no_subtree_check,no_root_squash)
  11. #配置所有ip可以挂载
  12. /data/share *(rw,sync,insecure,no_subtree_check,no_root_squash)
说明: /data/share-共享目录
192.168.0.130-IP地址,可以是特定的ip地址、ip地址段或所有可以访问的ip
rw,sync,insecure,no_subtree_check,no_root_squash-访问控制参数,具体参考下面列表。
参数 说明
ro 只读
rw 读写
sync 同步共享-所有数据在请求时写入共享
async 异步共享-nfs 在写入数据前可以响应请求
secure nfs 通过 1024 以下的安全 TCP/IP 端口发送
insecure nfs 通过 1024 以上的端口发送
wdelay 如果多个用户要写入 nfs 目录,则归组写入(默认)
no_wdelay 如果多个用户要写入 nfs 目录,则立即写入,当使用 async 时,无需此设置
hide 在 nfs 共享目录中不共享其子目录
no_hide 共享 nfs 目录的子目录
subtree_check 如果共享 /usr/bin 之类的子目录时,强制 nfs 检查父目录的权限(默认)
no_subtree_check 不检查父目录权限
all_squash 共享文件的 UID 和 GID 映射匿名用户 anonymous,适合公用目录
no_all_squash 保留共享文件的 UID 和 GID(默认)
root_squash root 用户的所有请求映射成如 anonymous 用户一样的权限(默认)
no_root_squash root 用户具有根目录的完全管理访问权限
anonuid=xxx 指定 nfs 服务器 /etc/passwd 文件中匿名用户的 UID
anongid=xxx 指定 nfs 服务器 /etc/passwd 文件中匿名用户的 GID

5、启动服务并测试

1. 启动rpc服务

  1. service rpcbind start
  2. #或者使用如下命令
  3. /bin/systemctl start rpcbind.service
  4. # 查看 NFS 服务项 rpc 服务器注册的端口列表
  5. rpcinfo -p localhost

2. 启动nfs服务

  1. service nfs start
  2. #或者使用如下命令亦可
  3. /bin/systemctl start nfs.service
  4. # 启动 NFS 服务后 rpc 服务已经启用了对 NFS 的端口映射列表
  5. # rpcinfo -p localhost

3. 在另一台 Linux 上挂载目录

查看配置,showmoun -e 192.168.0.130
  1. #新建目录
  2. mkdir -p /share
  3. #挂载共享目录
  4. mount 192.168.0.130:/data/share /share
  5. #如果要卸载目录
  6. umount /share

4. 设置防火墙

NFS 使用是:2049 RPC 端口:111 RPC client :随机端口
  1. # firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.223.225.0/24" port protocol="tcp" port="9110" accept"
  2. firewall-cmd --zone=public --add-port=111/tcp --permanent
  3. firewall-cmd --zone=public --add-port=2049/tcp --permanent
  4. firewall-cmd --reload

6、安全加固

以下是银河麒麟v10,其他系统如:openEuler类似

1. nfs相关端口固定

端口固定,便于开放策略,默认端口如mountd:20048、rquotad:875等

固定后:rquotad:30001、lockd:30002、mountd:30003、statd:30004

开放策略时firewall-cmd --permanent --add-port=30001-30004/tcp

其他原因暂时未想到!

1)service中固定

  1. # 查看nfs-utils是否已安装,未安装需要安装
  2. rpm -qa | grep nfs
  3. nfs-utils-help-2.5.4-4.oe2203.x86_64
  4. nfs-utils-2.5.4-4.oe2203.x86_64
  5. # 查看服务状态 和 默认监听的端口
  6. systemctl status nfs rpcbind
  7. # 查看端口所对应的服务名,以便在/etc/services中注释配置
  8. rpcinfo -p
  9. # 修改配置,固定NFS端口
  10. vi /etc/services
  11. #添加注释
  12. #mountd 20048/tcp # NFS mount protocol
  13. #mountd 20048/udp # NFS mount protocol
  14. #rquotad 875/tcp # rquota daemon
  15. #rquotad 875/udp # rquota daemon
  16. #添加自定义端口配置(这些端口是哪里的)
  17. rquotad 30001/tcp
  18. rquotad 30001/UDP
  19. lockd 30002/tcp
  20. lockd 30002/udp
  21. mountd 30003/tcp
  22. mountd 30003/udp
  23. statd 30004/tcp
  24. statd 30004/tcp
  25. # 重启服务,进行验证
  26. systemctl status nfs-idmap nfs-lock nfs-server rpcbind
  27. systemctl restart nfs-idmap nfs-lock nfs-server rpcbind
  28. #systemctl restart nfs-lock
  29. #systemctl restart nfs-server
  30. #systemctl restart rpcbind

以上还有随机端口,未修改到的则进行下一步

2)nfs.conf进一步固定

  1. vi /etc/nfs.conf
  2. #在原来的标签下添加内容
  3. [lockd]
  4. port=30002
  5. udp-port=30002
  6. [statd]
  7. port=30004
  8. # 再次重启服务进行验证无误即可
  9. systemctl status nfs-idmap nfs-lock nfs-server rpcbind
  10. systemctl restart nfs-idmap nfs-lock nfs-server rpcbind

2. 防火墙策略开放

  1. # 让防火墙通过NFS服务
  2. firewall-cmd --permanent --add-service=nfs
  3. # 通过rpc服务(如果不开启,rpcinfo就不能扫描)
  4. firewall-cmd --permanent --add-service=rpc-bind
  5. # 通过mountd服务(如果不开启,不能远程showmount)
  6. firewall-cmd --permanent --add-service=mountd
  7. #重载
  8. firewall-cmd --reload
  9. # 端口方式
  10. firewall-cmd --permanent --add-port=2049/tcp
  11. firewall-cmd --permanent --add-port=2049/udp
  12. firewall-cmd --permanent --add-port=111/tcp
  13. firewall-cmd --permanent --add-port=111/udp
  14. firewall-cmd --permanent --add-port=30001-30004/tcp
  15. firewall-cmd --permanent --add-port=30001-30004/udp
  16. firewall-cmd --reload

3. 安全加固 设置黑白名单

思路:通过系统白名单和nfs配置来进行加固

1)修改nfs配置

这一步可能已经做过了,查看一下配置核对一下

  1. # 1.修改nfs配置
  2. cat /etc/exports
  3. # 此处表示允许10.223.225.0/24这个网段访问,其他不允许访问
  4. /data/nfs 10.223.225.0/255.255.255.0(rw,sync,no_root_squash)
  5. #重载配置
  6. exportfs -avr

2)添加访问白名单和黑名单

  1. # 白名单
  2. cat /etc/hosts.allow
  3. # 10.223.225. 表示此网段可访问
  4. mountd:10.223.225.
  5. rpcbind:10.223.225.:allow
  6. portmap:10.223.225.
  7. lockd:10.223.225.
  8. rquotad:10.223.225.
  9. statd:10.223.225.
  10. #本机
  11. portmap:127.0.0.1
  12. lockd:1127.0.0.1
  13. rquotad:127.0.0.1
  14. statd:127.0.0.1
  15. mountd:127.0.0.1
  16. rpcbind:127.0.0.1:allow
  17. # 黑名单
  18. cat /etc/hosts.deny
  19. mountd:ALL
  20. rpcbind:ALL:deny
  21. statd:ALL
  22. portmap:ALL
  23. lockd:ALL
  24. rquotad:ALL

3)验证

  1. # 同一网段访问
  2. rpcinfo server-ip
  3. showmount -e server-ip
  4. # 不同网段放
  5. rpcinfo server-ip
  6. showmount -e server-ip
  • 不同网段获取不到nfs的信息,也不能挂载即成功加固

4. 配置IP可挂载

  1. # 修改配置
  2. vim /etc/exports
  3. /data/nfs IP1(rw,sync,no_root_squash)
  4. /data/nfs IP2(rw,sync,no_root_squash)
  5. /data/nfs IP3(rw,sync,no_root_squash)
  6. ...
  7. /data/nfs IPn(rw,sync,no_root_squash)
  8. # 重启nfs服务
  9. systemctl restart nfs-server.service
  10. # 验证
  • 最后一个是重启nfs-server之后的

NFS部署 - 图1

附件

https://www.volcengine.com/theme/2587133-C-7-1

:::warning RPM仓库地址:

搜狐开源镜像站 http://mirrors.sohu.com/

网易开源镜像站 http://mirrors.163.com/

阿里云开源镜像站 http://mirrors.aliyun.com/

公云开源镜像站 http://mirrors.pubyun.com/

首都在线开源镜像站 http://mirrors.yun-idc.com/

linux运维派开源镜像站 http://mirrors.skyshe.cn/

中科院开源镜像站 http://mirrors.opencas.cn/

北京理工大学开源镜像站 http://mirror.bit.edu.cn/web/

北京交通大学开源镜像站 http://mirror.bjtu.edu.cn/cn/

兰州大学开源镜像站 http://mirror.lzu.edu.cn/

中国科技大学开源镜像站 http://mirrors.ustc.edu.cn/

浙江大学开源镜像站 http://mirrors.zju.edu.cn/

厦门大学开源镜像站 http://mirrors.xmu.edu.cn/

东北大学开源镜像站 http://mirror.neu.edu.cn/

华中科技大学开源镜像站 http://mirrors.hust.edu.cn/

重庆大学开源镜像站 http://mirrors.cqu.edu.cn/

东软信息学院开源镜像站 http://mirrors.neusoft.edu.cn/

大连理工大学开源镜像站 http://mirror.dlut.edu.cn/

中山大学开源镜像站 http://mirror.sysu.edu.cn/

上海交通大学ftp站 http://ftp.sjtu.edu.cn/

北京大学ftp站 ftp://ftp.pku.edu.cn

北京邮电大学ftp站 ftp://ftp.bupt.edu.cn

:::