NFS 简介
NFS(Network File System)网络文件系统,最大的功能就是可以通过网络,让不同的机器不同的操作系统可以共享彼此的文件。简单理解就是可以挂载远程主机的共享目录到本地,如操作本地磁盘,非常方便的操作远程文件。
部署环境
NFS服务端IP:192.168.0.204/24
NFS客户端IP:192.168.0.205/24
NFS服务端配置
[root@centos7 ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@centos7 ~]# yum -y install rpcbind nfs-utils #安装NFS服务包
[root@centos7 ~]# mkdir -p /data/share #创建共享目录
[root@centos7 ~]# chmod 755 -R /data/share/ #授予共享目录755权限
[root@centos7 ~]# vi /etc/exports # nfs的配置文件是 /etc/exports ,在配置文件中加入以下配置:
/data/share/ 192.168.0.0/24(insecure,rw,no_root_squash) #共享目录/data/share/共享给本地端,允许本地192.168.0.0/24网段内的所有主机访问
[root@centos7 ~]# systemctl start rpcbind && systemctl enable rpcbind
[root@centos7 ~]# systemctl start nfs && systemctl enable nfs # 顺序启动 rpcbind 和nfs 服务
[root@centos7 ~]# showmount -e localhost # 使用showmount命令验证服务端nfs服务可用性
Export list for localhost:
/data/share 192.168.0.0/24
NFS客户端配置
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@centos8 ~]# yum install -y epel-release
[root@centos8 ~]# yum install -y nfs-utils psmisc
[root@centos8 ~]# mkdir -p /data/share/share205
[root@centos8 ~]# mount -t nfs 192.168.0.204:/data/share /data/share/share205/
[root@centos8 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/cl-root 44G 1.7G 43G 4% /
/dev/vda2 2.0G 146M 1.7G 8% /boot
tmpfs 378M 0 378M 0% /run/user/1000
192.168.0.204:/data/share 44G 1.3G 43G 3% /data/share/share205
[root@centos8 ~]# echo "192.168.0.204:/data/share /data/share/share205 nfs defaults 0 0">> /etc/fstab #配置开机自动挂载
[root@centos8 ~]# mount -a
[root@centos8 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jul 24 14:27:02 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=43c58574-46ad-4c8e-b71f-9157c555f57c /boot ext4 defaults 1 2
/dev/mapper/cl-swap swap swap defaults 0 0
192.168.0.204:/data/share /data/share/share205 nfs defaults 0 0
[root@centos8 ~]# reboot #重启验证自动挂载是否生效,同时在服务端、客户端挂载目录写入测试文件观察两台机器是否都有该测试文件