Wednesday, May 28, 2008

automount with sshfs and sshpass

sshfs is great for mounting a remote filesystem and having it appear as a local drive. The major drawback is that it is difficult to automatically mount with sshfs at startup without using ssh keys. Using sshpass, I have worked out a solution.

For this example, let's assume the following parameters:
  • username on remote host: user
  • password on remote host: password
  • remote host: host.com
  • directory: /directory
  • mount point: /media/ssh
In order to accomplish the mount, you will need to create two files:
  1. A file containing just your password, followed by a newline. In the example, I will assume this file is located at /home/user/.pwd. If your password is "password", it should look like this:

    password

  2. A bash script that indicates how to use sshpass. In the example, I will assume that this file is located at /home/user/ssh.sh. Note that the location of your .pwd file must be included after the -f option. This file must be made executable.

    #!/bin/bash
    sshpass -f /home/user/.pwd ssh $*

To mount this from the command line, you could issue the following command:

sudo mount -t fuse sshfs#user@host.com:/directory /media/ssh -o ssh_command=/home/user/ssh.sh

However, the title of this post is automounting, so let's do that. To automount this ssh server, you can add a line like this to your /etc/fstab:

sshfs#user@host.com:/directory /media/ssh fuse ssh_command=/home/user/ssh.sh 0 0

That's it. Pretty cool. Hope you find it as useful as I do.

1 comment:

Unknown said...

Excellent tuto.
Thanks a lot.
The first that worked for me.

Just a note:
You have to install sshpass (apt-get install sshpass) in order to make it works.. well on my Debian Lenny.

Kenny