当前位置首页 > CentOS知识

CentOS7安装配置NFS

阅读次数:266 次  来源:admin  发布时间:

https://abelsu7.top/2019/10/17/centos7-install-nfs/

1. 环境说明

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

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

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

2. NFS 服务端 2.1 安装 nfs-util

注:对应的 Apt 包为nfs-kernel-server和nfs-commo

> 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 作为依赖会自动安装

Bash Copy 2.2 配置并启动服务

允许rpcbind.service、nfs.service开机自启:

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

Bash Copy

启动相关服务:

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

Bash Copy

防火墙允许服务通过:

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

> firewall-cmd --reload
success

Bash Copy 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

Bash Copy

之后修改/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)

Bash Copy

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

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

保存之后,重启nfs服务:

> systemctl restart nfs

Bash Copy 2.4 查看共享目录列表

在centos-2本地查看:

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

Bash Copy 3. NFS 客户端 3.1 安装 nfs-util

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

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

Bash Copy 3.2 配置并启动服务

设置rpcbind服务开机启动:

> systemctl enable rpcbind

Bash Copy

启动rpcbind:

> systemctl start rpcbind

Bash Copy

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

3.3 挂载共享目录

先查看服务端的共享目录:

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

Bash Copy

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

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

Bash Copy

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

> 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)

Bash Copy 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

Bash Copy

最后重新加载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)

Bash Copy 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
上一篇:linux下利用nohup后台运行jar文件包程序
下一篇:Debian添加软件源