1. 程式人生 > >linux autofs自動掛載服務配置

linux autofs自動掛載服務配置

autofs簡介

mount是用來掛載檔案系統的,可以在系統啟動的時候掛載,也可以在系統啟動後掛載。

對於本地固定裝置,如硬碟可以使用mount掛載,而光碟、軟盤、NFS、SMB等檔案系統具有動態性,即需要的時候才有必要掛載。

光碟機和軟盤我們一般知道什麼時候需要掛載,但NFS和SMB共享等就不一定知道了,即我們一般不能及時知道NFS共享和SMB什麼時候可以掛載。

而autofs服務就提供這種功能,好像windows中的光碟機自動開啟功能,能夠及時掛載動態載入的檔案系統,免去我們手動掛載的麻煩。

要實現光碟機,軟盤等的動態自動掛載,需要進行相關的配置。

下面我們就給出配置的方法。


autofs配置

autofs的主要配置檔案有兩個,分別是/etc下的auto.masterauto.misc

其中,auto.master是起控制作用的,它定義了掛在點和automount動作的檔案。auto.misc就是automount動作的檔案

/etc/auto.master的內容如下:

#
# $Id: auto.master,v 1.4 2005/01/04 14:36:54 raven Exp $
#
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
/misc   /etc/auto.misc      --/misc是定義的自動mount的掛載點,/etc/auto.misc裡定義了mount的動作(/etc/auto.misc一般為預設的mount檔案,還可以自定義)


#
# NOTE: mounts done from a hosts map will be mounted with the
#       "nosuid" and "nodev" options unless the "suid" and "dev"
#       options are explicitly given.
#
/net    -hosts
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
 


/etc/auto.misc的內容如下:

#
# $Id: auto.misc,v 1.2 2003/09/29 08:22:35 raven Exp $
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom   --將/dev/cdrom自動掛載到/misc/cd/下
# the following entries are samples to pique your imagination
#linux -ro,soft,intr ftp.example.org:/pub/linux      --將ftp.example.org的共享目錄/pub/linux/自動掛載到/misc/linux/下
#boot -fstype=ext2 :/dev/hda1                            --將本地磁碟分割槽/dev/hda1自動掛載到/misc/boot下
#floppy -fstype=auto :/dev/fd0                            --將軟盤裝置/dev/fd0自動掛載到/misc/floppy下
#floppy -fstype=ext2 :/dev/fd0
#e2floppy -fstype=ext2 :/dev/fd0
#jaz -fstype=ext2 :/dev/sdc1
#removable -fstype=ext2 :/dev/hdd

要訪問上面的automount資源,可以用如下命令:

shell> ls /misc/cd/
shell> ls /misc/linux/
shell> ls /misc/boot/
shell> ls /misc/floppy/

這些資源只有在你試圖訪問的時候才會去自動掛載,而在一段時間之後,如果你不再使用這些資源,autofs會自動解除安裝這些資源

預設時間為5分鐘(300秒),此選項由/etc/sysconfig/autofs定義,根據需要可以修改。

/etc/sysconfig/autofs的內容如下:

