<>1.NFS介绍

NFS(Network File
System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样

NFS是运行在应用层的协议。随着NFS多年的发展和改进,NFS既可以用于局域网也可以用于广域网,且与操作系统和硬件无关,可以在不同的计算机或系统上运行。

以下是NFS最显而易见的好处:

*
节省本地存储空间,将常用的数据存放在一台NFS服务器上且可以通过网络访问,那么本地终端将可以减少自身存储空间的使用。

*
用户不需要在网络中的每个机器上都建有Home目录,Home目录可以放在NFS服务器上且可以在网络上被访问使用。

*
一些存储设备如软驱、CDROM和Zip(一种高储存密度的磁盘驱动器与磁盘)等都可以在网络上被别的机器使用。这可以减少整个网络上可移动介质设备的数量。

<>2.NFS服务器的搭建

实验环境
重置两个虚拟机,配置网络,设置主机名,分别为client.westos.com和server.westos.com
<http://xn--client-vy7i92xsja.westos.xn--comserver-sw9o.westos.com>
,配置yum源,完成本地解析,



在server虚拟机
vim /etc/hosts 172.25.254.113 server.westos.com

在client虚拟机
vim /etc/hosts 172.25.254.213 client.westos.com

完成后关闭图形界面,用主机ssh去连接两个虚拟机

文件系统

nfs
在server下载 nfs-utils
vim /etc/exports /mnt 172.25.254.0/24(ro,async)
async表示不同步,速率块,可靠性不高,sync速率慢,可靠性高

systemctl start nfs-utils.service 开启服务 exports -rv 刷新
关闭火墙

在客户端
showmount -e 172.25.254.113
查看共享
[root@client ~]# showmount -e 172.25.254.113 Export list for 172.25.254.113:
/mnt 172.25.254.0/24
将共享出来的目录挂载
[root@client ~]# mount 172.25.254.113:/mnt/ /mnt/ [root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda1 10473900 3144380
7329520 31% / devtmpfs 469332 0 469332 0% /dev tmpfs 484920 140 484780 1%
/dev/shm tmpfs 484920 12696 472224 3% /run tmpfs 484920 0 484920 0%
/sys/fs/cgroup 172.25.254.113:/mnt 10473984 3237632 7236352 31% /mnt

cd /mnt touch file 不能建立,因为之前写的配置文件,分享的/mnt是只读的

在server
vim /etc/exports /mnt 172.25.254.0/24(rw,async)

exportfs -rv 刷新

在客户端
/mnt目录下
[root@client mnt]# touch file touch: cannot touch ‘file’: Permission denied

建立文件,显示被禁止,说明是权限的问题

在server端,
chmod 777 /mnt
在client端再次建立,便可以
[root@client mnt]# touch file [root@client mnt]# ls file


[root@client mnt]# ll total 0 -rw-r--r--. 1 nfsnobody nfsnobody 0 May 24 22:28
file

显示使用的用户为nfsnobody

用户的管理
vim /eyt/exports /mnt 172.25.254.0/24(rw,async,anonuid=1001,anongid=1001)
指定用户为1001

exportfs -rv
在client端建立文件
[root@client mnt]# touch file1 [root@client mnt]# ll total 0 -rw-r--r--. 1
nfsnobody nfsnobody 0 May 24 22:28 file -rw-r--r--. 1 1001 1001 0 May 24 22:29
file1

服务端
[root@server mnt]# ll total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 24 22:28
file -rw-r--r-- 1 1001 1001 0 May 24 22:29 file1

no_root_squash 服务端沿用客户端的用户
vim /etc/exports /mnt
172.25.254.0/24(rw,async,anonuid=1001,anongid=1001,no_root_squash) exportfs -rv

客户端
[root@client mnt]# useradd westos [root@client mnt]# chmod 777 /mnt

[root@client mnt]# su - westos [westos@client ~]$ cd /mnt [westos@client mnt]$
touch file2 [westos@client mnt]$ ll total 0 -rw-r--r--. 1 nfsnobody nfsnobody 0
May 24 22:28 file -rw-r--r--. 1 westos westos 0 May 24 22:29 file1 -rw-rw-r--.
1 westos westos 0 May 24 22:32 file2

服务端
[root@server mnt]# ll total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 May 24 22:28
file -rw-r--r-- 1 1001 1001 0 May 24 22:29 file1 -rw-rw-r-- 1 1001 1001 0 May
24 22:32 file2

服务端沿用客户端的用户,因为服务端没有westos用户,所以就将id直接裸露出来了


/mnt
172.25.254.0/24(rw,async,anonuid=1001,anongid=1001,all_squash,no_root_squash)
客户端用户身份为服务器指定的用户,若没有指定,则为匿名用户,与客户端用户无关
服务端

exportfs -rv
客户端

切换到student用户在/mnt下建立file3,查看,发现还是文件的用户身份还是westos
这是因为服务器指定的是1001用户,而在客户端1001即为westos用户,所以显示为(影射为客户端的westos)westos

在服务端查看


客户端自动挂载卸载
yum install autofs


下载后启动软件
将之前做的挂载,卸载掉

cd /net/172.25.254.113/mnt
然后df发现已经挂载,cd退出目录,5分钟后自动卸载

如何修改它的默认时间呢

不同版本的软件配置文件不同,实际情况
rpm -qc autofs
查看配置文件

Vim /ect/autofs.conf

修改 timeout=10 单位为秒

Systemctl restart autos

cd /net/172.25.254.113/mnt
df 查看,已挂载

cd 退出目录 ,10秒后再df查看

发现已经自动卸载

修改默认挂载目录
在客户端的autofs的主配置文件
vim /etc/auto.master
添加
/mnt /etc/auto.nfs 这个文件是自定义名称

然后配置这个文件
Vim /etc/auto.nfs westos 172.25.254.113:/mnt

systemctl restart autofs
测试
[root@client ~]# cd /mnt/westos [root@client westos]# df Filesystem 1K-blocks
Used Available Use% Mounted on /dev/vda1 10473900 3154080 7319820 31% /
devtmpfs 469332 0 469332 0% /dev tmpfs 484920 140 484780 1% /dev/shm tmpfs
484920 12708 472212 3% /run tmpfs 484920 0 484920 0% /sys/fs/cgroup
172.25.254.113:/mnt 10473984 3236864 7237120 31% /mnt/westos

如何使/mnt下的子目录用哪个就挂载哪个
一。先在server的/mnt下建立三个目录
建好后刷新 exportfs -rv


二。配置client的/etc/auto.nfs文件
vim /etc/auto.nfs
添加
* 172.25.254.113:/mnt/&

systemctl restart autofs
测试
[root@client ~]# cd /mnt/westos1 [root@client westos1]# df Filesystem
1K-blocks Used Available Use% Mounted on /dev/vda1 10473900 3154140 7319760 31%
/ devtmpfs 469332 0 469332 0% /dev tmpfs 484920 140 484780 1% /dev/shm tmpfs
484920 12708 472212 3% /run tmpfs 484920 0 484920 0% /sys/fs/cgroup
172.25.254.113:/mnt/westos1 10473984 3237376 7236608 31% /mnt/westos1

[root@client westos1]# cd 10s后自动卸载后再做下一步操作
[root@client ~]# cd /mnt/westos2 [root@client westos2]# df Filesystem
1K-blocks Used Available Use% Mounted on /dev/vda1 10473900 3154120 7319780 31%
/ devtmpfs 469332 0 469332 0% /dev tmpfs 484920 140 484780 1% /dev/shm tmpfs
484920 12708 472212 3% /run tmpfs 484920 0 484920 0% /sys/fs/cgroup
172.25.254.113:/mnt/westos2 10473984 3237376 7236608 31% /mnt/westos2

用mount命令来查看

172.25.254.113:/mnt/westos2 on /mnt/westos2 type nfs4
(rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.25.254.213,local_lock=none,addr=172.25.254.113)


可以看到自动挂载时,不能像用mount命令可以设置挂载的权限
那么自动挂载如何操作呢
vim /etc/auto.nfs * -ro,vers=3 172.25.254.113:/mnt/&

systemctl restart autofs

测试
cd /mnt/westos2 mount
172.25.254.113:/mnt/westos2 on /mnt/westos2 type nfs
(ro,relatime,vers=3,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=172.25.254.113,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=172.25.254.113)

与上面对比发现,操作生效

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信