深入理解与实践:Linux下的autofs自动挂载技术

时间:2025-05-16 关注公众号 来源:网络

在Linux系统管理中,自动化管理存储设备的挂载与卸载是一个提高系统效率和便捷性的重要环节。autofs,作为Linux环境下的一个强大工具,能够根据需要自动挂载网络文件系统或本地文件系统,极大地简化了用户和管理员的操作流程。本文将深入浅出地探讨autofs的工作原理,配置步骤以及最佳实践,帮助用户掌握如何有效地利用autofs实现资源的按需访问,从而优化系统资源管理,减少不必要的手动干预,提升系统的自动化水平。无论是对于Linux初学者还是经验丰富的系统管理员,理解并掌握autofs的使用方法都是提升系统运维能力的关键一步。我们将从autofs的基本概念入手,逐步展开其配置文件的解析、服务的启动与管理,以及常见问题的解决策略,确保您能灵活运用这一利器。

  

linux实现自动挂载autofs的方法详解

  目录   

实现自动挂载-autofs

  

autofs工具简单使用

  

autofs配置详细说明

  

自动挂载资源有两种格式

  

优化Linux系统性能

  

安装Tuned

  

选择调整配置文件

  

检查系统推荐的调整配置文件

  实现自动挂载-autofs   

autofs服务可以实现对外部设备、NFS共享目录等的自动挂载,并在空闲5分钟后自动卸载。

  

相关包和文件:

  

软件包:autofs

  

服务文件:/usr/lib/systemd/system/autofs.service

  

配置文件:/etc/auto.master

  autofs工具简单使用 #安装autofs工具   [root@rhel82~]#yuminstall-yautofs   

启动autofs服务

[root@rhel82~]#systemctlstartautofs

autofs服务启动后会生成/misc/cd目录,设置虚拟机连接光盘,实现自动挂载系统光盘

[root@rhel82~]#ll/misc/   总用量0

[root@rhel82~]#cd/misc/cd   [root@rhel82cd]#df-h   文件系统容量已用可用已用%挂载点   devtmpfs1.9G01.9G0%/dev   tmpfs2.0G02.0G0%/dev/shm   tmpfs2.0G10M2.0G1%/run   tmpfs2.0G02.0G0%/sys/fs/cgroup   /dev/nvme0n1p525G4.4G21G18%/   /dev/nvme0n1p21014M208M807M21%/boot   tmpfs392M1.2M391M1%/run/user/42   tmpfs392M4.6M387M2%/run/user/0   /dev/sr07.9G7.9G0100%/misc/cd

[root@rhel82~]#rpm-qlautofs   [root@rhel82~]#rpm-qcautofs   登录后复制autofs配置详细说明   

查看帮助:man5autofs

  自动挂载资源有两种格式   

相对路径挂载法

  

将mountpoint挂载点路径分为dirname和basename分别配置,可能会影响现有的目录结构

#例如挂载光盘:mount/dev/sr0/mnt/sr0,其中/mnt目录为dirname,/mnt/sr0为basename,相当于/mnt/sr0=/dirname/basename   登录后复制

autofs主配置文件/etc/auto.master格式

  

挂载点的dirname指定目录的配置文件路径,如:/etc/test.auto

  

指定子配置文件格式/etc/test.auto

  

挂载点的basename挂载选项选项设备

  

注意:autofs配置的dirname目录和basename目录不需要手动创建,会覆盖已存在挂载点的dirname目录下原数据

  

autofs默认提供挂载光盘范例

[root@Centos8~]#cat/etc/auto.master   /misc/etc/auto.misc

[root@centos8~]#cat/etc/auto.misc   cd-fstype=iso9660,ro,nosuid,nodev:/dev/cdrom

特殊写法:挂载点dataname和挂载目录dataname相同,即:mount10.0.0.18:/data/www/misc/www

  • -fstype=nfs10.0.0.18:/data/&   登录后复制

    范例:利用autofs自动挂载NFS

    #服务端和客户端安装nfs-utils工具包   [root@server~]#yuminstall-ynfs-utils   [root@client~]#yuminstall-ynfs-utils   [root@server~]#mkdir/nfs   [root@server~]#cp/etc/passwd/nfs/

