Autofs Ubuntu
On-demand NFS mounting utility: autofs. Learn what is autofs, why, and when to use autofs and autofs configuration steps in the Linux server.
There are two types of automounters in Linux, autofs and AMD. AMD is implemented in user space and not a part of kernel. However, autofs is a newer system assisted by the kernel and it requires kernel lever support. As said before, autofs is implemented in kernel-space, so in order to use autofs the kernel must have support compiled in. The file /etc/auto.master is the main configuration file for ‘autofs’ service, this file contains the master map lists of the mount points on the system controlled by ‘autofs’, and their corresponding configuration files or network sources which are known as ‘ automount maps ‘. This is how the file should looks like.
The first place to manage mount points on any Linux system is /etc/fstab
file. These files mount all listed mount points at the system startup and made them available to the user. Although I explained mainly how autofs
advantages us with NFS mount points, it also works well with native mount points.
NFS mount points are also part of it. Now, the issue is even if users don’t access NFS mount points they are still mounted by /etc/fstab
and leech some system resources in the background continuously. Like NFS services need to check connectivity, permissions, etc details of these mount points in the background continuously. If these NFS mounts are considerably high in numbers then managing them through /etc/fstab
will be a major drawback since you are allotting major system resource chunk to system portion which is not frequently used by users.
Why use AutoFS?
In such a scenario, AutoFS comes in picture. AutoFS is on-demand NFS mounting facility. In short, it mounts NFS mount points when a user tries to access them. Again once time hits timeout value (since last activity on that NFS mount), it will automatically un-mount that NFS mount saving system resources serving idle mount point.
It also reduces your system boot time since the mounting task is done after system boot and when the user demands it.
When use AutoFS?
- If your system is having a large number of mount points
- Many of them are not being used frequently
- The system is tight on resources and every single piece of system resource counts
AutoFS configuration steps
First, you need to install package autofs using yum or apt. The main configuration file for autofs is /etc/auto.master
which is also called a mast map file. This file has autofs controlled mount points details. The master file follows below format :
mount_point map_file options
where –
- mount_point is a directory on which mounts should be mounted
- map_file (automounter map file) is a file containing a list of mount points and their file systems from which they should be mounted
- options are extra options to be applied on mount_point
Sample master map file looks like one below :
In above sample, mount points defined under /etc/auto.misc
files can be mounted on /my_auto_mount
directory with timeout value 60 sec.
Parameter map_file (automounter map file) in the above master map file is also a configuration file which has below format :
mount_point options source_location
where –
- mount_point is a directory on which mounts should be mounted
- options are mounting options
- source_location is FS or NFS path from where the mount will be mounted
Sample automounter map file looks like one below :
Users should be aware of the share path. Means, in our case, /my_auto_mount
and Linux, data1 these paths should be known to users in order to access them.
In all both these configuration file collectively tells :
Whenever user tries to access mount point Linux or data1 –
- autofs checks data1 source (
/dev/fs0
) with option (-fstype=ext3
) - mounts data1 on
/my_auto_mount/data1
- Un-mounts
/my_auto_mount/data1
when there is no activity on mount for 60 secs
Once you are done with configuring your required mounts you can start autofs service. Reload its configurations :
That’s it! Configuration is done!
Testing AutoFS configuration
Once you reload configuration, check and you will notice autofs defined mount points are not mounted on systems (output of df -h
).
Now cd
into /my_auto_mount/data1
and you will be presented with a listing of the content of data1 from /dev/fd0
!
Another way is to use watch utility in another session and keep watch on command mount. As you execute commands, you will see mount point is mounted on system and after timeout value it’s un-mounted!
AutoFS is a program that uses the Linux kernel automounter to automatically mount filesystems on demand. It works with USB flash drives and external hard drives, network shares, CD-ROM/DVD/Blu-ray, and so on.
AutoFS works by monitoring directories on the local filesystem. Whenever a program tries to access one of those directories, AutoFS will mount something on that directory. The directories to monitor, as well as what to mount on them, are specified in the AutoFS configuration files such as /etc/autofs/autofs.master. Examples of how to configure AutoFS mounts are given down the page, under #Usage.
Installation
AutoFS requires a kernel module and a userspace program.
To mount a filesystem (whether with AutoFS, or manually), the corresponding mount helper needs to be installed at the time of mounting. For example, sys-fs/ntfs3g or something equivalent is required to mount an NTFS filesystem. Some filesystems may also require changes to the kernel configuration. Check the wiki page for the specific filesystem to see what software and configuration will be required.
Kernel configuration
The following kernel option activates the kernel functionality required for automounting.
If the option is set to M, the partition that contains the module file must already be mounted before AutoFS can work.
Userspace program
As with most Linux filesystems, in addition to the relevant option being enabled in the kernel, the userspace package must be installed to actually handle the mounting.
USE flags fornet-fs/autofsKernel based automounter
ldap | Install LDAP module |
libtirpc | Use TiRPC library instead of SunRPC |
mount-locking | Enable locking to prevent corruption of /etc/mtab in the presence of concurrent auto-mounting. If enabled, recursive auto-mounting (eg. using autofs to bind or loop mount a filesystem which is itself auto-mounted) is not possible. |
sasl | Enable SASL support in the LDAP module |
systemd | Enable use of systemd-specific libraries and features like socket activation or session tracking |
Install it with the following command:
Additional software
To be able to mount NFS file systems, the net-fs/nfs-utils package is required:
For CIFS file systems the net-fs/cifs-utils package is additionally required:
Configuration
Service
The AutoFS daemon needs to be running for automounting to work.
OpenRC
Add AutoFS to the default runlevel:
To begin using the automounter before rebooting, start it manually:
Of course it is advisable to edit the configuration files first, as described below. If AutoFS is already running when the configuration is edited, run
to make it reload the configuration.
Files
The default installation of AutoFS provides the following four configuration files:
File | Description |
---|---|
/etc/conf.d/autofs | This file can be used to pass command-line options to the automount program. Most users will not need to edit this file. |
/etc/autofs/autofs.conf | This file defines some default parameters for AutoFS, such as the location of the master map file and the default timeout that causes an inactive mount to be disconnected. Most users will not need to edit this file. |
/etc/autofs/auto.master | This is the 'master map', effectively an index to the 'map files' and other resources that tell AutoFS what to mount and where. Most users will need to edit this file. |
/etc/autofs/auto.misc | This is an example of a 'map file' which is referenced by the master map. It specifies what to mount and where to mount it. Most users will need to edit this file. |
/etc/conf.d/autofs
/etc/conf.d/autofs is the configuration file that corresponds to the initscript /etc/init.d/autofs. For basic AutoFS usage, there is no need to modify this file.
The file defines two variables:
- USE_MISC_DEVICE: If this is set to
'yes'
, the initscript will create the device file /dev/autofs. - OPTIONS: This contains command-line arguments to be passed to
automount
. Runman 8 automount
to view the manual page which lists all the allowed options.
automount
may be given one non-option argument, the filename of the master map, but for most users the default value (auto.master
) is fine. If you do want to change the master map filename, setting the master_map_name configuration variable in /etc/autofs/autofs.conf is probably a better way to do it. The rest of this page assumes the master map filename has been left at the default value./etc/autofs/autofs.conf
/etc/autofs/autofs.conf is AutoFS's own configuration file. The default installation is well commented, and the options that can be specified in this file are also documented in the manual page, which can be viewed by running
For basic AutoFS usage, there is no need to modify this file.
/etc/autofs/auto.master
/etc/autofs/auto.master is the (default) 'master map'. Each line describes an AutoFS mount.
AutoFS does not use the terms 'mount' and 'mount point' in quite the same way as
/bin/mount
or /etc/fstab.Typically, the lines in this file take the following format:
Autofs Ubuntu Server
In brief, mount-point
specifies a directory for AutoFS to watch, and map
that tells AutoFS what to mount there. For details, see #Usage below, or view the manual page by running man 5 auto.master
. Neither of these fields may contain spaces.
After handling the first two (whitespace-separated) fields as mount-point
and map
respectively, anything else on the line is treated as an option to be passed to either AutoFS's automount (if it starts with a dash), or mount (if it does not). Options passed to mount will follow the -o
switch. See #Usage for an example.
The comments in
auto.master
say the format is key [ -mount-options-separated-by-comma ] location
. As of version 5.1.2, that is wrong. That format applies to entries in map files, such as /etc/autofs/auto.misc./etc/autofs/auto.misc
/etc/autofs/auto.misc is an example of a 'map file'. Only those map files actually referenced in the master map file are actually used by AutoFS, so it is safe to rename or delete this file as long as you edit /etc/autofs/auto.master to match. You can also create additional map files following the same syntax. By convention, map files are named with the pattern /etc/autofs/auto.*.
Lines in this file take the following format:
Here key
specifies a unique key associated with the AutoFS mount, which forms part or all of the path at which the filesystem will be mounted. location
tells AutoFS what filesystem to mount there. -options
is a comma-separated list of options to pass to mount, except for some special options which are handled by AutoFS (such as fstype
). For details, see #Usage below, or view the manual page by running
Usage
AutoFS mounts are specified by lines in /etc/autofs/auto.master. As a reminder, lines in this file take the format:
/etc/autofs/auto.master
Normally, map
is the full path to a map file, such as /etc/autofs/auto.misc, which contains lines of the format:
Here the location
field takes the format host:path
. The host
component may be left empty to refer to a path on the local machine. Otherwise, the named path from the named remote host will be mounted using NFS.
-master-options
and -map-options
are parsed as described above:
-master-options
is a space-separated list of options to pass to either automount (if they begin with a dash) or mount (if not)-map-options
is a comma-separated list of options, most of which are passed to mount
There are two kinds of AutoFS mounts, direct and indirect.
Direct AutoFS mounts
For a direct AutoFS mount, the mount-point
is /-
, and key
in the map file is the full path at which the filesystem will be mounted. For example, the manual pages for AutoFS include an example like this:
/etc/autofs/auto.master
These lines tell AutoFS to watch the directory /tst/sbin. If a program tries to access anything in that directory, AutoFS will mount the directory /usr/sbin
from the remote host bogus
on the local directory /tst/sbin
. In other words, it will effectively execute the command
An example of mounting a local device might look like this:
/etc/autofs/auto.master
This will effectively execute the command
Indirect AutoFS mounts
For an indirect AutoFS mount, the mount-point
is a directory path, and map
is the full path to a file which describes rules for mounting devices inside that directory. For example, the default installation includes the following line (though it is commented out):
/etc/autofs/auto.master
This line would tell AutoFS to watch files and directories under /misc for filesystem accesses. When a program tries to access something under /misc, AutoFS will use the configuration in /etc/autofs/auto.misc to determine whether to automatically mount something. Each (non-comment, non-empty) line in that file corresponds to something that AutoFS will be able to mount under /misc. For example, the line
tells AutoFS to watch /misc/cd. When a program tries to access this directory, AutoFS will effectively run the command
root #
mount -t iso9660 -o ro,nosuid,nodev /dev/cdrom /misc/cd
Here are some other examples:
Indirect mounts allow AutoFS to use wildcards. For example, if users' home directories are stored on a different machine and mounted over NFS, AutoFS could be configured as follows:
/etc/autofs/auto.master
This way, when a user larry
logs in and accesses some files in their home directory, AutoFS will effectively run the command:
root #
mount -t nfs neighborhood:/export/home/larry /home/larry
Useful options
Autofs Mount Options
These options can be given in the master map file.
--timeout=<seconds>
specifies the number of seconds that an automounted filesystem can go unused before AutoFS unmounts it.--ghost
orbrowse
(no dash in the latter form) can be useful for indirect mounts. It causes AutoFS to create the directory on which something would be mounted when the automount daemon starts up, rather than only when the directory is accessed.
For a full description of options, run
Non-file maps
In /etc/autofs/auto.master, instead of merely specifying map
, the second column can take a more complicated form such as map-type:map
, which allows the map to be something other than a file. For instance, it can be an executable which prints out map specifications (the lines that would be included in a map file), or any of various types of databases. For a full description of recognized types, run
Autofs Ubuntu Windows 10
Simple Windows-like Samba share mounting
Here is a way to automatically mount network Samba shares, as Windows does. This configuration allows automounting a share by issuing the following command in a shell:
or navigating to /net/192.0.2.1/share in a filesystem browser or dialog. The files inside will appear as if they were located on the local machine.
For this to work, Samba must be installed and configured prior to mounting.
/etc/autofs/auto.smbm
Troubleshooting
In case of mount failure or problems use following steps to narrow the source of the issue.
Stop the autofs service:
Run the automount
daemon in the foreground to log to stderr, add the verbose option to view logging of general status and progress messages in the current running terminal:
As a regular system user mount the filesystem by changing into the directory:
Verify the output running the daemon in foreground and with verbose mode. Example failure message displayed in the output below:
External resources
- Automount mini-Howto on The Linux Documentation Project website
- AutoFS page on the Ubuntu Community Help Wiki