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. linux mysqlERROR 1045 (28000): linux忘记数据库密码

    已验证没问题 #1.停止mysql数据库(确定能停止掉,不然第二部有问题) /etc/init.d/mysqld stop   #2.执行如下命令 mysqld_safe --user=mysql - ...

  2. sass 使用clac的问题

    最后在github的issue中找到了方法,要想在sass的calc中使用变量,必须对这个变量使用sass的插值方法(#{$variable}). 所以把代码改正下面的形式就可以了: width: c ...

  3. yyyy-MM-dd 转换为年月日

      yyyy-MM-dd 转换为年月日   先用parse转成date型,再用format转成string. Date date = new SimpleDateFormat("yyyy-M ...

  4. JVM 图解--1.6,1.7,1.8

  5. Spring-data-redis redis

    什么是spring-data-redis spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作,使用java操作redis最常用 ...

  6. Executors与ThreadPoolExecutor

    最近阿里发布的 Java开发手册中强制线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗 ...

  7. NBU 还原LINUX ORACLE 数据库(EHR)

    一.E-HR数据库(全备)恢复 目录 一.E-HR数据库(全备)恢复... 1 1. 使用bplist 命令读取备份文件... 1 2. 启动到nomount状态... 2 3. 利用rman还原控制 ...

  8. LINUX 设置 backspace为删除键

    描述 :在linux/unix平台上的 sqlplus中,如果输错了字符,要想删除,习惯性的按下backspace键后,发现非但没有删除想要删掉的字符,还多出了两个字符^H. 原因:由于终端默认ctr ...

  9. cdh 安装系列3--cdh manager 安装 cdh 6.01 版本

    安装前提是cdh manager 已经可以通过admin登录,管理台安装在192.168.20.163 一.安装自动TLS Setup Auto-TLS 1.ssh 192.168.20.163 2. ...

  10. kali装virtualbox

    系统换成了kali,因为有一些windows上的软件需要使用,于是在kali上安装virtualbox虚拟机,爬了不少坑费了不少劲终于安装好了. 1.首先下载virtualbox:https://ww ...