共享存储服务NFS
那用于Linux之间进行文件共享则是用NFS服务(Network FileSystem)
network file system 网络文件系统
NFS服务端部署流程:
1.安装NFS + RPC
# 检查是否存在nfs和rpc
rpm -qa nfs-utils rpcbind
yum install nfs-utils rpcbind -y
2.启动、检查rpcbind进程
# 必须先启动rpcbind,然后再启动nfs
netstat -tunlp |grep rpc
systemctl start rpcbind
systemctl enable rpcbind
3.创建共享目录 ,修改配置文件:共享目录 访问控制(读写权限,写入磁盘)
[root@nfs01 ~]# vim /etc/exports
/data/www 172.16.1.0/24(rw,sync,all_squash)
[root@nfs01 ~]# mkdir -p /data/www
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data/www
# 允许访问的nfs客户端为172.16.1.0/24网络用户
# ro 只读
# rw 读写
# root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户,它的用户ID/GID会变成nfsnobody
# no_root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员,不安全,避免使用
# all_squash 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户,生产常用参数,降低用户权限,增大安全性
# sync 同时将数据写入到内存与硬盘中,保证不丢失数据
# async 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据
# anonuid=用户ID 设置访问nfs服务的用户的uid,uid需要在/etc/passwd中存在
# anongid=用户组ID 设置访问nfs服务的用户的gid
配置文件修改
语法
默认配置文件路径是/etc/exports
exports配置文件语法
NFS共享目录 NFS客户端地址(参数1、参数2...) 客户端地址2(参数1、参数2...)
例如
/ hostname1(rw) hostname2(rw,no_root_squash)
/pub *(rw)
/home/chao 123.206.16.61(ro)
参数解释
1.NFS共享目录:为NFS服务器要共享的实际目录,必须绝对路径,注意目录的本地权限,如果要读写共享,要让本地目录可以被NFS客户端的(nfsnobody)读写
2.NFS客户端地址,也就是NFS服务器端授权可以访问共享目录的客户端地址,详见下表
3.权限参数,对授权的NFS客户端访问权限设置,见下表
4.启动NFS服务、检查nfs服务状态、以及端口、以及进程信息
systemctl start nfs
systemctl status nfs
netstat -tnlp|grep rpc
ps -ef|grep nfs
5.检查nfs服务端的共享情况,使用showmount命令查看
[root@nfs01 ~]# showmount -e 172.16.1.31 # 172.16.1.31为nfs服务端的ip地址,-e:显示NFS服务器上所有的共享目录
Export list for 172.16.1.31:
/data/www 172.16.1.0/24
6.设置nfs服务端开机自启、包括rpncbind服务
[root@nfs01 ~]#systemctl enable rpcbind nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
注意: /etc/exports文件的语法不要写错,细心 修改/etc/exports文件后,注意要重启systemctl reload nfs或是exportfs -r重新加载配置,无需重启
NFS客户端部署
1.安装nfs工具包
# 查看机器的挂载情况
mount -l
# 查看磁盘分区挂载情况
df -h
yum install nfs-utils rpcbind -y
2.运行客户端的rpcbind程序
systemctl start rpcbind
netstat -tnlp|grep rpc
systemctl start nfs-server nfs-secure-server
3.远程查看nfs服务器信息
# 查看服务端挂载信息
[root@web-7 ~]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/www 172.16.1.0/24
4.挂载测试
mkdir -p /data
[root@web-7 ~]#mount -t nfs 172.16.1.31:/data/www /data # -t表示挂载文件类型,nfs为网络文件类型,172.16.1.31远端挂载的主机IP,/data/www远端挂载的主机目录,/data本地要挂载的目录
[root@web-7 ~]#df -h|grep nfs
172.16.1.31:/data/www 17G 1.8G 16G 11% /data
6.配置开机自动挂载nfs
[root@web-7 /data]#tail -1 /etc/fstab
172.16.1.31:/data/www /data nfs defaults 0 0