Autofs Sftp

broken image



Contents

  1. Introduction
  2. Configuration
  3. EXAMPLE: Auto-mounting an NFS share
  4. Advanced Information
  5. Mounting Other Types of Files Systems

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

  1. 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.
  2. Autofs is a program that automatically mounts specified directories on an on-demand basis. It is based on a kernel module for high efficiency, and can manage both local directories and network shares. These automatic mount points are mounted only when they are accessed, and unmounted after a certain period of inactivity. This on-demand behavior saves bandwidth and results in better performance.

The basic format of one line in such maps is: key -options location. For indirect mounts this is the part of the path name between the mount point and the path into the filesystem when it is mounted. Usually you can think about the key as a sub-directory name below the autofs managed mount point. For direct mounts this is the full path.

To avoid confusion, the following terminologies will be used:

Autofs sftp command
  • 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.

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

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:

  • 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:

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:

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:

  • 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.:

3.2. Map Files

As indicated above, each autofs mount has its own map file. These files are usually named using the convention auto., where 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:

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:
  2. Add the following line at the end of /etc/auto.master:

4.2. Create /etc/auto.nfs

Now we will create the file which contains our automounter map:

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):

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: Midnight custom themes for sketch 2 0.

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

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

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.

Next, remove (or comment out) their respective entries in /etc/fstab.

4.4. Reload /etc/init.d/autofs

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

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

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

4.5. Make sure it works

Autofs fstab
  • 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.

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

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:

  • 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:

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:

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:

  • 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.:

3.2. Map Files

As indicated above, each autofs mount has its own map file. These files are usually named using the convention auto., where 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:

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:
  2. Add the following line at the end of /etc/auto.master:

4.2. Create /etc/auto.nfs

Now we will create the file which contains our automounter map:

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):

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: Midnight custom themes for sketch 2 0.

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

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

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.

Next, remove (or comment out) their respective entries in /etc/fstab.

4.4. Reload /etc/init.d/autofs

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

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

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

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:

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.

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:

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:

This works, but is cumbersome. Instead, you can use wild-card characters, as follows:

Autofs Sftp Server

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.

6.1. CIFS

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

Example:

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

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

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.

Autofs Sftp Login

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

Autofs Sftp Command

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:

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

Example:

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

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

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

  1. Stop the autofs daemon

  2. Run automount in the foreground with verbose information

  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.
  • Mount - Information about the mounting process and its configuration in Ubuntu.

Autofs Sftp File

  • The mount man page - hosted on the official Ubuntu website.

This is awesome. Rhinoceros 5 5 4 x 8.

I worked on this for something like 2 hours this afternoon, and finally tracked down all the nuances to get it working. I'm really pleased with the results, and hope that they can be of some to use to you as well, because I could not find a decent tutorial on this subject despite extensive Googling.

The Problem: Connect to a remote filesystem over SSH

Odds are if you've stumbled on this tutorial, you already know the problem: You want to access a remote file system over SSH. You want to use FUSE SSHFS, and you don't want to ever have to think about it, so you're looking for Autofs integration. To keep this to the point, I'm going to skip over the installation of these packages and just explain the configuration, especially since installation is very distribution specific. I'll simply say on my system (Ubuntu Feisty) it consisted of:

The Solution

Getting SSHFS to work with Autofs really isn't hard, you just need the magic configuration. Here's how I got things working for me:

  1. Set up certificate authentication for your localroot to the remote account on the remote machine, by use of sudo ssh-keygen locally, and the (remote account's) ~/.ssh/authorized_keys file.
  2. Test the certificate authentication by verifying that the following command does not prompt for your remote password:
  3. Test that sshfs can establish the requisite connection:

    Note that the : is required after the host to specify the remote directory. (: alone means the remote user's home. :/remote/path indicates a remote path.)

  4. Add the following line to your /etc/auto.master file:

    Where /mnt/ssh is the path you want all ssh automounts to appear in,
    1000 is the UID of the user you want the sshfs mount to belong to (i.e., be writable by),
    1000 is the GID of the user you want the sshfs mount to belong to, and
    30 is the timeout in seconds to keep the FUSE connection alive.

  5. Copy the following into a new file /etc/auto.sshfs:

    This creates two sshfs mappings (obviously, adding or removing lines creates more or fewer mappings).
    The first will be at /mnt/ssh/remote1, and map to the home directory of remoteuser on the host remotehost1.
    The second will be at /mnt/ssh/remote2, and map to the directory /remote/path on the host remotehost2, with the permissions of the user remoteuser2.
    Note the characters to escape # and : These escape characters are what took me two hours to track down: FUSE requires a parameter of the form: sshfs#user@host:directory, but autofs treats everything following a # as a comment, and the : character has a special meaning. These characters must be escaped by a

  6. Restart autofs to reload the configuration files:
  7. Test it out! As root or the user indicated by uid above, run:

    You should be greeted by the contents of the remote file system. Congratulations!

The Problems

  • This exact setup only works for one user due to specifying a uid. This is fine for a home desktop system, but will likely need further work to allow multiple users access to the remote filesystem. Perhaps careful usage of gid could alleviate this problem, though logging into the remote machine as a specific user still represents a security risk.
  • I have not examined the architecture enough since I am only seeking to enable my home desktop system, so I cannot vouch for the security of this setup whatsoever. For example, the use of the allow_other option for FUSE may have security consequences since the mountpoint is created as root (to my understanding, at least).




broken image