1.基础环境配置
IP地址 | 主机名 |
192.168.200.10 | nfs-master |
192.168.200.20 | nfs-client |
(1)创建两台虚拟机,节点为192.168.200.10(20),分别更改主机名
[root@localhost ~]# hostnamectl set-hostname nfs-master
[root@localhost ~]# bash
[root@nfs-master ~]#
[root@localhost ~]# hostnamectl set-hostname nfs-client
[root@localhost ~]# bash
[root@nfs-client ~]#
(2)两台虚拟机配置本地yum源,此处以nfs-master为例
[root@nfs-master ~]# mkdir /opt/centos
[root@nfs-master ~]# mount /dev/cdrom /opt/centos
mount: /dev/sr0 is write-protected, mounting read-only
[root@nfs-master ~]# mv /etc/yum.repos.d/* /media/
[root@nfs-master ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[root@nfs-master ~]# yum clean all
[root@nfs-master ~]# yum repolist
(3)两台虚拟机关闭防火墙和selinux,此处以nfs-master为例
[root@nfs-master ~]# systemctl stop firewalld
[root@nfs-master ~]# systemctl disable firewalld
[root@nfs-master ~]# setenforce 0
2.在nfs-master上创建NFS服务端并配置
(1)安装NFS和rpcbind服务
[root@nfs-master ~]# yum -y install nfs-utils rpcbind
(2)创建用于NFS文件共享的文件夹,并设置足够权限
[root@nfs-master ~]# mkdir /root/data
[root@nfs-master ~]# chmod -Rf 777 /root/data
[root@nfs-master ~]# ll
drwxrwxrwx. 2 root root 6 Sep 30 11:26 data
(3)配置NFS服务程序
[root@nfs-master ~]# vi /etc/exports
/root/data 192.168.200.*(rw,sync,root_squash)
NFS配置文件的格式为 “共享的目录名称 运行使用的客户端地址(权限参数1,权限参数2...)”
参数 |
作用 |
---|---|
ro |
只读 |
rw |
读写 |
root_squash |
当NFS客户端以root管理员身份访问时,映射为NFS服务器的匿名用户 |
no_root_squash |
当NFS客户端以root管理员身份访问时,映射为NFS服务器的root管理员 |
all_squash |
无论NFS客户端使用什么身份访问,都映射为NFS服务器的匿名用户 |
sync |
同时将数据写入到内存和硬盘中,保证不丢失数据 |
async |
优先将数据保存到内存,然后再写入硬盘,可能会丢失数据 |
(4)启动NFS和rpcbind服务
[root@nfs-master ~]# systemctl restart rpcbind
[root@nfs-master ~]# systemctl enable rpcbind
[root@nfs-master ~]# systemctl start nfs-server
[root@nfs-master ~]# systemctl enable nfs-server
3.在nfs-client上创建NFS客户端并配置
(1)安装安装NFS和rpcbind服务,并使用showmount命令查看NFS服务器共享信息
[root@nfs-client ~]# yum -y install nfs-utils rpcbind
[root@nfs-client ~]# showmount -e 192.168.200.10
Export list for 192.168.200.10:
/root/data 192.168.200.*
(2)创建用于挂载NFS文件共享的文件夹并挂载
[root@nfs-client ~]# mkdir /nfs_data
[root@nfs-client ~]# mount -t nfs 192.168.200.10:/root/data /nfs_data/
(3)使用df -h命令查看挂载的详细信息
[root@nfs-client ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 1.1G 49G 3% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 911M 0 911M 0% /dev/shm
tmpfs 911M 9.6M 902M 2% /run
tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/mapper/centos-home 47G 33M 47G 1% /home
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 183M 0 183M 0% /run/user/0
/dev/sr0 4.2G 4.2G 0 100% /opt/centos
192.168.200.10:/root/data 50G 1.1G 49G 3% /nfs_data
4.在nfs-client上创建测试文件,在nfs-master上查看
[root@nfs-client ~]# cd /nfs_data/
[root@nfs-client nfs_data]# echo "Hello" > client
[root@nfs-master ~]# cd /root/data/
[root@nfs-master data]# cat client
Hello
(补充内容)
持久化挂载NFS文件共享的文件夹
[root@nfs-client ~]# vi /etc/fstab
192.168.200.10:/root/data /nfs_data nfs defaults 0 0
卸载挂载的NFS文件夹
[root@nfs-client ~]# umount /nfs_data