个人工具

UbuntuHelp:Autofs

来自Ubuntu中文

跳转至: 导航, 搜索


Introduction

Automount (or autofs) is a way to mount directories on an as-needed basis. Automounts are mounted only as they are accessed, and they are unmounted after a period of inactivity. Because of this, automounting NFS/Samba shares conserves bandwidth and offers better overall performance compared to static mounts via fstab. In this howto, we will configure autofs to automount an NFS share, using a set of config files. There are other ways to configure autofs on a network (see AutofsLDAP), but config files provide the simplest setup. This howto assumes that you are already familiar with NFS exports, and that you already have a properly-functioning NFS share on your network. See NFSServerHowTo to learn how to set up such a server.

Quick note on terms

Put simply, automount and autofs are two separate technologies working in tandem to provide automatic filesystem mounting. However, for our purposes there is little need to distinguish between the two, and as such, they are used interchangeably in this howto.

Installation

Install the autofs package:

$ sudo apt-get install autofs

Configuration

Edit /etc/auto.master

To configure autofs we will edit /etc/auto.master.

$ sudo nano /etc/auto.master

Here is the sample file provided by Ubuntu:

#
# $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 --timeout=60
#/smb   /etc/auto.smb
#/misc  /etc/auto.misc
#/net    /etc/auto.net

As you can see, everything is commented out. Take note of the format used by the examples. Each mount point is associated with another configuration file. We will create a new configuration file for our NFS share(s). Add the following line at the end of /etc/auto.master:

/nfs   /etc/auto.nfs

This creates a mount point at /nfs and configures it according to the settings specified in /etc/auto.nfs (which we are about to create).

Create /etc/auto.nfs

Now we will create the file which countains our automounter map:

$ sudo nano /etc/auto.nfs

This file should contain a separate line for each NFS share. The format for a line is {mount point} [{mount options}] {location}. If you have previously configured static mounts in /etc/fstab, it may be helpful to refer to those. Remember, the mount points specified here will be relative to the mount point given in /etc/auto.master. The following line is for shares using older versions of NFS (prior to version 4):

server   server:/

This creates a new mount point at /nfs/server/ and mounts the NFS root directory exported by the machine whose hostname is server.

NFSv4

If your NFS shares use NFSv4, you need to tell autofs about that. In such a case, the above line would appear as follows:

server   -fstype=nfs4   server:/

Unmount static mounts and edit /etc/fstab

If you have previously configured the NFS shares as static mounts, now is the time to unmount them.

$ sudo umount /server

Next, remove (or comment out) their respective entries in /etc/fstab.

#server:/ /server/ nfs defaults 0 0

Reload /etc/init.d/autofs

After entering your changes, run the following command to reload autofs:

$ sudo /etc/init.d/autofs reload

Make sure it works

In order to access the share and verify that it is working properly, enter the following into a shell:

$ ls /nfs/server

If you see your NFS share listed, congratulations! You have a functioning NFS mount via autofs! If you want to learn some more advanced information, keep reading.


Advanced Information

Following the example directory structure above, if you were to enter ls /nfs into a shell, you might be surprised to see nothing listed. But remember that you need to access a directory before it is automounted. To access the share, enter ls /nfs/server. Once it has been accessed, your share will be listed only until it times out. This is good to keep in mind, as it could save you time diagnosing an autofs problem that isn't really there.

Note on /net and /smb

These two default configurations may be useful for your setup. If you have a lot of NFS or Samba shares, you may want to uncomment these lines. /net enables automounting of file systems elsewhere on the network which are exported by NFS. For example, if you have a server named fileserver with an NFS export directory called /export, you can mount it by typing in a shell command line cd /net/fileserver/export. In an environment with NFS file servers, such a configuration can be useful. /smb functions the same way but is for Samba file systems. However, if you need to authenticate before accessing the Samba share, automount will not function.

Wildcard characters

Let's say you have a directory with a number of subdirectories which you want to have automounted individually. An example of this is the /home directory, in which case /etc/auto.master might contain the following line:

/home   /etc/auto.home

If user1 is logged in, you will want to automount his home directory. However, if you create a mount point for the whole /home directory, you will also mount the home directories of every other user at the same time, thus wasting bandwidth. One solution to this would be to create separate entries for each directory, as follows:

# /etc/auto.home
user1   server:/home/user1
user2   server:/home/user2
user3   server:/home/user3

This works, but is cumbersome. Instead, you can use wildcard characters, as follows:

*   server:/home/&

The asterisk (*) is used in place of the mount point and the ampersand (&) in place of the directory to be mounted. For more detail on the use of wildcards see Using Wildcard Characters as Shortcuts in AutoFS Maps.