centos6系统nfs服务叫做nfs.service

centos7系统上nfs.service和nfs-server.service同一个服务

centos8只有nfs-server.service服务

[root@server~]#systemctlstartnfs

centos7系统可以解决服务之间依赖关系,并且nfs服务启动会自动启动rpcbind.service

[root@server~]#systemctlstatusrpcbind

[root@server~]#vim/etc/exports   /nfs*(rw)

[root@server~]#exportfs-r   [root@server~]#exportfs-v   /nfs(sync,wdelay,hide,no_subtree_check,sec=sys,rw,root_squash,no_all_squash)

[root@server~]#systemctlrestartnfs   [root@server~]#showmount-e192.168.192.128   Exportlistfor192.168.192.128:   /nfs*

[root@client~]#showmount-e192.168.192.128   Exportlistfor192.168.192.128:   /nfs*

[root@client~]#mkdir/opt/nfs   [root@client~]#mount192.168.192.128:/nfs/opt/nfs/   [root@client~]#df-h|grepnfs   192.168.192.128:/nfs62G1.7G61G3%/opt/nfs

编写autofs主配置文件

[root@client~]#vim/etc/auto.master   /opt/etc/auto.master.d/auto.nfs

编写子配置文件

[root@client~]#vim/etc/auto.master.d/auto.nfs   nfs-fstype=nfs192.168.192.128:/nfs

挂载点/dirname是/目录,查看autofs配置未生效,/目录数据

[root@client~]#cp/root/anaconda-ks.cfg/opt/   [root@client~]#ll/opt/   总用量4   -rw-------.1rootroot145312月504:03anaconda-ks.cfg

如果修改主配置文件需要重启服务

[root@client~]#systemctlrestartautofs

一旦重启autofs服务,挂载dirname目录属于autofs服务管理,源数据不存在

[root@centos8~]#ll/opt/   total0

cd进入指定挂载点,autofs就会自动挂载

[root@client~]#ls/opt/   [root@client~]#cd/opt/nfs   [root@clientnfs]#ls   passwd

[root@clientnfs]#df-h|grepnfs   192.168.192.128:/nfs62G1.7G61G3%/opt/nfs   登录后复制

绝对路径挂载法

  

直接匹配全部的绝对路径名称,都写入到指定的配置文件里,不会影响本地目录结构

  

autofs主配置文件/etc/auto.master格式

  

/-指定目录的配置文件路径(使用/-表示使用绝对目录)

  

指定子配置文件格式/etc/test.auto

  

挂载点绝对路径挂载选项选项设备

  

范例

[root@client~]#vim/etc/auto.master   /-/etc/auto.master.d/auto.nfs

[root@client~]#vim/etc/auto.master.d/auto.nfs   /opt/nfs-fstype=nfs192.168.192.128:/nfs

autofs服务使用绝对路径自动挂载,不会覆盖原数据

[root@client~]#systemctlstartautofs   [root@client~]#ll/opt/   总用量4   -rw-------.1rootroot145312月504:03anaconda-ks.cfg   drwxr-xr-x.2rootroot2012月419:39nfs

[root@client~]#cd/opt/nfs/   [root@clientnfs]#ls   passwd   登录后复制优化Linux系统性能   

使用tuned-adm命令优化Linux系统性能。作为系统管理员,可以通过调整各种设置来优化Linux系统的性能,以适应当前用例的工作负载,帮助提升Linux的性能。

  

