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 reload autofs

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的更多相关文章

  1. autofs自动挂载

    autofs是根据需要自动挂载,默认5分钟不使用自动卸载挂载点!nfs,smb,iso,sd*的挂载 环境:RHEL6.5/Centos6.5    172.24.0.25 01.安装autofs y ...

  2. autofs实现nfs自动挂载

    apt-get install autofs 主配置文件/etc/auto.master 副配置文件可以在之配置文件中自定义 能生效的配置文件如下例: 将/usr/local/nginx/html挂载 ...

  3. Nis+Nfs+Autofs

    Nis: NIS服务的应用结构中分为NIS服务器和NIS客户机两种角色 NIS服务器集中维护用户的帐号信息(数据库)供NIS客户机进行查询 用户登录任何一台NIS客户机都会从NIS服务器进行登录认证, ...

  4. Linux nfs+autofs 环境搭建

    两台服务器环境为centos 6.6 1.安装配置nfs 安装portmap 和  nfs [root@node0 ~]# yum install portmap [root@node0 ~]# yu ...

  5. RHCE之配置autofs远程挂载远程服务器的家目录

    [root@server0 ~]# yum -y install autofs              安装包 [root@server0 ~]# vim /etc/auto.master      ...

  6. Autofs自动挂载探讨

    Autofs介绍: mount是用来挂载文件系统的,可以在系统启动的时候挂载也可以在系统启动后挂载.对于本地固定设 备,如硬盘可以使用mount挂载:而光盘.软盘.NFS.SMB等文件系统具有动态性, ...

  7. 绑定到外部验证服务LDAP、配置 autofs

    题1:您的系统需要按照以下要求绑定到这个服务上:验证服务器的基本 DN 是: dc=xxxx,dc=xxxx,dc=xxxx. 帐户信息和验证信息都是由 LDAP 提供的.连 接 需 要 使 用 证 ...

  8. NFS使用autofs自动挂载

    NFS自动挂载设置在/etc/fstab和/etc/rc.local可能挂载不成功,假如是服务端NFS宕机还可能导致客户端无法启动,可以使用autofs实现自动挂载 安装autofs yum -y i ...

  9. 004.Autofs自动挂载

    一 安装autofs [root@imxhy data]# yum -y install autofs 二 编辑自动挂载相关配置 2.1 修改master [root@imxhy ~]# vi /et ...

随机推荐

  1. SpringBoot @Async注解失效分析

    有时候在使用的过程中@Async注解会失效(原因和@Transactional注解有时候会失效的原因一样). 下面定义一个Service: 两个异步执行的方法test03()和test02()用来模拟 ...

  2. MySQL 事务 隔离级别

    前两天面试,问到了四种隔离级别,当时觉得大多数数据库都为read committed,结果没想到mysql是个例外.在此做一下隔离级别和各种数据库锁的使用. 首先说一下ACID四大特性: 四大特性   ...

  3. Java的map键值对的用法,map的遍历,Entry对象的使用

    思路: 1.定义集合 2.存储数据 3.添加元素 4.遍历 4.1将需要遍历的集合的键封装到set集合中(这用到了entrySet方法,和Entry对象) 4.2声明迭代器或者用for增强循环 4.3 ...

  4. 大数据入门到精通1--大数据环境下的基础文件HDFS 操作

    1.使用hdfs用户或者hadoop用户登录 2.在linux shell下执行命令 hadoop fs -put '本地文件名' hadoop fs - put '/home/hdfs/sample ...

  5. 如何添加Modeling 菜单

    创建page 在protal settings的cdoform中add new item 在modeing中就可以使用该对象对应的维护页面了.

  6. bpm 学习笔记一

    名词解释: DC: Development Component WD:Web Dynpro Keep DC Local for Now

  7. phacon只能访问index action

    location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?_url=$1 last; break; } }

  8. 第二章 向量(a)接口与实现

  9. LibreOJ 6277. 数列分块入门 2

    题目链接:https://loj.ac/problem/6278 参考博客:https://blog.csdn.net/qq_36038511/article/details/79725027 这题我 ...

  10. nodejs中.npmrc文件的内容

    . nodejs安装后,使用npm安装模块的时候我出现了一个错误. getaddrinfo ENOTFOUND xxx 主要是这个配置文件的问题.搞不清楚.直接打开把文件内容删除变成 npmrc文件内 ...