NFS练习题
2025-08-22 16:00:08,

一、任务背景

题目:

1.开放/nfs/share目录,提供给任意用户只读查询
2.开放/nfs/upload目录,提供给172.16.1.0/24网段内的机器上传数据,并且要求限制上传数据映射为nfs-upload用户,uid和gid均为200
3.开放/home/chaoge目录仅共享给172.16.1.41这台机器,且只有chaoge01用户可以完全访问该目录
4.添加10G硬盘,给nfs服务端/nfs-nginx-data目录使用,且仅提供给主机名为web-7的机器使用,并且要求限制上传数据映射为www用户,uid、gid均为11211;并且提供HTML、png资源给nginx用;确保nginx可正确访问该静态数据。

主机列表:

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110

二、实验记录

1.开放/nfs/share目录,提供给任意用户只读查询

1.1.在nfs-31机器上安装和启动nfs-utilsrpC++bind

1.1.1.安装nfs-utilsrpcbind

[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理

1.1.2.启动rpcbind服务

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     

1.1.3修该nfs配置文件/etc/exports

[root@nfs-31 share]# vim /etc/exports
[root@nfs-31 share]# cat /etc/exports
/nfs/share *(ro)

1.1.4 建立目录/nfs/share,并在此目录中创建一个文件用于测试。

[root@nfs-31 ~]# mkdir -p /nfs/share
[root@nfs-31 ~]# ll -d /nfs/share/
drwxr-xr-x 2 root root 6 11月 26 09:40 /nfs/share/
[root@nfs-31 ~]#  cd /nfs/share/
[root@nfs-31 share]# echo '这是一个测试文件,你只能读' > testro.txt
[root@nfs-31 share]# ll
总用量 4
-rw-r--r-- 1 root root 40 11月 26 09:42 testro.txt

1.1.5 启动nfs服务

[root@nfs-31 share]# systemctl start nfs
[root@nfs-31 share]# systemctl status nfs
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
  Drop-In: /run/systemd/generator/nfs-server.service.d
           └─order-with-mounts.conf
   Active: active (exited) since 二 2024-11-26 10:09:36 CST; 3s aGo
  Process: 2681 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
  Process: 2678 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
  Process: 2677 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
  Process: 7932 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 7919 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 7917 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 7919 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service
#将服务设置为开机自启
[root@nfs-31 ~]# systemctl enable nfs
[root@nfs-31 ~]# systemctl is-enabled nfs
enabled
[root@nfs-31 ~]# systemctl is-enabled rpcbind
enabled

1.2在client-110进行挂载测试

1.2.1 安装nfs-utils

[root@client-110 ~]# yum install -y nfs-utils
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
...
...
完毕!

1.2.2 查询nfs服务器上开放的目录

[root@client-110 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/nfs/share *

1.2.3 新建一个目录作为挂载点进行挂载

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
0

1.2.4 测试挂载是否是只读

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
1

测试达到题目要求。

2.开放/nfs/upload目录,提供给172.16.1.0/24网段内的机器上传数据,并且要求限制上传数据映射为nfs-upload用户,uidgid均为200

2.1.在nfs-31机器上配置和重载nfs

2.1.1 .新建nfs-upload用户,uidgid均为200

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
2

2.1.2 .修该nfs配置文件/etc/exports

开放/nfs/upload目录,提供给172.16.1.0/24网段内的机器上传数据,并且要求限制上传数据映射为nfs-upload用户

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
3

2.1.3.新建目录/nfs/upload,修改其属性。

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
4

2.1.4.重载nfs服务,查看nfs开放情况

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
5

2.2.在client-110进行挂载和测试

2.2.1.查询nfs服务器上开放的目录

[root@client-110 ~]# showmount -e 172.16.1.31

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
6

2.2.2.新建一个目录作为挂载点进行挂载

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
7

2.2.3.进入挂载点进行测试

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
8

2.2.4.在nfs-31上进入开放目录查看上传的文件

# 外网地址                内网地址          主机名
192.168.122.7       172.16.1.7        web-7
192.168.122.31    172.16.1.31     nfs-31
192.168.122.41    172.16.1.41    rsync-41
192.168.122.110  172.16.1.110  client-110
9

测试达到题目要求。

3.开放/home/chaoge目录仅共享给172.16.1.41这台机器,且只有chaoge01用户可以完全访问该目录

3.1 .在nfs-31机器上配置和重载nfs

3.1.1.新建chaoge01用户,指定用户主目录为/home/chaoge
[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
0

可以看到,chaoge01uidgid都为1001,且只有chaoge01用户可以完全访问该目录

3.1.2.修该nfs配置文件/etc/exports
[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
1
3.1.3.重载nfs服务和查看开放情况
[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
2

3.2.在rsync-41机器上进行挂载和测试

3.2.1.安装nfs-utils,并查看nfs-31上的开放情况
[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
3
[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
4
3.2.2.新建挂载点目录/mnt/nfs/chaoge01,进行挂载测试
[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
5

测试达到题目要求。

4.添加10G硬盘,给nfs服务端/nfs-nginx-data目录使用,且仅提供给主机名为web-7的机器使用,并且要求限制上传数据映射为www用户,uidgid均为11211;并且提供HTML、png资源给nginx用;确保nginx可正确访问该静态数据。

4.1.在nfs-31添加硬盘并按要求进行配置

4.1.1.添加硬盘为/dev/sdb,并进行挂载

查看磁盘/dev/sdb

[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
6

对磁盘进行分区格式化

[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
7
[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
8

新建挂载点/nfs-nginx-data,并挂载

[root@nfs-31 ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                         | 3.6 kB  00:00:00     
epel                                                                                                         | 5.4 kB  00:00:00     
extras                                                                                                       | 2.9 kB  00:00:00     
updates                                                                                                      | 2.9 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.aarch64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.aarch64 已安装并且是最新版本
无须任何处理
9

4.1.2.新建www用户,uidgid均为11211

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
0

4.1.3 配置/etc/exports,并重载nfs

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
1

4.1.4 进入/nfs-nginx-data目录,准备好index文件等资源。

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
2

4.1.5 修改/nfs-nginx-data目录属性

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
3

4.2.在web-7机器上部署nginx,并挂载nfs上的数据。

4.2.1.新建www用户

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
4

4.2.2.部署nginx服务,修改nginx用户。

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
5

4.2.3.启动nginx服务,测试网页。

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
6
[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
7

测试网页能打开

4.2.4.安装nfs-utils,挂载nfs

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
8

查看nfs-31上开放情况

[root@nfs-31 ~]# systemctl start rpcbind
[root@nfs-31 ~]# netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2806/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      2806/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2806/rpcbind        
udp        0      0 0.0.0.0:861             0.0.0.0:*                           2806/rpcbind        
udp6       0      0 :::111                  :::*                                2806/rpcbind        
udp6       0      0 :::861                  :::*                                2806/rpcbind     
9

挂载

[root@nfs-31 share]# vim /etc/exports
[root@nfs-31 share]# cat /etc/exports
/nfs/share *(ro)
0

4.2.5.刷新网页,已能访问nfs上的资源


修改网页再次刷新

[root@nfs-31 share]# vim /etc/exports
[root@nfs-31 share]# cat /etc/exports
/nfs/share *(ro)
1


至此已完成题目。