
AutoFs服務(wù)程序與Mount命令不同之處在于它是一種守護(hù)進(jìn)程,只有檢測到用戶試圖訪問一個(gè)尚未掛載的文件系統(tǒng)時(shí)才自動(dòng)的檢測并掛載該文件系統(tǒng)。
Autofs非常方便,主要有兩點(diǎn):
1、設(shè)置開機(jī)不一定要掛載的目錄,當(dāng)用的時(shí)候才實(shí)現(xiàn)自動(dòng)掛載。
2、用戶不使用自動(dòng)掛載的目錄一段的時(shí)間,會(huì)自動(dòng)卸載。(默認(rèn)時(shí)間為5分鐘)
一、安裝服務(wù)
[root@localhost ~]# yum -y install autofs
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
(1/2): rhel7/group_gz | 134 kB 00:00
(2/2): rhel7/primary_db | 3.4 MB 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 3.6 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!
[root@localhost Desktop]#

二、修改配置文件(主配置+子配置)
autofs服務(wù)程序的主配置文件中需要按照“掛載目錄 子配置文件”的格式寫入?yún)?shù)。掛載目錄是設(shè)備要掛載位置的上一級(jí)目錄,例如咱們的光盤設(shè)備一般是掛載到/media/cdrom目錄中的,那么此處就應(yīng)該寫成/media即可,而對應(yīng)的子配置文件則是對這個(gè)目錄內(nèi)掛載設(shè)備信息的進(jìn)一步說明,子配置文件是需要用戶自行定義的,文件名字沒有嚴(yán)格要求,具體的配置參數(shù)如第8行加粗字所示。
[root@localhost ~]# 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 /misc /etc/auto.misc
8 /media /etc/cdrom.misc
--------省略部分輸出------------
子配置文件中應(yīng)按照“掛載目錄 掛載文件類型及權(quán)限 :設(shè)備名稱”的格式寫入?yún)?shù),例如想要把設(shè)備掛載到/media/cdrom目錄中,則此時(shí)寫iso即可,而-fstype為文件系統(tǒng)格式參數(shù),iso9660為光盤系統(tǒng)設(shè)備格式,ro、nosuid及nodev為光盤設(shè)備具體的權(quán)限參數(shù),最終/dev/cdrom則是定義要掛載的設(shè)備名稱,配置完成后順手再把a(bǔ)utofs服務(wù)程序啟動(dòng)并加入到開機(jī)啟動(dòng)項(xiàng)中吧:
[root@localhost ~]# vim /etc/cdrom.misc
cdrom -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
[root@localhost ~]# systemctl restart autofs
[root@localhost ~]# systemctl enable autofs
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'

接下來會(huì)就要發(fā)生一幕非常有趣的事情了,先來查看下當(dāng)前的設(shè)備掛載情況,確認(rèn)光盤設(shè)備目前是沒有被掛載使用的,而且在/media目錄中根本就沒有一個(gè)cdrom子目錄,但卻可以通過cd命令切換進(jìn)去,同時(shí)光盤設(shè)備會(huì)被立即自動(dòng)掛載上,咱們也就能順利的查看到光盤內(nèi)的所有內(nèi)容了。
[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@localhost ~]# cd /media
[root@localhost media]# ls
[root@localhost media]# cd cdrom
[root@localhost cdrom]# 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/cdrom
/dev/sda1 497M 119M 379M 24% /boot
- 練習(xí)題
環(huán)境:
兩臺(tái)REHLL7 linux系統(tǒng),客戶端IP:172.16.24.1,服務(wù)器IP:172.16.24.3,
實(shí)驗(yàn)步驟:
- 服務(wù)器配置NFS
- 客戶端配置Autofs
配置 autofs
描述:本機(jī) 172.16.24.1 可自動(dòng)掛載由主機(jī) 172.16.24.3 提供的家目錄: /home/guests/ldapuser1。
配置步驟:
服務(wù)器配置
- 安裝nfs-utlis包
- 修改配置文件/etc/exports,可以按照共享目錄的路徑 允許訪問的NFS資源客戶端(共享權(quán)限參數(shù))的格式來寫入?yún)?shù),定義要共享的目錄與相應(yīng)的權(quán)限。
客戶端配置 - 安裝autofs包
- 修改主配置文件和子配置文件