1. Introduction

autofs is a program for automatically mounting directories on an as-needed basis. Auto-mounts are mounted only as they are accessed, and 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.

1.1. Quick note on terms

To avoid confusion, the following terminologies will be used:

  • automount is the program used to configure a mount point for autofs. When autofs is started, an automount daemon is spawned for each map.

  • Auto-mount or auto-mounting refers to the process of automatically mounting filesystems.
  • autofs is the program that controls the operation of the automount daemons.

2. Installation

Install the autofs package either by clicking here or entering the following in a terminal window:

  • $ sudo apt-get install autofs

3. Configuration

autofs can be configured by editing configuration files. There are other ways to configure autofs on a network (see AutofsLDAP), but config files provide the simplest setup.

3.1. The Master Map File

The master configuration file for autofs is /etc/auto.master by default. Unless you have a good reason for changing this, leave it as the default.

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
  • By default, all lines are commented out by using the # character.

Each of the lines in auto.master describes a mount and the location of its map. These lines have the following format:

  • mount-point [map-type[,format]:] map [options]

3.1.1. Direct and Indirect Maps

automount maps can be direct or indirect. Indirect maps, such as those in the auto.master file shown above, create-mount points as subdirectories inside the main mount-point. For example, consider the following master map entry:

  • /smb   /etc/auto.smb

This entry in auto.master tells autofs to look in /etc/auto.smb and create mount-points in the /smb directory.

Direct maps create a mount-point at the path specified in the relevant map file. The mount-point entry in auto.master is always /-. For example, the following line instructs autofs to create a mount-point at the place specified in auto.data:

  • /-        /etc/auto.data
  • If the map file is not specified using a full local or network path, the Name Service Switch configuration will be used to locate the map, e.g.:
    /-        auto.data

3.2. Map Files

As indicated above, each autofs mount has its own map file. These files are usually named using the convention auto.<X>, where <X> can be anything as long as it matches an entry in auto.master and is valid for a file-name.

Map files take the following format:

  • key [-options] location

4. EXAMPLE: Auto-mounting an NFS share

In this howto, we will configure autofs to auto-mount an NFS share, using a set of configuration files. This howto assumes that you are already familiar with NFS exports, and that you already have a properly-functioning NFS share on your network. Go to the NFS Setup Page to learn how to set up such a server.

4.1. Edit /etc/auto.master

The following step creates a mount point at /nfs and configures it according to the settings specified in /etc/auto.nfs (which we will create in the next step).

  1. Type the following into a terminal:
    $ sudo nano /etc/auto.master
  2. Add the following line at the end of /etc/auto.master:

    /nfs   /etc/auto.nfs

4.2. Create /etc/auto.nfs

Now we will create the file which contains 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 host-name is server.

4.2.1. 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:/

The client needs the same changes to /etc/default/nfs-common to connect to an NFSv4 server.

  • In /etc/default/nfs-common we set:

    NEED_IDMAPD=yes
    NEED_GSSD=no # no is default

4.3. 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

4.4. Reload /etc/init.d/autofs

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

  • $ sudo service autofs reload

If working on an older ubuntu version, and that does not work try:

  • $ sudo /etc/init.d/autofs reload

If working in Natty, and that does not work try:

  • $ sudo /etc/init.d/autofs restart

4.5. 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.

5. 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 auto-mounted. 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.

5.1. Note on /net and /smb

These two default configurations may be useful for your set-up. If you have a lot of NFS or Samba shares, you may want to uncomment these lines. /net enables auto-mounting 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.

5.2. Wildcard characters

Let's say you have a directory with a number of subdirectories which you want to have auto-mounted 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 auto-mount 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 wild-card 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 wild-cards see Using Wild-card Characters as Short-cuts in AutoFS Maps.

You can also use variables (see autofs(5) man page) to substitute users and other parameters to be able to create generic file for multiple users. Example below is smb map that maps based on user which asks for the share using $USER variable.

* -fstype=cifs,rw,credentials=/home/$USER/.smbcredentials,iocharset=utf8,uid=$USER,gid=users,file_mode=0700,dir_mode=0700 ://server/$USER
share1 -fstype=cifs,rw,credentials=/home/$USER/.smbcredentials,iocharset=utf8,uid=$USER,gid=users ://server/share1
share2 -fstype=cifs,rw,credentials=/home/$USER/.smbcredentials,iocharset=utf8,uid=$USER,gid=users ://server/share2

6. Mounting Other Types of Files Systems

6.1. CIFS

When specifying a CIFS share in a map file, specify -fstype=cifs and precede the share location with a colon (:).

Example:

mntpoint -fstype=cifs ://example.com/shrname

Example: Mount read-write, specifying a user and group to own the files:

mntpoint -fstype=cifs,rw,uid=myuserid,gid=mygrpid ://example.com/shrname

Example: Mount read-write, specifying a username and password to use to connect to the share:

mntpoint -fstype=cifs,rw,username=myuser,password=mypass ://example.com/shrname

6.2. FUSE based file systems

FUSE based file systems are mounted by specifying -fstype=fuse. The file-system location specifies the user-space binary used to mount the file system, followed by a hash (#), followed by the location.

When specifying a FUSE file system location in map file, certain characters, notably the hash (#) and the colon (:), must be escaped by a backslash (\). The entire location must be preceded by a colon (:).

Since automount performs the mount as root it is usually necessary to specify allow_other on the mount options to allow your non-root userid to access the share.

6.2.1. SSHFS file system

SSHFS is a FUSE based file-system. In an autofs mount, the colon (:) following the server name must be escaped by a backslash (\).

You should have already set-up password-less authentication via public key encryption. Be sure that you understand the security implications of this before proceeding.

Remember, automount will mount your SSHFS file-system as root, so you need to:

  1. Copy your private key to the /root/.ssh directory. Be sure that you understand the security implications of this before proceeding.

  2. Add the necessary host keys to /root/.ssh/known_hosts.

  3. Specify the user-name used to connect

To test mounting your SSHFS file system as root issued the following:

sudo sshfs user@example.com:/ mountpoint

If the mount succeeds without prompting you for a password, you are ready to mount the file-system via autofs

Example:

mntpoint -fstype=fuse,allow_other :sshfs\#user@example.com\:/path/to/mount

Example: Mount read-write, specifying a user and group to own the files:

mntpoint -fstype=fuse,rw,uid=1000,gid=1000,allow_other :sshfs\#user@example.com\:/path/to/mount

Note that for FUSE mounts, uid and gid must be numeric ids.

7. Debugging Auto Mount Problems

If you are having trouble automounting your file systems, it may be useful to run automount in the foreground.

  1. Stop the autofs daemon

    sudo service autofs stop
  2. Run automount in the foreground with verbose information

    sudo automount -f -v
  3. From another terminal, try to mount your file-systems by changing directories into the mountpoint.
  4. Check the output from the first terminal for clues as to why the mount failed or was not attempted.

8. See Also

  • Mount - Information about the mounting process and its configuration in Ubuntu.

9. External Links

Autofs (last edited 2019-07-08 16:36:05 by tomreyn)