当前位置: 首页 > 科技观察

Linux系统安装AutoFs挂载服务

时间:2023-03-12 21:29:55 科技观察

无论是Samba服务还是NFS服务,都必须将挂载信息写入/etc/fstab,这样服务器一开机就会自动挂载远程共享资源。虽然这很方便,但如果安装了过多的远程资源,则会对网络带宽和服务器硬件资源造成沉重负担。如果资源挂载后长期不用,也会造成服务器硬件资源的浪费。有的读者可能会说,每次使用前都可以执行mount命令进行手动挂载。这是一个不错的选择,但是每次使用前都需要先挂载,不觉得麻烦吗?autofsautomount服务可以帮助我们解决这个问题。与mount命令不同,autofs服务程序是一个Linux系统守护进程。当它检测到用户试图访问一个尚未挂载的文件系统时,它会自动挂载该文件系统。也就是说,我们将挂载信息填入/etc/fstab文件后,系统每次开机都会自动挂载,当用户需要使用文件系统时,autofs服务程序会动态挂载,从而节省网络资源和服务器硬件资源。[root@localhost~]#yuminstallautofsLoadedplugins:langpacks,product-id,subscription-manager...RunningtransactionInstalling:hesiod-3.2.1-3.el7.x86_641/2Installing:1:autofs-5.0.7-40。el7.x86_642/2验证:hesiod-3.2.1-3.el7.x86_641/2验证:1:autofs-5.0.7-40.el7.x86_642/2已安装:autofs.x86_641:5.0.7-40.el7DependencyInstalled:hesiod。x86_640:3.2.1-3.el7完成!生产环境中的Linux服务器一般会同时管理很多设备的挂载操作。如果将这些设备挂载信息写入到autofs服务的主配置文件中,无疑会使主配置文件臃肿,不利于服务执行的效率,也不利于以后修改配置内容.因此,在autofs服务程序中需要按照“挂载目录子配置文件”的格式填写主配置文件。挂载目录为设备挂载位置的上层目录。比如CD-ROM设备一般挂载在/media/cdrom目录下,所以挂载目录可以写成/media。对应的子配置文件进一步解释了该挂载目录下的挂载设备信息。子配置文件需要用户自行定义。文件名没有严格要求,但后缀必须以.misc结尾。具体配置参数在第7行加粗显示。(5)./media/etc/iso.misc/misc/etc/auto.misc##注意:mountsdonefromahostsmapwillbemountedwiththe#"nosuid"and"nodev"optionsunlessthe"suid"and"dev"#optionsareexplicitlygiven./net-hosts##包括/etc/auto.master.d/*。autofs+dir:/etc/auto.master.d##Includecentralmastermafitcanbefoundusing#nsswitchsources.##Notthatifthereareentriesfor/netor/misc(as#above)intheincludedmastermapanykeysthatarethe#samewillnotbeseenasthefirstreadkeyseentakes#precedence.+auto.master子配置文件中,应该挂载为按照“FileTypeandPermissiontoMountDirectory:DeviceName”格式填写。比如挂载一个光盘设备到/media/iso目录,可以把挂载目录写成iso,-fstype是文件系统格式参数,iso9660是光盘设备格式,ro,nosuid,和nodev是光盘设备Permission参数的具体参数,/dev/cdrom是定义要挂载的设备名称。配置完成后,启动autofs服务程序,加入系统启动项:[root@localhost~]#vim/etc/iso.misciso-fstype=iso9660,ro,nosuid,nodev:/dev/cdrom[root@localhost~]#systemctlstartautofs[root@localhost~]#systemctlenableautofsln-s'/usr/lib/systemd/system/autofs.service''/etc/systemd/system/multi-user.target.wants/autofs.service'connection接下来会发生一件很有趣的事情。我们先查看一下当前的光驱设备挂载情况,确认光驱设备没有挂载,并且/media目录下根本没有iso子目录。不过我们可以使用cd命令切换到这个iso子目录,CD设备会立即自动挂载。我们就可以顺利的看到光盘的内容了。[root@localhost~]#df-hFilesystemSizeUsedAvailUse%Mountedon/dev/mapper/rhel-root18G3.0G15G17%/devtmpfs905M0905M0%/devtmpfs914M140K914M1%/dev/shmtmpfs914M8.9M905M1%/runtmpfs914M0914M0%/sys/fs/cgroup/dev/sda1497M119M379M24%/boot[root@linuxprobe~]#cd/media[root@localhostmedia]#ls[root@localhostmedia]#cdiso[root@localhostiso]#ls-ltotal812dr-xr-xr-x.4rootroot2048May72017addonsdr-xr-xr-x。3rootroot2048May72017EFI-r--r--r--.1rootroot8266Apr42017EULA-r--r--r--.1rootroot18092Mar62012GPLdr-xr-xr-x.3rootroot2048May72017imagesdr-xr-xr-x.2rootroot2048May72017isolinuxdr-xr-xr-x.2rootroot2048May72017LiveOS-r--r--r--.1rootroot108May72017media.repodr-xr-xr-x.2rootroot774144May72017Packagesdr-xr-xr-x.24rootroot6144May72017release-notesdr-xr-xr-x.2rootroot4096May72017repodata-r--r--r--。1rootroot3375Apr12017RPM-GPG-KEY-redhat-beta-r--r--r--.1rootroot3211Apr12017RPM-GPG-KEY-redhat-release-r--r--r--.1rootroot1568May72017TRANS.TBL[root@localhost~]#df-hFilesystemSizeUsedAvailUse%Mountedon/dev/mapper/rhel-root18G3.0G15G17%/devtmpfs905M0905M0%/devtmpfs914M140K914M1%/dev/shmtmpfs914M8.9M905M1%/runtmpfs914M0914M0%/sys/media1/cdrom/iso/cdrom/G5.5/dev/dev/shmtmpfs914M8.9M905M1%dev/sda1497M119M379M24%/引导