Linux中NFS共享与autofs的使用

1.NFS共享

NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。
实验:
让客户端可以访问并挂载服务端的共享目录
<1>在服务端安装nfs软件并开启服务

[[email protected] ~]# yum install nfs-utils -y	##安装nfs软件
[[email protected] ~]# systemctl start nfs	##开启nfs服务

Linux中NFS共享与autofs的使用
Linux中NFS共享与autofs的使用
注意:如果出现注册的问题就是没有开启nfs
<2>在服务端开启火墙

[[email protected] ~]# systemctl start firewalld	##开启火墙

Linux中NFS共享与autofs的使用
<3>在客户端尝试从ip为172.25.254.166的主机上获取共享目录,但发现会出现报错

[[email protected] ~]# showmount -e 172.25.254.166	##会出现报错

Linux中NFS共享与autofs的使用
<4>在服务端开启nfs服务以及给客户端随机分配端口的一个工具

[[email protected] ~]# firewall-cmd --permanent --add-service=nfs	开启nfs服务
[[email protected] ~]# firewall-cmd --permanent --add-service=rpc-bind	##给客户端随即分配端口的一个工具
[[email protected] ~]# firewall-cmd --reload

Linux中NFS共享与autofs的使用
<5>在客户端再次尝试从ip为172.25.254.166的主机上获取共享目录,会出现报错,但报错的内容不一样

[[email protected] ~]# showmount -e 172.25.254.166	##出现的报错不一样

Linux中NFS共享与autofs的使用
<6>在服务端开启客户主机在挂载的时候需要的一个服务

[[email protected] ~]# firewall-cmd --permanent --add-service=mountd
[[email protected] ~]# firewall-cmd --reload

Linux中NFS共享与autofs的使用
<7>在客户端再次尝试从ip为172.25.254.166的主机上获取共享目录,没有出现报错

[[email protected] ~]# showmount -e 172.25.254.166

Linux中NFS共享与autofs的使用
<8>在服务端修改/etc/exports中的内容并使其生效

[[email protected] ~]# vim /etc/exports
[[email protected] ~]# exportfs -rv

Linux中NFS共享与autofs的使用
配置文件中的内容如下:

/westoslinux *(ro,sync)

Linux中NFS共享与autofs的使用
<9>在客户端从ip为172.25.254.166的主机上获取共享目录,再将其挂载至/mnt目录下,实验完后后,将其卸载

[[email protected] ~]# showmount -e 172.25.254.166
[[email protected] ~]# mount 172.25.254.166:/westoslinux /mnt
[[email protected] ~]# umount /mnt

Linux中NFS共享与autofs的使用

2.autofs

mount是用来挂载文件系统的,可以在系统启动的时候挂载也可以在系统启动后挂载。对于本地固定设备,如硬盘可以使用mount挂载;而光盘、软盘、NFS、SMB等文件系统具有动态性,即需要的时候才有必要挂载。光驱和软盘我们一般知道什么时候需要挂载,但NFS和SMB共享等就不一定知道了,即我们一般不能及时知道NFS共享和SMB什么时候可以挂载。而autofs服务就提供这种功能,好像windows中的光驱自动打开功能,能够及时挂载动态加载的文件系统。免去我们手动挂载的麻烦。要实现光驱,软盘等的动态自动挂载,需要进行相关的配置。
autofs的实验(注意:挂载上之后不用会占用资源,因为会自动挂载和自动卸载):
-1-安装autofs软件
下面内容都在客户端进行
<1>安装autofs

[[email protected] ~]# yum install autofs.x86_64 -y
[[email protected] ~]# systemctl start autofs

Linux中NFS共享与autofs的使用
<2>查看/net下的内容,发现没有内容

[[email protected] ~]# cd /net
[[email protected] net]# ls
[[email protected] net]# ls -a

Linux中NFS共享与autofs的使用
<3>查看autofs相关的配置文件有哪些,打开控制自动卸载时间的文件(内核7.0的是/etc/sysconfig/autofs,内核7.0以上的是/etc/autofs.conf),将其时间更改成3s,便于查看变化,并重启服务

