Automatically mounting a remote directory in Ubuntu using autofs + sshfs

  1. Install sshfs and autofs:
    sudo aptitude install sshfs autofs
  2. Add yourself to the fuse group:
    sudo gpasswd -a $USER fuse
  3. Log out and in again.
  4. Generate a private key (~/.ssh/id_dsa_open) without passphrase, and a corresponding public key (~/.ssh/id_dsa_open.pub):
    ssh-keygen -t dsa -f ~/.ssh/id_dsa_open
    (press enter to keep the passphrase empty; it has to be passphrase-less, because autofs runs as root so can't use your ssh-agent to handle the passphrase)
  5. Copy the public key into ~/.ssh/authorized_keys on the remote host:
    ssh-copy-id -i ~/.ssh/id_dsa_open.pub remoteusername@example.com
  6. Test that root can log into the remote host using the new key pair:
    sudo ssh -i ~/.ssh/id_dsa_open remoteusername@example.com
  7. Add a new entry to /etc/auto.master:
    /media/sshfs	/etc/auto.sshfs	--timeout=3600 --ghost --verbose
    (the remote folders will be mounted locally at /media/sshfs; change this if you like)
  8. Add one or more entries to /etc/auto.sshfs:
    foldername -fstype=fuse,rw,allow_other,noatime,IdentityFile=/home/localusername/.ssh/id_dsa_open :sshfs\#remoteusername@example.com\:/path/to/folder
    (replacing "foldername" (the name of the local folder that will be created), "localusername", and the sshfs information as appropriate)
  9. Restart autofs:
    sudo restart autofs
  10. Open /media/sshfs - the remote folder(s) should now be mounted there.