sshfs: a hidden gem

Posted on May 3, 2011

sshfs is a proven way to mount a remote directory to a local machine. You can use it in place of ever failing NFS or permission unaware Samba. It is easy to use in today's distributions.

man sshfs

A typical way to use it is like this:

sshfs -o reconnect -o follow_symlinks -o allow_other -o uid=1001 -o gid=1001 remote-machine:/remote-dir /local-dir

That's it.

Pesky details

  1. You need to be able to ssh from the local machine to the remote machine, without a password preferably (using SSH keys). I usually use the root account to do that and restrict the root ssh access on the remote machine with

    ``AllowUsers root@local-machine-IP``
    

    in its /etc/sshd_config. You can also use a different account to do this.

  2. Permissions of the remote files will display as owned by the user with the UID 1001 (or GID) on the local machine.

  3. You may need to install the filesystem first:

    apt-get install sshfs
    

EC2 peculiarities

  1. You need to load the kernel module first in order to use sshfs on EC2 instances:

    modprobe fuse
    
  2. Add the module to /etc/modules to automatically load it on boot:

    echo fuse >>/etc/modules
    

Startup script

Create an init script to automatically mount the location automatically. This Ubuntu script uses upstart, put it into /etc/init/mount-remote.conf:

stop on runlevel 0
stop on runlevel 1
stop on runlevel 6

console output

exec sshfs -o reconnect -o follow_symlinks -o allow_other -o uid=1001 -o gid=1001 remote-machine:/remote-dir /local-dir

Enjoy!