14.2 NFS服务端安装配置

时间:2022-04-27
本文章向大家介绍14.2 NFS服务端安装配置,主要内容包括NFS服务端安装配置目录概要、NFS服务端安装配置、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

NFS服务端安装配置目录概要

  • yum install -y nfs-utils rpcbind
  • vim /etc/exports //加入如下内容
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
  • 保存配置文件后,执行如下准备操作
  • mkdir /home/nfstestdir
  • chmod 777 /home/nfstestdir
  • systemctl start rpcbind
  • systemctl start nfs
  • systemctl enable rpcbind
  • systemctl enable nfs

NFS服务端安装配置

  1. 首先准备两台机器,我这里准备两台虚拟机,A机器IP分别为192.168.202.130,B机器IP为192.168.202.131
  • A机器,查看IP
[root@hanfeng ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:feff:458f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)
        RX packets 684  bytes 68341 (66.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 226  bytes 26200 (25.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.150  netmask 255.255.255.0  broadcast 192.168.202.255
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 8  bytes 536 (536.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 536 (536.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@hanfeng ~]# 
  • B机器,查看IP
[root@hf-01 ~]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.131  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::baa:c015:db72:1d5c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b6:4d:41  txqueuelen 1000  (Ethernet)
        RX packets 63  bytes 7905 (7.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 79  bytes 11615 (11.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5524 (5.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5524 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@hf-01 ~]# 
  1. 将A机器作为服务端,并安装两个包,分别为 nfs-utils 和 rpcbind
  • yum install -y nfs-utils rpcbind
[root@hanfeng ~]# yum install -y nfs-utils rpcbind
  1. 将B机器作为客户端,安装 nfs-utils 包
  • 每次刚开机运行yum的时候都会很慢,因为需要重新生成一个缓存文件
  • 有时yum安装的时候很慢,我们可以先禁掉,进入到/etc/yum.repos.d/目录下,将目录下的epel.repo文件改个名字,重新安装即可
  • yum install -y nfs-utils
[root@hf-01 ~]# yum install -y nfs-utils
  1. 在安装完成后,需要去A机器中 /etc/exports 编辑配置文件
  • vim /etc/exports //加入如下内容
[root@hanfeng ~]# vim /etc/exports

/home/nfstestdir     
要分享出去的目录是哪一个目录,这个目录是不存在的,后期还需要创建的
192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
指定你要给哪个机器去分享这个目录,首先定义IP或IP段

保存退出
  1. 下面就可以启动服务了,启动服务前需要先创建目录,并设置权限
  2. 在A机器上创建分享的目录
[root@hanfeng ~]# mkdir /home/nfstestdir
[root@hanfeng ~]# 
  1. 并设置成777的权限,这里设置777权限是为了方便接下里的实验
[root@hanfeng ~]# chmod 777 /home/nfstestdir
[root@hanfeng ~]# 
  1. 启动 rpcbind ,再启动前查看A机器上监听的端口,就会看到启动了1/systemd,这是centos7系统的一个特性——>111端口是rpcbind服务监听的
  • 用ps可以查看服务已经启动,因为有这个服务,所以监听了111端口
[root@hanfeng ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2172/master         
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1115/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2172/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2264/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1115/sshd           
[root@hanfeng ~]# ps aux |grep rpc
rpc       2393  0.0  0.1  64964  1044 ?        Ss   15:40   0:00 /sbin/rpcbind -w
root      2537  0.0  0.0 112680   972 pts/0    R+   16:17   0:00 grep --color=auto rpc
  1. 再到B机器上查看下端口,会看到也启动了rpcbind
[root@hf-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2393/rpcbind        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1334/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2146/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2029/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      2393/rpcbind        
tcp6       0      0 :::22                   :::*                    LISTEN      1334/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2146/master         
[root@hf-01 ~]# ps aux |grep rpc
root     16644  0.0  0.0 112656   992 pts/2    R+   04:46   0:00 grep --color=auto rpc
[root@hf-01 ~]# 
  1. 在A机器启动了rpcbind,下面就可以启动nfs服务,在使用ps查看
  • systemctl start rpcbind
[root@hanfeng ~]# systemctl start nfs 
[root@hanfeng ~]# ps aux |grep nfs
root      4560  0.0  0.0      0     0 ?        S<   04:52   0:00 [nfsd4]
root      4561  0.0  0.0      0     0 ?        S<   04:52   0:00 [nfsd4_callbacks]
root      4565  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4566  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4567  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4568  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4569  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4570  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4571  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4572  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4576  0.0  0.0 112676   984 pts/0    R+   04:52   0:00 grep --color=auto nfs
[root@hanfeng ~]# 
  1. 同时也可以在A机器上ps aux |grep rpc查询,在启动nfs服务的时候,它会自动帮你启动rpc相关的一些服务
[root@hanfeng ~]# ps aux |grep rpc
rpc       2957  0.0  0.1  64908  1416 ?        Ss   03:05   0:00 /sbin/rpcbind -w
rpcuser   4530  0.0  0.1  42380  1744 ?        Ss   04:52   0:00 /usr/sbin/rpc.statd
root      4531  0.0  0.0      0     0 ?        S<   04:52   0:00 [rpciod]
root      4539  0.0  0.0  19324   652 ?        Ss   04:52   0:00 /usr/sbin/rpc.idmapd
root      4550  0.0  0.0  42556   948 ?        Ss   04:52   0:00 /usr/sbin/rpc.mountd
root      4602  0.0  0.0 112676   984 pts/0    R+   05:00   0:00 grep --color=auto rpc
[root@hanfeng ~]# 
  1. 而在客户端B机器上是没有的这些服务的
[root@hf-01 ~]# ps aux |grep rpc
root     16644  0.0  0.0 112656   992 pts/2    R+   04:46   0:00 grep --color=auto rpc
[root@hf-01 ~]# 
  1. 如果想让nfs开机启动,还需要执行systemctl enable nfs 命令,在服务端A机器上调用
  • systemctl enable nfs 开机启动nfs
  • systemctl disable nfs 关闭开机启动
[root@hanfeng ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@hanfeng ~]#