[[email protected] westoslinux]# rpm -qc autofs
[[email protected] westoslinux]# vim /etc/autofs.conf
[[email protected] westoslinux]# systemctl restart autofs.service

Linux中NFS共享与autofs的使用
配置文件中更改的内容如下:

timeout = 3

Linux中NFS共享与autofs的使用
-2-自动挂载和自动卸载
<4>进入/net/172.25.254.166/westoslinux查看,再用df命令查看,会发现共享目录已经自动挂载了

[[email protected] ~]# cd /net
[[email protected] net]# cd 172.25.254.166
[[email protected] 172.25.254.166]# ls
[[email protected] 172.25.254.166]# cd westoslinux
[[email protected] westoslinux]# df

Linux中NFS共享与autofs的使用
<5>退出共享目录后,3s后进行查看,发现共享目录已经自动卸载了

[[email protected] westoslinux]# cd
[[email protected] ~]# df	##3s过后进行查看

Linux中NFS共享与autofs的使用
-3-指定共享目录的挂载点
<6>进入一个新建的共享目录的配置文件,里面写入关于共享目录文件的内容

[[email protected] ~]# vim /etc/auto.westos

Linux中NFS共享与autofs的使用
文件中写入的内容如下:

westos  172.25.254.166:/westoslinux	##westos是最终挂载点,以及挂载的时候的共享 不写默认是rw

Linux中NFS共享与autofs的使用
<7>进入主策略文件(/etc/auto.master),修改后,要重启服务

[[email protected] ~]# vim /etc/auto.master
[[email protected] ~]# systemctl restart autofs.service

Linux中NFS共享与autofs的使用
文件中更改的内容如下:

/misc   /etc/auto.misc
/opt    /etc/auto.westos	##/opt是最终挂载点的上层目录

Linux中NFS共享与autofs的使用
<8>进入设置挂载的目录下,再利用df命令进行查看,会发现共享目录已经成功挂载到想要挂载的目录下了

[[email protected] ~]# cd /opt
[[email protected] opt]# ls
[[email protected] opt]# cd westos
[[email protected] westos]# df
[[email protected] westos]# cd
[[email protected] ~]# df

Linux中NFS共享与autofs的使用

3.在共享目录下设定用户和用户组

-1-在共享目录下新创建的文件的用户和组都是默认的nfsnobody
<1>在服务端修改/etc/exports中的内容并使其生效

[[email protected] ~]# vim /etc/exports
[[email protected] ~]# exportfs -rv

Linux中NFS共享与autofs的使用
在文件中增加的内容如下:

/westoslinux *(ro,sync) 172.25.254.155(rw,sync)	##说明当ip为172.25.254.155的主机访问共享目录时,可以进行读写操作

Linux中NFS共享与autofs的使用
<2>在客户端进入共享目录并创建文件,查看文件的属性时发现文件的用户和组都是默认的nfsnobody

[[email protected] ~]# cd /opt/westos
[[email protected] westos]# ls
[[email protected] westos]# touch file
[[email protected] westos]# ll

Linux中NFS共享与autofs的使用
-2-在共享目录下新创建的文件的用户和组都是root
<3>在服务端修改/etc/exports中的内容并使其生效

[[email protected] ~]# vim /etc/exports
[[email protected] ~]# exportfs -rv

Linux中NFS共享与autofs的使用
在文件中增加的内容如下:

/westoslinux *(ro,sync) 172.25.254.155(rw,sync,no_root_squash
) ##说明当ip为172.25.254.155的主机访问共享目录时,建立的文件的用户和用户组都是root用户

Linux中NFS共享与autofs的使用
<4>在客户端进入共享目录并创建文件,查看文件的属性时发现文件的用户和组都b变成了指定的超级用户

[[email protected] ~]# cd /opt/westos
[[email protected] westos]# touch file2
[[email protected] westos]# ll

