前言

当我们在linux环境下想要访问一些需要挂载才能使用的文件或目录时,也许你会毫不犹豫的直接mount就好了。

当你经常访问这个文件时,也许你会想,写入开机挂载(/etc/fstab)就好了。

但当你经常访问很多的文件,目录,镜像等等,而且挂载点有些是网络资源时,全部写入开机挂载项就有点不合适了。

而且全部挂载会拖慢开机速度,这时,按需自动挂载就很重要了,而autofs正可以解决这个问题。

使用方法

首先配置auto.master文件,添加挂载点(本机位置),以及对应的挂载文件(远程位置等信息)。

然后再创建一个对应点挂载配置文件,如**/etc/auto.home**,格式可以参照自带的auto.misc

配置好,重启autofs服务即可。

  • 如需使用nfs挂载,则需安装对应的支持库nfs-utils-lib

/etc/auto.master

这里我在最后添加了/home的挂载项(几乎原封不动)。

#
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
/misc   /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
#       "nosuid" and "nodev" options unless the "suid" and "dev"
#       options are explicitly given.
#
/net    -hosts
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#

/home   /etc/auto.home

+auto.master

/etc/auto.home

而这个文件就简单了,由于是自己创建的,能简则简,这里实现了对nfs的挂载。

# *可以匹配任意名称,而最后的&则是取得匹配到的名称。
# 比如我访问本机的/home/user1,当该目录没有挂载时,会自动挂载host机器上的/home/user1到本机。
*       -fstype=nfs     host:/home/&