Using FUSE ssh to mount/unmount a remote directory

I recently helped a colleague (who wants to remain anonymous so let’s call him Stéphanie for the sake of secrecy) installing xUbuntu OS on his computer and remarked that he was using sshfs to mount remote folders over a SSH connexion. However, the trick was used through a crappy alias included in his .bash_history, which I do not like to use. I searched for a cleaner way to achieve a similar configuration using fstab and the mount/umount commands. So, this is the story… (and btw, thank you Stéphanie for pointing out the tool).

Use case description

Suppose that we have the following situation:

  • a local computer, called daemon, on which I want to mount a remote folder. In my case, the computer runs on xUbuntu OS, version 14.04 LTS and the user is called tuxette;
  • a remote server, called far, which can be reached by a user called pro through a SSH connexion.

Installation

sshfs is simply installed on (X)Ubuntu OS using:

sudo apt-get install sshfs

Eventually, you may need to add the user (I mean, you) to the fuse group to allow you to use FUSE userland filesystem utility (let’s call the user tuxette):

sudo gpasswd -a tuxette fuse

Configuration

SSH access with public/private key pair

First, it is strongly advised that far can be accessed using public/private keys from daemon: this will prevent you from having your password prompted each time you want to mount the remote folder. For doing it,

  1. first create a public/private key pair on daemon with
    ssh-keygen -t dsa
    

    Using the default option, the keys are stored in ~/.ssh/id_dsa (private key) and ~/.ssh/id_dsa.pub (public key);

  2. copy the public key in ~/.ssh/authorized_keys on far:
    scp ~/.ssh/id_dsa.pub pro@far:.ssh/authorized_keys
    

    if the file ~/.ssh/authorized_keys does not already exists or

    ssh-copy-id -i ~/.ssh/id_dsa.pub pro@far
    

    to append the new key in ~/.ssh/authorized_keys if it has previously been created.

SSHFS configuration

If you simply modify the file /etc/fstab as described in this tutorial, you will be able to mount easily your directory but umounting it will result in the following error message:

umount: PATH-TO-LOCAL-MOUNT mount disagrees with the fstab

Solving this issue simply demands to create an alias to the command line mount.fuseusing the following command line:

sudo ln -s mount.fuse /usr/bin/mount.fuse.sshfs

Modify fstab

Suppose that you have created a local directory ~/mnt/farmount on daemon in which you want to mount the remote home folder on far (I like to have all my shared folders (remote mounted directories, owncloud, dropbox – yes, sorry, some of my colleagues force me to use dropbox… -, etc) stored in the same directory ~/mnt/ but you are free to mount the remote folder wherever you want). Then, using administrator privileges, edit the file /etc/fstab to add the following line:

pro@far:./ /home/tuxette/mnt/farmount fuse.sshfs 
  defaults,idmap=user,user,rw,noauto,follow_symlinks 0 0

Using your new configuration

… is as simple as breathing (from your user’s root directory on daemon):

mount mnt/farmount

will mount the remote directory and

umount mnt/farmount

will unmount it.