Linux中NFS共享与autofs的使用
-3-在共享目录下新创建的文件的用户是uid为1002的用户
<5>在服务端修改/etc/exports中的内容并使其生效

[[email protected] ~]# vim /etc/exports
[[email protected] ~]# exportfs -rv

Linux中NFS共享与autofs的使用
在文件中增加的内容如下:

/westoslinux *(ro,sync) 172.25.254.155(rw,sync,anonuid=1002)
) ##说明当ip为172.25.254.155的主机访问共享目录时,建立的文件的用户是指定的1002

Linux中NFS共享与autofs的使用
<6>在客户端进入共享目录并创建文件,查看文件的属性时发现文件的用户变成了指定的用户(uid=1002的用户)

[[email protected] ~]# cd /opt/westos
[[email protected] westos]# touch file3
[[email protected] westos]# ll

Linux中NFS共享与autofs的使用
-4-在共享目录下新创建的文件的用户是uid为1002的用户,用户组为gid为1001的用户组
<7>在服务端修改/etc/exports中的内容并使其生效

[[email protected] ~]# vim /etc/exports
[[email protected] ~]# exportfs -rv

Linux中NFS共享与autofs的使用
在文件中增加的内容如下:

/westoslinux *(ro,sync) 172.25.254.155(rw,sync,anonuid=1002,anongid=1001)
) ##说明当ip为172.25.254.155的主机访问共享目录时,建立的文件的用户是指定的1002,用户组是指定的1001

Linux中NFS共享与autofs的使用
<8>在客户端进入共享目录并创建文件,查看文件的属性时发现文件的用户变成了指定的用户(uid=1002的用户)

[[email protected] ~]# cd /opt/westos
[[email protected] westos]# touch file4
[[email protected] westos]# ll
[[email protected] westos]# id westos

Linux中NFS共享与autofs的使用
-5-修改NFS的版本(向下兼容的)
<9>更改子策略文件并使其生效

[[email protected] westos]# vim /etc/auto.westos
[[email protected] westos]# systemctl restart autofs

Linux中NFS共享与autofs的使用
文件中更改的内容如下:

westos  172.25.254.166:/westoslinux

Linux中NFS共享与autofs的使用
<10>进入共享目录的挂载路径并查看挂载信息,查看到挂载目录的版本是4.0

[[email protected] ~]# cd /opt/westos
[[email protected] westos]# mount

Linux中NFS共享与autofs的使用
Linux中NFS共享与autofs的使用
<11>更改子策略文件(将其版本号更改成3.0)并使其生效

[[email protected] westos]# vim /etc/auto.westos
[[email protected] westos]# systemctl restart autofs

Linux中NFS共享与autofs的使用
将其挂载目录版本设置成3.0

westos -vers=3.0  172.25.254.166:/westoslinux

Linux中NFS共享与autofs的使用
<12>进入共享目录的挂载路径并查看挂载信息,查看到挂载目录的版本是3.0

[[email protected] ~]# cd /opt/westos
[[email protected] westos]# mount

Linux中NFS共享与autofs的使用
Linux中NFS共享与autofs的使用
-6-修改NFS的模式
NFS模式默认的是hard,当建立一个文件,当建立的动作发生了阻塞,在建立三次之后,会报错
在挂载的时候基本用soft,因为会响应时间的
<13>进入共享目录的挂载路径并查看挂载信息
[[email protected] ~]# cd /opt/westos
[[email protected] westos]# mount
发现共享目录中的模式为hard
Linux中NFS共享与autofs的使用Linux中NFS共享与autofs的使用
<14>退出自动挂载目录后重新挂载目录,此时加上soft模式,最终会发现共享目录中的模式为soft
注意:不要在文件内更改,因为可能会卡死

[[email protected] ]# mount -o soft 172.25.254.128:/westoslinux /opt	##尽量不要在文件内更改,因为可能会卡死
[[email protected] ]# mount

Linux中NFS共享与autofs的使用
Linux中NFS共享与autofs的使用