In a previous tip, I looked at the Amd automounting daemon and how to use it to provide on-demand access to remote NFS shares. Amd is purely a user-space tool, available for Linux and other UNIX operating systems. AutoFS, on the other hand, is partially user-space and partially kernel-space on Linux systems. Unlike Amd, AutoFS comes standard on almost every Linux system.
If it isn't already on your system, install the AutoFS package. Using apt, urpmi, or yum, all the required dependencies will be installed. Next, edit /etc/autofs/auto.master:
# mount point config file options
/net /etc/autofs/autofs.net —timeout=30 —ghost
/home /etc/autofs/autofs.home —timeout=60 —ghost
This edit defines two master mount points where all automounted directories will live. In this case, both /net and /home are top-level directories that will contain the automounted directories. They are defined by the /etc/autofs/autofs.net and /etc/autofs/autofs.home configuration files, respectively. Finally, the /net tree has an inactivity timeout of 30 seconds, whereas the /home tree has a timeout of 60 seconds. Both create ghost (empty) directories of the mount points listed in the configuration files. This prevents the directories from being removed when they are unmounted.
Make sure the defined top-level directories exist; you will need to create both the /net and /home directories.
The /etc/autofs/autofs.net configuration might look like the following:
# directory mount options device to mount
remote_nfs -rsize=8192,wsize=8192,soft,timeo=30,rw nfs.host:/srv
remote_smb -fstype=smbfs,credentials=/etc/smb.auth ://win.host/Share
Here, two different types of remote directories are defined. The first is a remote NFS directory, and the second is a remote Samba share. In this case, the first will be mounted as /net/remote_nfs/ and the second as /net/remote_smb/. For the Samba share, credentials are required and are defined in /etc/smb.auth, which would look like:
Make sure this file is mode 0600 and owned by root so the credentials aren't exposed.
Unlike Amd, AutoFS can also be used to automount local devices. You can use it to automount CD-ROM or floppy devices as well. For instance, to automount a floppy, you would use the following definition:
floppy -fstype=auto :/dev/fd0
Another difference from Amd is that with AutoFS, the mount program will not show remotely mounted directories. Instead, you will need to peek at /proc/mounts to see what's mounted:
# cat /proc/mounts | grep autofs
AutoFS is pretty flexible in that you can use it to automount CD-ROMs, remote NFS and Samba shares, even ISO images. Some distributions include "magic" configuration files that will automount any network mounts available on the network without any preconfiguration. Simply start AutoFS, point to one of these configuration files, and start changing to directories (for example, /net/titan if the remote host's name is titan).
Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!
Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.