Linux系统安装AutoFs挂载服务

系统 Linux
无论是Samba服务还是NFS服务,都要把挂载信息写入到/etc/fstab中,这样远程共享资源就会自动随服务器开机而进行挂载。

 无论是Samba服务还是NFS服务,都要把挂载信息写入到/etc/fstab中,这样远程共享资源就会自动随服务器开机而进行挂载。虽然这很方便,但是如果挂载的远程资源太多,则会给网络带宽和服务器的硬件资源带来很大负载。如果在资源挂载后长期不使用,也会造成服务器硬件资源的浪费。

可能会有读者说,可以在每次使用之前执行mount命令进行手动挂载。这是一个不错的选择,但是每次都需要先挂载再使用,您不觉得麻烦吗?

autofs自动挂载服务可以帮我们解决这一问题。与mount命令不同,autofs服务程序是一种Linux系统守护进程,当检测到用户试图访问一个尚未挂载的文件系统时,将自动挂载该文件系统。

换句话说,我们将挂载信息填入/etc/fstab文件后,系统在每次开机时都自动将其挂载,而autofs服务程序则是在用户需要使用该文件系统时才去动态挂载,从而节约了网络资源和服务器的硬件资源。 

  1. [root@localhost ~]# yum install autofs  
  2. Loaded plugins: langpacks, product-id, subscription-manager  
  3. ......  
  4. Running transaction  
  5. Installing : hesiod-3.2.1-3.el7.x86_64 1/2  
  6. Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2  
  7. Verifying : hesiod-3.2.1-3.el7.x86_64 1/2  
  8. Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2  
  9. Installed:  
  10. autofs.x86_64 1:5.0.7-40.el7  
  11. Dependency Installed:  
  12. hesiod.x86_64 0:3.2.1-3.el7  
  13. Complete! 

处于生产环境中的Linux服务器,一般会同时管理许多设备的挂载操作。如果把这些设备挂载信息都写入到autofs服务的主配置文件中,无疑会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容,因此在 autofs 服务程序的主配置文件中需要按照“挂载目录 子配置文件”的格式进行填写。挂载目录是设备挂载位置的上一级目录。

例如,光盘设备一般挂载到/media/cdrom目录中,那么挂载目录写成/media即可。对应的子配置文件则是对这个挂载目录内的挂载设备信息作进一步的说明。子配置文件需要用户自行定义,文件名字没有严格要求,但后缀必须以.misc结束。具体的配置参数如第7行的加粗字所示。 

  1. [root@localhost ~]# vim /etc/auto.master  
  2.  
  3. # Sample auto.master file  
  4. # This is an automounter map and it has the following format  
  5. # key [ -mount-options-separated-by-comma ] location  
  6. # For details of the format look at autofs(5).  
  7. /media /etc/iso.misc  
  8. /misc /etc/auto.misc  
  9.  
  10. # NOTE: mounts done from a hosts map will be mounted with the  
  11. # "nosuid" and "nodev" options unless the "suid" and "dev"  
  12. # options are explicitly given.  
  13. /net -hosts  
  14.  
  15. # Include /etc/auto.master.d/*.autofs  
  16. +dir:/etc/auto.master.d  
  17.  
  18. # Include central master map if it can be found using  
  19. # nsswitch sources.  
  20.  
  21. # Note that if there are entries for /net or /misc (as  
  22. # above) in the included master map any keys that are the  
  23. # same will not be seen as the first read key seen takes  
  24. # precedence.  
  25. +auto.master 

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

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

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

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

 

责任编辑:庞桂玉 来源: 良许Linux
相关推荐

2019-05-08 13:18:22

Linux网络文件系统系统运维

2010-02-24 15:56:48

Linux系统

2020-03-26 22:12:49

Linux系统服务器

2021-03-29 10:29:09

LinuxdockerLinux系统

2009-09-08 15:06:45

NFS服务器

2024-01-04 17:03:43

Linux操作系统硬盘

2009-12-17 17:19:45

Linux系统

2018-07-05 10:27:05

Linux系统数据盘

2010-11-09 13:53:33

2010-02-04 09:57:06

Linux mount

2009-12-02 15:46:36

Linux系统挂接

2023-09-27 23:19:04

Linuxmount

2011-08-18 15:21:37

autofs中文man

2015-05-29 13:22:10

Linux挂载运维

2009-12-17 17:41:52

2011-08-23 10:08:25

automount中文man

2014-08-04 11:22:21

linuxsamba服务器

2010-03-15 16:30:53

Ubuntu Linu

2010-03-02 15:09:26

Linux mount

2021-05-31 07:50:59

Linux文件系统
点赞
收藏

51CTO技术栈公众号