参考 CentOS 7 下 yum 安装和配置 NFS | Zhanming’s blog,补充整理部分内容

1. 环境说明

本文中的服务器环境如下:

Role Hostname OS
NFS 服务端 centos-2 CentOS 7.5
NFS 客户端 abelsu7-ubuntu Ubuntu 18.04

:为简略起见,以下命令均以root身份运行,省略sudo

2. NFS 服务端

2.1 安装 nfs-utils

:对应的 Apt 包为nfs-kernel-servernfs-common

> yum info nfs-utils

Available Packages
Name        : nfs-utils
Arch        : x86_64
Epoch       : 1
Version     : 1.3.0
Release     : 0.65.el7
Size        : 412 k
Repo        : base/7/x86_64
Summary     : NFS utilities and supporting clients and daemons for the kernel NFS server
URL         : http://sourceforge.net/projects/nfs
License     : MIT and GPLv2 and GPLv2+ and BSD
Description : The nfs-utils package provides a daemon for the kernel NFS server and
            : related tools, which provides a much higher level of performance than the
            : traditional Linux NFS server used by most users.
            : 
            : This package also contains the showmount program.  Showmount queries the
            : mount daemon on a remote host for information about the NFS (Network File
            : System) server on the remote host.  For example, showmount can display the
            : clients which are mounted on that host.
            : 
            : This package also contains the mount.nfs and umount.nfs program.

> yum install nfs-utils
# rpcbind 作为依赖会自动安装

2.2 配置并启动服务

允许rpcbind.servicenfs.service开机自启

# 允许服务开机自启
> systemctl enable rpcbind
> systemctl enable nfs

启动相关服务:

# 启动相关服务
> systemctl start rpcbind
> systemctl start nfs

防火墙允许服务通过:

# 防火墙允许服务通过
> firewall-cmd --zone=public --permanent --add-service={rpc-bind,mountd,nfs}
success

> firewall-cmd --reload
success

2.3 配置共享目录

例如需要共享的目录为/mnt/kvm/

# 创建 /mnt/kvm 并修改权限
> cd /mnt
/mnt > mkdir kvm
/mnt > chmod 755 kvm

# 验证目录权限
/mnt > ls -l
total 0
drwxr-xr-x 2 root root 59 Oct 17 17:49 kvm

之后修改/etc/exports,将/mnt/kvm/添加进去:

> cat /etc/exports

# 1. 只允许 abelsu7-ubuntu 访问
/mnt/kvm/ abelsu7-ubuntu(rw,sync,no_root_squash,no_all_squash)

# 2. 根据 IP 地址范围限制访问
/mnt/kvm/ 192.168.0.0/24(rw,sync,no_root_squash,no_all_squash)

# 3. 使用 * 表示访问不加限制
/mnt/kvm/ *(rw,sync,no_root_squash,no_all_squash)

关于/etc/exports中的参数含义

  • /mnt/kvm/:需要共享的目录
  • 192.168.0.0/24客户端 IP 范围*表示无限制
  • rw权限设置,可读可写
  • sync同步共享目录
  • no_root_squash:可以使用root授权
  • no_all_squash:可以使用普通用户授权

保存之后,重启nfs服务:

> systemctl restart nfs

2.4 查看共享目录列表

centos-2本地查看:

> showmount -e localhost
Export list for localhost:
/mnt/kvm abelsu7-ubuntu

3. NFS 客户端

3.1 安装 nfs-utils

# CentOS/Fedora, etc.
> yum install nfs-utils

# Ubuntu/Debian, etc.
> apt install nfs-common

3.2 配置并启动服务

设置rpcbind服务开机启动

> systemctl enable rpcbind

启动rpcbind

> systemctl start rpcbind

客户端不需要打开防火墙,也不需要开启 NFS 服务

3.3 挂载共享目录

先查看服务端的共享目录

> showmount -e centos-2
Export list for centos-2:
/mnt/kvm abelsu7-ubuntu

在客户端创建并挂载对应目录

> mkdir -p /mnt/kvm
> mount -t nfs centos-2:/mnt/kvm /mnt/kvm

最后检查一下是否挂载成功:

> df -hT /mnt/kvm
Filesystem         Type  Size  Used  Avail  Use%  Mounted on
centos-2:/mnt/kvm  nfs4  500G  119G   382G   24%  /mnt/kvm

> mount | grep /mnt/kvm
centos-2:/mnt/kvm on /mnt/kvm type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=222.xxx.xxx.xxx,local_lock=none,addr=116.xxx.xxx.xxx)

3.4 配置自动挂载

在客户端编辑/etc/fstab

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system>                            <mount point>  <type>  <options>       <dump>  <pass>

# / was on /dev/sda8 during installation
UUID=26d36e85-367a-4200-87fb-0505c5837078  /              ext4    errors=remount-ro    0       1

# /boot/efi was on /dev/sda1 during installation
UUID=000E-274F                             /boot/efi      vfat    umask=0077           0       1

# swap was on /dev/sda9 during installation
# UUID=ee4da9a3-0288-4f8e-a86e-ab8ac3faa6bc  none           swap    sw                   0       0

# For nfs
centos-2:/mnt/kvm                          /mnt/kvm       nfs     defaults             0       0

最后重新加载systemctl,即可实现重启后自动挂载:

> systemctl daemon-reload

> mount | grep /mnt/kvm
centos-2:/mnt/kvm on /mnt/kvm type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=222.xxx.xxx.xxx,local_lock=none,addr=116.xxx.xxx.xxx)

4. NFS 读写速度测试

待更新…

> time dd if=/dev/zero of=/mnt/kvm-lun/test-nfs-speed bs=8k count=1024
> time dd if=/mnt/kvm-lun/test-nfs-speed of=/dev/null bs=8k count=1024

参考文章

NFS 安装与配置

  1. CentOS 7 下 yum 安装和配置 NFS | Zhanming’s blog
  2. NFS | ArchLinux
  3. NFS 服务配置 | 极客学院
  4. NFS 服务器 | 鸟哥的私房菜
  5. NFS 配置不当那些事 | Blood_Zero
  6. CentOS 7.1 安装配置 NFS 共享文件系统 | Linux 运维日志
  7. Ubuntu 18.04 下安装 NFS 详细步骤 | CSDN
  8. NFS /etc/exports 参数解释 | CSDN
  9. 常用 NFS mount 选项介绍 | 博客园

dd 读写速度测试

  1. 测试 nfs 文件读写速度 | CSDN
  2. NFS 使用详解之三:NFS 传输速度优化 | CSDN

cp 命令显示进度

  1. 使用 rsync 命令取代 cp 拷贝,可显示进度 | ReMember
  2. Ubuntu 下的拷贝 cp 命令无进度条或进度显示的解决 | 梦翔天空
  3. Linux 中 cp 文件或目录时如何显示进度? | CSDN
  4. 一个可以显示 Linux 命令运行进度的工具:Coreutils Viewer (cv) | Linux 中国
  5. cv: 显示 cp、mv 等命令的进度 | LinuxToy
  6. progress - Linux tool to show progress for cp, mv, dd, … (formerly known as cv) | Github

NFS 性能优化

  1. KVM NFS 磁盘性能不佳 | 程序园
  2. NFS 性能优化手册 | CSDN
  3. NFS 读写块大小分析 | 开源中国