Centos7 NFS安装配置

环境说明

CentOS 7(Minimal Install)

$ cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core)

本例演示环境如下

Name IP Addr Descprition
Server 192.169.10.177 服务端 IP
Client 192.168.10.171 客户端 IP
Client 192.168.10.172 客户端 IP
Client 192.168.10.173 客户端 IP

根据官网说明 Chapter 8. Network File System (NFS) - Red Hat Customer Portal,CentOS 7.4 以后,支持 NFS v4.2 不需要 rpcbind 了,但是如果客户端只支持 NFC v3 则需要 rpcbind 这个服务。

服务端

步骤 1: 服务端安装

使用 yum 安装 NFS 安装包。

$ sudo yum install nfs-utils

注意

只安装 nfs-utils 即可,rpcbind 属于它的依赖,也会安装上。

步骤 2: 服务端配置

设置 NFS 服务开机启动

$ sudo systemctl enable rpcbind
$ sudo systemctl enable nfs

启动 NFS 服务

$ sudo systemctl start rpcbind
$ sudo systemctl start nfs

防火墙需要打开 rpc-bind 和 nfs 的服务

$ sudo firewall-cmd --zone=public --permanent --add-service=rpc-bind
success
$ sudo firewall-cmd --zone=public --permanent --add-service=mountd
success
$ sudo firewall-cmd --zone=public --permanent --add-service=nfs
success
$ sudo firewall-cmd --reload
success

步骤 3: 配置共享目录

服务启动之后,我们在服务端配置一个共享目录

$ sudo mkdir /data/nfs
$ sudo chmod 755 /data/nfs

根据这个目录,相应配置导出目录

$ sudo vi /etc/exports

添加如下配置

/data/nfs     192.168.10.0/24(rw,sync,no_root_squash,no_all_squash)
  1. /data/nfs: 共享目录位置。
  2. 192.168.0.10/24: 客户端 IP 范围,* 代表所有,即没有限制。
  3. rw: 权限设置,可读可写。
  4. sync: 同步共享目录。
  5. no_root_squash: 可以使用 root 授权。
  6. no_all_squash: 可以使用普通用户授权。

:wq 保存设置之后,重启 NFS 服务。

$ sudo systemctl restart nfs

可以检查一下本地的共享目录

$ showmount -e localhost
Export list for localhost:
/data/nfs 192.168.10.0/24

这样,服务端就配置好了,接下来配置客户端,连接服务端,使用共享目录。

客户端

步骤 1: 客户端安装

与服务端类似

$ sudo yum install nfs-utils

步骤 2: 客户端配置

设置 rpcbind 服务的开机启动

$ sudo systemctl enable rpcbind

启动 NFS 服务

$ sudo systemctl start rpcbind

注意

客户端不需要打开防火墙,因为客户端时发出请求方,网络能连接到服务端即可。 
客户端也不需要开启 NFS 服务,因为不共享目录。

步骤 3: 客户端连接 NFS

先查服务端的共享目录

$ showmount -e 192.168.10.177
Export list for 192.168.10.177:
/data/nfs 192.168.10.0/24

在客户端创建目录

$ sudo mkdir /data/resources

挂载

$ sudo mount -t nfs 192.168.10.177:/data/nfs /data/resources

挂载之后,可以使用 mount 命令查看一下

$ mount
...
...
192.168.10.177:/data/nfs on /data/resources type nfs4 (rw,relatime,sync,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.10.171,local_lock=none,addr=192.168.10.177)

这说明已经挂载成功了。

步骤 5: 测试 NFS

测试一下,在客户端向共享目录创建一个文件

$ cd /data/resources
$ sudo touch a

之后取 NFS 服务端 192.168.10.177 查看一下

$ cd /data/resources
$ ll
total 0
-rw-r--r--   1 root root   0 Feb 25  2019 a

可以看到,共享目录已经写入了。

步骤 6: 客户端自动挂载

自动挂载很常用,客户端设置一下即可。

$ sudo vi /etc/fstab

在结尾添加类似如下配置


192.168.10.177:/data/nfs      /data/resources                   nfs     defaults        0 0

由于修改了 /etc/fstab,需要重新加载 systemctl

$ sudo systemctl daemon-reload

之后查看一下

$ mount
...
...
192.168.0.177:/data/nfs on /data/resources type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.10.171,local_lock=none,addr=192.168.10.177)

此时已经启动好了。如果实在不放心,可以重启一下客户端的操作系统,之后再查看一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章