SSHFS

SSH is a highly versatile and secure protocol, allowing remote shell access (through the ssh command) and secure file transfer (through the scp command). SSHFS extends the file transfer concept to provide a filesystem client via FUSE (Filesystem in USErspace).

Assuming you already have shell access to a remote machine, it is trivial to mount a remote part of the filesystem locally and access it over the network as if it were a local filesystem, with the added security of the SSH conduit.

Installing SSHFS

aptitude install sshfs
Mounting a Remote Directory

To mount a remote

at the local mount point :

mkdir <localdir>
sshfs <user>@<host>:<dir> <localdir>

Unmounting an SSHFS mount

fusermount -u <localdir>

Automounting SSHFS

Combining AutoFS with SSHFS gives a powerful system allowing remote filesystems to be mounted automatically when required. First install AutoFS if it is not already installed

aptitude install autofs

To allow automount to mount the filesystem in the background, you must have SSH public/private key authentication set up with the remote server. Details of this procedure are widely available.

Next create a configuration file for SSHFS filesystems

touch /etc/auto.sshfs

In this file create an entry for each SSHFS filesystem you wish to automount. These lines take the form of

<mount> -fstype=fuse,<options> :sshfs\#<user>@<host>\:<dir>

and is typically rw,nodev,nonempty,allow_other,max_read=65536.

Note: The # is because FUSE wants a parameter of the form sshfs#@\:

and the \ is to escape the # since autofs will see it as the start of a comment. The spacing and position of the first colon is also important.

In /etc/auto.master add the new configuration file

/var/autofs/sshfs /etc/auto.sshfs uid=<uid>,gid=<gid>,--timeout=30,--ghost

Some of the options here are:

  • uid= – set your user ID for the mount (from /etc/passwd).
  • gid= – set your group ID for the mount (from /etc/group).
  • --timeout=30 – sets the timeout. If the filesystem is not accessed within this time autofs will unmount it.

Create a symlink to the various SSHFS mount points in /etc/auto.sshfs

ln -s /var/autofs/sshfs/<mount> /mnt/<mount-name>
Article last modified on December 27, 2015 at 5:46 pm.

Leave a Reply

Your email address will not be published.