autofs自动挂载服务是一种Linux系统守护进程,当检测到用户视图访问一个尚未挂载的文件系统时,会自动挂载该文件系统。简单来说,将挂载信息写入/etc/fstab文件中,系统在每次开机时都会自动挂载,而autofs服务则是在用户需要使用该文件系统时才去动态挂载,从而节约了网络资源和服务器硬件资源的开销。
*前面为理解过程操作直接看底部就可以实现应用
第一个里程
检查/安装autofs:
rpm -qa autofs
yum install -y autofs
第二个里程
配置autofs信息:
autofs配置比较简单,由两个文件组成
- /etc/auto.master //这个文件是统一,唯一的。系统配置文件,里面注明了自动挂载的根目录和应用的配置文件
/media /etc/auto.media --timeout 300
/media代表了挂载的根目录所在,auto.media代表所应用的配置文件 --timeout 300代表系统超时,单位s - /etc/auto.media //这个文件是自定义的,对应master中的配置文件,里面写明了实际挂载目录,挂载参数和挂载项所在位置
Rhel6.4 -fstype=iso9660,loop,ro :/iso/Rhel6p4.iso
Rhel6.4代表挂载位于/media/Rhel6.4 -fstype=iso9660文件类型为光盘 loop是将文件模拟块设备挂载在本地 ro=readonly :/iso/Rhel6p4.iso为镜像所在位置为/iso
挂载NFS网络文件系统
第三个里程
主配置文件auto.master和次配置文件auto.misc
/etc/auto.master 文件定义本地挂载点.
/etc/auto.misc 配置文件是用来设置需要挂载的文件系统类型和选项
当在xuegod64上执行 cd /tmp/a/nfs 命令时,自动把xuegod63上nfs共享的/tmp/a/root_squash目录,自动挂载到xuegod64的/tmp/a/nfs目录下
autofs服务要实现自动挂载涉及到两个文件,主配置文件auto.master和次配置文件auto.misc
[root@xuegod64 ~]#mkdir/tmp/a
[root@xuegod64 ~]#vim /etc/auto.master
/tmp/a /etc/auto.nfs --timeout=60
#-timeout=60 挂载超时时间,单位为秒。可以修改这个参数。
[root@xuegod64 ~]#vim /etc/auto.nfs
nfs -fstype=nfs 192.168.1.63:/tmp/a/root_squash
启动/重启autofs服务:
systemctl start/restart autofs
注: 只有cd /tmp/a/nfs 进去, 触发一下,才能自动挂载。 另外 nfs目录,不能提前创建,自动挂载时,系统自动创建nfs目录。
测试:
[root@xuegod64 ~]# ls/tmp/a/ #没有发nfs目录
[root@xuegod64 ~]# cd/tmp/a/nfs
[root@xuegod64 nfs]# pwd
/tmp/a/nfs
[root@xuegod64 nfs]#touch a.txt
[root@xuegod64 nfs]# ll
total 0
-rw-r--r-- 1 nfsnobodynfsnobody 0 Mar 29 21:48 a.txt
或
[root@xuegod64 ~]#touch /tmp/a/nfs/b.txt
只要使用到/tmp/a/nfs目录,就会自动挂载
/etc/sysconfig/autofs 闲置时间配置文件 写入TIMEOUT = 20 代表闲置20秒后自动卸载
实际操作
yum install -y autofs
vim /etc/auto.master
底行模式 :G o
/data /etc/auto.nfs --timeout=60
mkdir /data
vim /etc/auto.nfs
nfs -fstype=nfs 172.16.1.109:/data/data1
启动/重启/开机自启autofs服务:
systemctl start/restart/enable autofs
注: 只有cd /data/nfs 进去, 触发一下,才能自动挂载。 另外 nfs目录,不能提前创建,自动挂载时,系统自动创建nfs目录
[root@web1 guazai]# ls /data
[root@web1 guazai]# cd /data/nfs
[root@web1 nfs]# pwd
/data/nfs
[root@web1 nfs]# touch 789789.txt
[root@web1 nfs]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 18 20:04 123.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 18 20:40 789789.txt



网友评论