可以调整的可用配置文件:

  balanced:非常适合在节能和性能之间寻求折衷的系统。   desktop:源自平衡配置文件,提供交互式应用程序的更快响应。   throughput-performance:调整系统以获得最大吞吐量。   latency-performance:对于要求低延迟,以功耗为代价的服务器系统的理想选择。   network-latency:源自延迟性能配置文件,它启用其他网络调整参数以提供较低的网络延迟。   network-throughput:从吞吐量性能概要文件得出,附加的网络调整参数适用于最大的网络吞吐量。   powersave:调整系统以最大程度地节省电力。   oracle:基于吞吐量性能概要文件针对Oracle数据库负载进行了优化。   virtual-guest:优化以在虚拟访客中运行。   virtual-host:如果用于运行KVMguest虚拟机,请调整系统以获得最佳性能。   安装Tuned [root@rhel82~]#yuminstalltuned-y

[root@rhel82~]#systemctlstatustuned   登录后复制选择调整配置文件   

调整的配置文件包含性能提升配置文件,性能提升配置文件包括侧重于:存储和网络的低延迟、高吞吐量的存储和网络、虚拟主机性能、虚拟机性能的配置文件。

  

我们将使用tuned-adm命令来选择和应用这些配置文件。

  

检查当前活动的调优配置文件:

[root@rhel82~]#tuned-admactive   Currentactiveprofile:virtual-guest   登录后复制

可以使用更多配置文件,如下:

[root@rhel82~]#tuned-admlist   Availableprofiles:

  • accelerator-performance-ThroughputperformancebasedtuningwithdisabledhigherlatencySTOPstates
  • balanced-Generalnon-specializedtunedprofile
  • desktop-OpTIMizeforthedesktopuse-case
  • hpc-compute-OptimizeforHPCcomputeworkloads
  • intel-sst-ConfigureforIntelSpeedSelectBaseFrequency
  • latency-performance-Optimizefordeterministicperformanceatthecostofincreasedpowerconsumption
  • network-latency-Optimizefordeterministicperformanceatthecostofincreasedpowerconsumption,focusedonlowlatencynetworkperformance
  • network-throughput-OptimizeforStreamingnetworkthroughput,generallyonlynecessaryonoldercpusor40G+networks
  • optimize-serial-console-Optimizeforserialconsoleuse.
  • powersave-Optimizeforlowpowerconsumption
  • throughput-performance-Broadlyapplicabletuningthatprovidesexcellentperformanceacrossavarietyofcommonserverworkloads
  • virtual-guest-OptimizeforrunninginsIDEAvirtualguest
  • virtual-host-OptimizeforrunningKVMguests   Currentactiveprofile:virtual-guest登录后复制

    tuned-adm配置文件命令用于将活动配置文件切换到其他配置文件,此示例将调整我们的系统以实现最大吞吐量:

    [root@rhel82~]#tuned-admprofilethroughput-performance   登录后复制

    确认当前配置文件:

    [root@rhel82~]#tuned-admactive   Currentactiveprofile:throughput-performance   登录后复制检查系统推荐的调整配置文件   

    tuned-adm命令还可以建议系统的调整配置文件,这基于各种系统特征,包括系统是否为虚拟机以及在系统安装期间选择的其他预定义类别:

    [root@rhel82~]#tuned-admrecommend   virtual-guest   登录后复制

    然后,可以将个人资料设置为推荐值:

    [root@rhel82~]#tuned-admprofilevirtual-guest   登录后复制

    查看配置文件详细信息,请运行:

    [root@rhel82~]#tuned-admprofile_infovirtual-guest   Profilename:   virtual-guest

Profilesummary:   Optimizeforrunninginsideavirtualguest

Profiledescription:   登录后复制

关闭已调优的调整活动:

[root@rhel82~]#tuned-admoff

[root@rhel82~]#tuned-admactive   Nocurrentactiveprofile.   登录后复制

以上就是Linux实现自动挂载autofs的方法详解的详细内容,更多请关注其它相关文章!

阅读全文
扫码关注“ 多特资源库
更多更全的软件资源下载
文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)
玩家热搜

相关攻略

正在加载中
版权
版权说明

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)

电话:13918309914

QQ:1967830372

邮箱:rjfawu@163.com

toast