1.NFS网络文件系统
(1)什么是NFS?
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。
(2)NFS的优点
-
节省本地存储空间,将常用的数据存放在一台NFS服务器上且可以通过网络访问,那么本地终端将可以减少自身存储空间的使用。
-
用户不需要在网络中的每个机器上都建有Home目录,Home目录可以放在NFS服务器上且可以在网络上被访问使用。
-
一些存储设备如软驱、CDROM和Zip(一种高储存密度的磁盘驱动器与磁盘)等都可以在网络上被别的机器使用。这可以减少整个网络上可移动介质设备的数量。
(3)NFS的组成
NFS体系至少有两个主要部分:一台NFS服务器和若干台客户机。
客户机通过TCP/IP网络远程访问存放在NFS服务器上的数据(在NFS服务器正式启用前,需要根据实际环境和需求,配置一些NFS参数)
2.搭建实验环境
两台虚拟机(系统版本均为7.0):
步骤一:设置虚拟机解析名
hostnamectl set-hostname nfs-server.example.com
hostnamectl set-hostname client.example.com
步骤二:配置网络、ip、yum源
nfs-server虚拟机:172.25.254.118
client虚拟机:172.25.254.218
3.自动挂载&卸载(autofs+nfs )
步骤一:服务端安装服务,设置开机启动,并在火墙中允许nfs、rpc-bind、mountd服务
yum install -y nfs-utils
systemctl start nfs-server
systemctl enable nfs-server
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
firewall-cmd --reload
步骤二:服务端编辑配置文件,写入共享信息
步骤三:客户端安装autofs自动挂载服务,查看服务端共享的目录
yum install -y sutofs
showmount -e 172.25.254.118 /*查看服务端共享的目录*/
- showmount询问rpc-bind有无共享,若服务端有共享,则rpc-bind回答有共享并发送给客户端一个端口
- showmount询问mountd共享的位置,mountd将nfs所共享的目录的位置放给客户端
- showmount询问nfs能否访问(nfs决定谁能访问共享、谁不能访问共享,以及共享内容权限)
步骤四:开启服务,客户端生成/net目录,进入该目录即进入服务器的目录,进入共享目录即实现自动挂载,退出目录即为退出服务,等待300秒后共享目录自动卸载
systemctl restart autofs.service
cd /net
cd 172.25.254.118
cd zxyos_nfs/
df
cd
df
步骤五:服务端在配置文件中设置退出系统后自动卸载等待时间为5秒(默认为300秒)
vim /etc/autofs.conf
time out =5
systemctl restart autofs.service
系统版本是7.0时,编辑/etc/autofs.conf文件
系统版本是7.3时,编辑/etc/sysconfig/autofs文件
nfs服务端的默认挂载点为/net/172.25.254.118/zxyos_nfs
4.更改自动挂载点
步骤一:客户端编辑主策略文件、子策略文件,更改挂载点
vim etc/auto.master /*主策略文件*/
/mnt etc/auto.nfs
/mnt:最终挂载点的上层目录
/etc/auto.nfs:子策略文件(自定义名称)
vim etc/auto.nfs /*子策略文件*/
zxyos 172.25.254.118:/zxyos_nfs
systemctl restart autofs.service
zxyos:最终挂载点(/mnt/zxyos)
172.25.254.118/zxyos_nfs:挂载的网络设备
步骤二:进入共享目录,df查看挂载信息(挂载点是否为设定的位置)
cd /mnt
cd zxyos
df /*已自动挂载*/
mount /*查看挂载信息,读写挂载,版本为4*/
cd
df
更改自动挂载为只读挂载,版本为3.0
步骤一:编辑子策略文件,修改挂载信息
vim etc/auto.nfs
zxyos -ro,vers=3 172.25.254.118:/zxyos_nfs
systemctl restart autofs.service
zxyos:最终挂载点(/mnt/zxyos)
-ro,vers=3:挂载参数(只读,版本为3)
172.25.254.118:/zxyos_nfs:挂载的网络设备
步骤二:进入服务查看挂载信息更改是否生效
cd /mnt
cd zxyos
df /*已自动挂载*/
mount /*查看挂载信息,只读挂载,版本为3*/
nfs读写共享目录
nfs-server服务端:
mkdir /nfs-zxyos
touch /nfs-zxyos/nfsfile{1..3}
vim /etc/exports
/nfs_zxyos *(rw,sync) /*读写方式同步共享给所有用户*/
exportfs -rv
nfs-client客户端:
showmount -e 172.25.254.118 /*查看118的共享*/
vim etc/auto.master
/mnt/sehun /etc/auto1.nfs
vim etc/auto.nfs
zxyos1 172.25.254.118:/nfs-zxyos
systemctl restart autofs.service
/mnt/sehun:最终挂载点的上级目录
/etc/auto1.nfs:子策略文件
zxyos1:最终挂载点
172.25.254.118:/nfs_zxyos:挂载的设备
cd /mnt/sehun
cd zxyos1
df /*已自动挂载*/
mount /*查看挂载信息,读写挂载,版本为4*/
6.修改共享目录的参数
(1-1)rw读写
nfs-server服务端
vim /etc/exports
/zxyos_nfs 172.25:254.218(rw)
exportfs -rv
chmod 777 /zxyos_nfs
nfs-client客户端
vim etc/auto.nfs
zxyos -rw,vers=3 172.25.254.118:/zxyos_nfs
systemctl restart autofs.service
cd /mnt/zxyos
ls
touch nfs-file
ll /*新建文件nfs-file的用户身份为nfsnodody,所属组为nfsnodody*/
(1-2)ro只读
nfs-server服务端
vim /etc/exports
/zxyos_nfs 172.25:254.218(ro)
exportfs -rv
nfs-client客户端:进入服务建立文件被拒绝
cd /mnt/zxyos
ls
touch nfs-file /*建立文件被拒绝*/
(2)指定匿名用户身份anonuid=1001,anongid=1001
nfs-server服务端
id zxyos
vim /etc/exports
/zxyos_nfs 172.25:254.218(rw,anonuid=1001,anongid=1001)
exportfs -rv
nfs-client客户端
touch nfs-file4
ll /*新建文件nfs-file4的用户身份为1001,所属组为1001(客户端uid1001没有对应用户)*/
(3-1)客户端身份转换no_root_squash
no_root_squash:客户端是超级用户,挂载时不转换超级用户身份,沿用服务器端的超级用户,此处的超级用户是指218这台主机中的超级用户,而不是nfs服务中的超级用户
nfs-server服务端:
vim /etc/exports
/zxyos_nfs 172.25:254.218(rw,anonuid=1001,anongid=1001,no_root_squash)
exportfs -rv
nfs-client客户端:
touch nfs-file5
ll /*新建文件nfs-file5的用户身份为超级用户root,所属组为root*/
su - student
mount //172.25.254.118/zxyos_nfs /*student用户使用mount命令不能挂载共享目录*/
cd 最终挂载点
touch nfs-file6
ll
218这台主机的student用户使用mount命令不能挂载共享目录,说明student用户没有查看共享目录的权限,而student可使用autofs自动挂载共享目录则是因为student使用了“超级用户”的身份并一直保持,故所建立文件的用户身份不转换,用户为student,所属组为student
(3-2)客户端身份转换all_squash
all_squash:默认转化身份
nfs-server服务端:
vim /etc/exports
/zxyos_nfs 172.25:254.218(rw,anonuid=1001,anongid=1001,all_squash)
exportfs -rv
nfs-client客户端:
su -
cd 最终挂载点
touch nfs-file7
ll /*新建文件nfs-file7的用户身份为超级用户1001,所属组为1001*/
su - student
cd 最终挂载点
touch nfs-file8
ll /*新建文件nfs-file8的用户身份为超级用户1001,所属组为1001*/
(4)同步方式sync/async
nfs-server服务端
vim /etc/exports
/zxyos_nfs 172.25:254.218(sync)
exportfs -rv
sync:完成任务操作后再进行数据同步,效率更高
vim /etc/exports
/zxyos_nfs 172.25:254.218(async)
exportfs -rv
async:数据实时同步,稳定性更高