Linux部署AutoFs挂载服务

Autofs非常方便,主要有两点: 1、设置开机不一定要挂载的目录,当用的时候才实现自动挂载。 2、用户不使用自动挂载的目录一段的时间,会自动卸载。(默认时间为5分钟)

[root@localhost ~]# yum install autofs  
Loaded plugins: langpacks, product-id, subscription-manager  
......  
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@localhost ~]# 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@localhost ~]# vim /etc/iso.misc  
iso   -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom  
[root@localhost ~]# systemctl start autofs  
[root@localhost ~]# 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@localhost ~]# 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@localhost media]# ls  
[root@localhost media]# cd iso  
[root@localhost 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@localhost ~]# 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://baike.qcidc.com/6723.html

(0)
晴川运维晴川运维
上一篇 2025年6月8日
下一篇 2025年6月8日

相关推荐

  • 远程连接Linux服务器具体方法

    如何远程连接linux服务器?作为一款服务器级别的操作系统,linux充分考虑了远程登录的问题,无论是从linux、windows还是其他一些操作系统登录到linux都是非常方便的…

    Linux系统 2025年6月8日
  • 详解grep命令的排除功能

    grep命令是linux中一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。 在一个或多个文件中搜素字符串模式,如果字符串模式包括空格,也必须被引用,模式后…

    Linux系统 2025年6月8日
  • 搭建Kubernetes具体流程(上)

    Kubernetes(k8s)是自动化容器操作的开源平台,这些操作包括部署,调度和节点集群间扩展。如果你曾经用过Docker容器技术部署容器,那么可以将Docker看成Kubern…

    Linux系统 2025年6月14日
  • 通过Linux源码的方式讲解一下进程

    进程process是指正在执行的程序;是程序正在运行的一个实例。它由程序指令,和从文件、其它程序中读取的数据或系统用户的输入组成。 进程的生命周期 img 进程一般处于这四种状态中…

    Linux系统 2025年6月8日
  • 分享一下Linux运维小技巧

    Linux运维人员主要是对Linux下各种网络服务、应用系统、监控系统等进行自动化脚本开发的工作,并根据项目对系统进行性能优化,下面为大家分享一下Linux运维常用小技巧。 1、查…

    Linux系统 2025年6月12日
  • 详解Linux内存、Swap、Cache、Buffer

    在Linux系统中 buffer和cache是内存上分出来的空间,swap分的是磁盘上的空间,下面重点为大家讲解一下Linux内存、Swap、Cache、Buffer。 1. 通过…

    Linux系统 6天前
  • Linux命令行中常用的计算工具

    在使用 Linux 时,我们有时会需要做一些计算,那么我们就可能需要用到计算器。在 Linux 命令行里,有许多计算器工具,这些命令行计算器可以让我们执行科学计算、财务计算或者一些…

    Linux系统 2025年6月8日
  • Linux下安装并使用NMState网络管理器

    NMState 是一个网络管理器,允许你按照声明式方法配置主机。这意味着你通过一个北向的声明式 API 定义所需的配置状态,这个工具通过南向的提供者provider应用配置。 目前…

    Linux系统 6天前
  • APF防火墙的安装和使用

    APF(Advanced Policy Firewall)是 Rf-x Networks 出品的Linux环境下的软件防火墙,被大部分Linux服务器管理员所采用,使用iptabl…

    Linux系统 2025年6月10日
  • Linux mysql定时备份脚本

    在生产环境上,为了避免数据的丢失,通常情况下都会定时的对数据库进行备份,本篇文章重点为大家讲解一下 mysql定时备份具体方法。 1. 创建备份脚本 备份脚本: #!/bin/ba…

    Linux系统 2025年6月8日

发表回复

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