#
# Define default options for autofs.
#
# MASTER_MAP_NAME - default map name for the master map.
#
#MASTER_MAP_NAME="auto.master"
#
# TIMEOUT - set the default mount timeout (default 600).
#
TIMEOUT=300
#
# NEGATIVE_TIMEOUT - set the default negative timeout for
#                    failed mount attempts (default 60).
#
#NEGATIVE_TIMEOUT=60
#
# UMOUNT_WAIT - time to wait for a response from umount(8).
#
#UMOUNT_WAIT=12
#
# BROWSE_MODE - maps are browsable by default.
#
BROWSE_MODE="no"
#
# APPEND_OPTIONS - append to global options instead of replace.
#
#APPEND_OPTIONS="yes"
#
# LOGGING - set default log level "none", "verbose" or "debug"
#
#LOGGING="none"
#
# Define base dn for map dn lookup.
#
# Define server URIs
#
# LDAP_URI - space seperated list of server uris of the form
#            <proto>://<server>[/] where <proto> can be ldap
#            or ldaps. The option can be given multiple times.
#            Map entries that include a server name override
#            this option.
#
#            This configuration option can also be used to
#            request autofs lookup SRV RRs for a domain of
#            the form <proto>:///[<domain dn>]. Note that a
#            trailing "/" is not allowed when using this form.
#            If the domain dn is not specified the dns domain
#            name (if any) is used to construct the domain dn
#            for the SRV RR lookup. The server list returned
#            from an SRV RR lookup is refreshed according to
#            the minimum ttl found in the SRV RR records or
#            after one hour, whichever is less.
#
#LDAP_URI=""
#
# LDAP__TIMEOUT - timeout value for the synchronous API  calls
#                 (default is LDAP library default).
#
#LDAP_TIMEOUT=-1
#
# LDAP_NETWORK_TIMEOUT - set the network response timeout (default 8).
#
#LDAP_NETWORK_TIMEOUT=8
#
# SEARCH_BASE - base dn to use for searching for map search dn.
#               Multiple entries can be given and they are checked
#               in the order they occur here.
#
#SEARCH_BASE=""
#
# Define the LDAP schema to used for lookups
#
# If no schema is set autofs will check each of the schemas
# below in the order given to try and locate an appropriate
# basdn for lookups. If you want to minimize the number of
# queries to the server set the values here.
#
#MAP_OBJECT_CLASS="nisMap"
#ENTRY_OBJECT_CLASS="nisObject"
#MAP_ATTRIBUTE="nisMapName"
#ENTRY_ATTRIBUTE="cn"
#VALUE_ATTRIBUTE="nisMapEntry"
#
# Other common LDAP nameing
#
#MAP_OBJECT_CLASS="automountMap"
#ENTRY_OBJECT_CLASS="automount"
#MAP_ATTRIBUTE="ou"
#ENTRY_ATTRIBUTE="cn"
#VALUE_ATTRIBUTE="automountInformation"
#
#MAP_OBJECT_CLASS="automountMap"
#ENTRY_OBJECT_CLASS="automount"
#MAP_ATTRIBUTE="automountMapName"
#ENTRY_ATTRIBUTE="automountKey"
#VALUE_ATTRIBUTE="automountInformation"
#
# AUTH_CONF_FILE - set the default location for the SASL
#                          authentication configuration file.
#
#AUTH_CONF_FILE="/etc/autofs_ldap_auth.conf"
#
# MAP_HASH_TABLE_SIZE - set the map cache hash table size.
#                       Should be a power of 2 with a ratio roughly
#                       between 1:10 and 1:20 for each map.
#
#MAP_HASH_TABLE_SIZE=1024
#
# General global options
#
# If the kernel supports using the autofs miscellanous device
# and you wish to use it you must set this configuration option
# to "yes" otherwise it will not be used.
USE_MISC_DEVICE="yes"
#
#OPTIONS=""
#


應用舉例

伺服器:192.168.78.142

客戶端:192.168.78.143

將伺服器上的/home目錄自動掛載到客戶端/app/home下,要求對掛載過來的/home目錄有讀寫功能
伺服器上的/home目錄通過nfs共享,需要將伺服器上/home目錄export出來,客戶端還需要能夠自動掛載此目錄。

1、伺服器端共享/home目錄

vi  /etc/exports

/home 192.168.78.*(rw)

2、客戶端配置automount

vi  /etc/auto.master

新增一行:

/app  /etc/auto.home     --/app是定義的自動mount的掛載點,/etc/auto.home定義了mount的動作,此檔案系統預設不存在,需要我們手工建立

在/etc下建立auto.home,並寫入一行:

home   -rw     192.168.78.142:/home/     --home即為在/app下的home目錄,因為在/etc/auto.master檔案中已經指定了掛載點了,此處掛載的是遠端目錄,若掛載本地目錄,

                                                                   不用指定ip地址,直接寫 :[要掛載的目錄] 即可

3、重啟autofs服務

service autofs restart

4、檢視是否掛載成功

cd /app/home/langkeziju                   --langkeziju是伺服器/home目錄下的目錄

ls -l                                                    --檢視下面的檔案

total 1
drwx------ 3 langkeziju root 4096 Oct 12 16:33     1.txt