linux系统中部署autofs自动挂载服务

samba、nfs服务、或者其他的硬盘、光盘挂载内容均可以把挂载信息写入到自动挂载的配置文件,/etc/fstab中,以实现开机自动挂载。

自动挂载简化了挂载工作,但是长时间不使用的资源挂载在服务器上,会占用带框、增加硬件负载。

autofs服务解决的问题就是:在需要使用硬盘等的资源时,才去自动进行挂载,避免不使用时挂载对资源的浪费。

 

下面实验在一台主机中完成,PC1主机。

 1、在PC1主机中安装autofs

[root@PC1 ~]# yum install autofs.x86_64 -y
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel7                                                    | 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            rhel7          550 k
Installing for dependencies:
 hesiod          x86_64          3.2.1-3.el7               rhel7           30 k

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 579 k
Installed size: 3.6 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                               78 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 
rhel7/productid                                          | 1.6 kB     00:00     
  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!

 

2、在PC1主机中修改主配置文件

[root@PC1 ~]# vim /etc/auto.master
 1 #
  2 # Sample auto.master file
  3 # This is an automounter map and it has the following format
  4 # key [ -mount-options-separated-by-comma ] location
  5 # For details of the format look at autofs(5).
  6 #
  7 /media  /etc/iso.misc   ## /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 #
 14 /net    -hosts
 15 #
 16 # Include /etc/auto.master.d/*.autofs
 17 #
 18 +dir:/etc/auto.master.d
 19 #
 20 # Include central master map if it can be found using
 21 # nsswitch sources.
 22 #
 23 # Note that if there are entries for /net or /misc (as
…………

 

3、在PC1主机中编辑自配置文件

[root@PC1 ~]# vim /etc/iso.misc
iso -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
## iso 为指定挂载点目录,文件系统类型光盘,光盘权限 ,将要挂载的目录

 

4、在PC1主机中查看挂载情况

[root@PC1 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  3.0G   15G  17% /
devtmpfs               985M     0  985M   0% /dev
tmpfs                  994M  140K  994M   1% /dev/shm
tmpfs                  994M  8.8M  986M   1% /run
tmpfs                  994M     0  994M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot
/dev/sr0               3.5G  3.5G     0 100% /media/cdrom   
[root@PC1 ~]# ll -d /dev/cdrom 
lrwxrwxrwx. 1 root root 3 Dec 3 00:39 /dev/cdrom -> sr0  ## 说明光盘当前挂载状态

 

5、在PC1主机中启动autofs服务

[root@PC1 ~]# systemctl restart autofs
[root@PC1 ~]# systemctl enable autofs.service 
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'
[root@PC1 ~]# df -h
df: ‘/media/cdrom’: No such file or directory
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  3.0G   15G  17% /
devtmpfs               985M     0  985M   0% /dev
tmpfs                  994M  140K  994M   1% /dev/shm
tmpfs                  994M  8.8M  986M   1% /run
tmpfs                  994M     0  994M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot
/dev/sr0               3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86

 

6、 在PC1主机中进入/media目录查看

[root@PC1 ~]# cd /media/
[root@PC1 media]# df -h
df: ‘/media/cdrom’: No such file or directory
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  3.0G   15G  17% /
devtmpfs               985M     0  985M   0% /dev
tmpfs                  994M  140K  994M   1% /dev/shm
tmpfs                  994M  8.8M  986M   1% /run
tmpfs                  994M     0  994M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot
/dev/sr0               3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@PC1 media]# ls
[root@PC1 media]# cd iso
[root@PC1 iso]# ls
addons  images      Packages                 RPM-GPG-KEY-redhat-release
EFI     isolinux    release-notes            TRANS.TBL
EULA    LiveOS      repodata
GPL     media.repo  RPM-GPG-KEY-redhat-beta
[root@PC1 iso]# df -h
df: ‘/media/cdrom’: No such file or directory
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  3.0G   15G  17% /
devtmpfs               985M     0  985M   0% /dev
tmpfs                  994M  140K  994M   1% /dev/shm
tmpfs                  994M  8.8M  986M   1% /run
tmpfs                  994M     0  994M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot
/dev/sr0               3.5G  3.5G     0 100% /media/iso   ## 当访问挂载点目录时,实现了自动挂载

以上实验实现了只有在使用硬盘资源时,能够实现自动挂载。

 

上一篇:程序编译安装和磁盘


下一篇:NFS服务器搭建与autofs自动挂载