树叶云linux教程:12.3 AutoFs自动挂载服务

无论是Samba服务还是NFS服务,都要把挂载信息写入到/etc/fstab中,这样远程共享资源就会自动随服务器开机而进行挂载。虽然这很方便,但是如果挂载的远程资源太多,则会给网络带宽和服务器的硬件资源带来很大负载。如果在资源挂载后长期不使用,也会造成服务器硬件资源的浪费。可能会有读者说,“可以在每次使用之前执行mount命令进行手动挂载”。这是一个不错的选择,但是每次都需要先挂载再使用,您不觉得麻烦吗?

autofs自动挂载服务可以帮我们解决这一问题。与mount命令不同,autofs服务程序是一种Linux系统守护进程,当检测到用户试图访问一个尚未挂载的文件系统时,将自动挂载该文件系统。换句话说,我们将挂载信息填入/etc/fstab文件后,系统在每次开机时都自动将其挂载,而autofs服务程序则是在用户需要使用该文件系统时才去动态挂载,从而节约了网络资源和服务器的硬件资源。

    [root@linuxprobe ~]# yum install autofs
    Loaded plugins: langpacks, product-id, subscription-manager
    This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
    rhel | 4.1 kB 00:00 
    Resolving Dependencies
    --> Running transaction check
    ---> Package autofs.x86_64 1:5.0.7-40.el7 will be installed
    --> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-40.el7.x86_64
    --> Running transaction check
    ---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
    --> Finished Dependency Resolution
    Dependencies Resolved
    ================================================================================
     Package Arch Version Repository Size
    ================================================================================
    Installing:
     autofs x86_64 1:5.0.7-40.el7 rhel 550 k
    Installing for dependencies:
     hesiod x86_64 3.2.1-3.el7 rhel 30 k
    Transaction Summary
    ================================================================================
    Install 1 Package (+1 Dependent package)
    Total download size: 579 k
    Installed size: 3.6 M
    Is this ok [y/d/N]: y
    Downloading packages:
    --------------------------------------------------------------------------------
    Total 9.4 MB/s | 579 kB 00:00 
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
     Installing : hesiod-3.2.1-3.el7.x86_64 1/2 
     Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2 
     Verifying : hesiod-3.2.1-3.el7.x86_64 1/2 
     Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2 
    Installed:
     autofs.x86_64 1:5.0.7-40.el7 
    Dependency Installed:
     hesiod.x86_64 0:3.2.1-3.el7 
    Complete!

处于生产环境中的Linux服务器,一般会同时管理许多设备的挂载操作。如果把这些设备挂载信息都写入到autofs服务的主配置文件中,无疑会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容,因此在autofs服务程序的主配置文件中需要按照“挂载目录 子配置文件”的格式进行填写。挂载目录是设备挂载位置的上一级目录。例如,光盘设备一般挂载到/media/cdrom目录中,那么挂载目录写成/media即可。对应的子配置文件则是对这个挂载目录内的挂载设备信息作进一步的说明。子配置文件需要用户自行定义,文件名字没有严格要求,但后缀建议以.misc结束。具体的配置参数如第7行的加粗字所示。

    [root@linuxprobe ~]# vim /etc/auto.master
    #
    # 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).
    #
    /media /etc/iso.misc
    /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 /etc/auto.master.d/*.autofs
    #
    +dir:/etc/auto.master.d
    #
    # 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.
    #
    +auto.master

在子配置文件中,应按照“挂载目录 挂载文件类型及权限 :设备名称”的格式进行填写。例如,要把光盘设备挂载到/media/iso目录中,可将挂载目录写为iso,而-fstype为文件系统格式参数,iso9660为光盘设备格式,ro、nosuid及nodev为光盘设备具体的权限参数,/dev/cdrom则是定义要挂载的设备名称。配置完成后再顺手将autofs服务程序启动并加入到系统启动项中:

    [root@linuxprobe ~]# vim /etc/iso.misc
    iso   -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
    [root@linuxprobe ~]# systemctl start autofs 
    [root@linuxprobe ~]# systemctl enable autofs 
    ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'

接下来将发生一件非常有趣的事情。我们先查看当前的光盘设备挂载情况,确认光盘设备没有被挂载上,而且/media目录中根本就没有iso子目录。但是,我们却可以使用cd命令切换到这个iso子目录中,而且光盘设备会被立即自动挂载上。我们也就能顺利查看光盘内的内容了。

    [root@linuxprobe ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/rhel-root 18G 3.0G 15G 17% /
    devtmpfs 905M 0 905M 0% /dev
    tmpfs 914M 140K 914M 1% /dev/shm
    tmpfs 914M 8.9M 905M 1% /run
    tmpfs 914M 0 914M 0% /sys/fs/cgroup
    /dev/sda1 497M 119M 379M 24% /boot
    [root@linuxprobe ~]# cd /media
    [root@linuxprobe media]# ls
    [root@linuxprobe media]# cd iso
    [root@linuxprobe iso]# ls -l
    total 812
    dr-xr-xr-x. 4 root root 2048 May 7 2017 addons
    dr-xr-xr-x. 3 root root 2048 May 7 2017 EFI
    -r--r--r--. 1 root root 8266 Apr 4 2017 EULA
    -r--r--r--. 1 root root 18092 Mar 6 2012 GPL
    dr-xr-xr-x. 3 root root 2048 May 7 2017 images
    dr-xr-xr-x. 2 root root 2048 May 7 2017 isolinux
    dr-xr-xr-x. 2 root root 2048 May 7 2017 LiveOS
    -r--r--r--. 1 root root 108 May 7 2017 media.repo
    dr-xr-xr-x. 2 root root 774144 May 7 2017 Packages
    dr-xr-xr-x. 24 root root 6144 May 7 2017 release-notes
    dr-xr-xr-x. 2 root root 4096 May 7 2017 repodata
    -r--r--r--. 1 root root 3375 Apr 1 2017 RPM-GPG-KEY-redhat-beta
    -r--r--r--. 1 root root 3211 Apr 1 2017 RPM-GPG-KEY-redhat-release
    -r--r--r--. 1 root root 1568 May 7 2017 TRANS.TBL
    [root@linuxprobe ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/rhel-root 18G 3.0G 15G 17% /
    devtmpfs 905M 0 905M 0% /dev
    tmpfs 914M 140K 914M 1% /dev/shm
    tmpfs 914M 8.9M 905M 1% /run
    tmpfs 914M 0 914M 0% /sys/fs/cgroup
    /dev/cdrom 3.5G 3.5G 0 100% /media/iso
    /dev/sda1 497M 119M 379M 24% /boot

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/156243.html<

(0)
运维的头像运维
上一篇2025-03-15 19:57
下一篇 2025-03-15 19:59

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注