Saturday, 10 May 2014

How to mount a remote file system through an intermediate machine?

Suppose that you've a remote machine from which you want to mount a file system on your local machine. However, your local machine can not connect directly to the remote machine. If you could connect directly, then you could have used sshfs to mount the file system without any problem.
In the current situation all you need to do is to create a tunnel to a third machine which will act as the middle-man. The tunnel is created with ssh command, as shown below. Before that, for further clarification on this scenario, the machines which are involved are: localpoint (which is your current local machine), endpoint (which is you machine of interest that you want to mount a file system from it) and middlepoint (which is the machine in between that have access to both machines).
First you need to create the tunnel:

ssh -f username@middlepoint -L 111:endpoint:22 -N

which basically tells to forward the local port 111 to port 22 of endpoint machine. You can use any port on your local machine as far as it is available for use.
Then mounting is performed through the tunnel:

sshfs -p 111 username@ /local_path

That's it! You've now mounted endpoint:/remote_path on your local machine localpoint:/local_path via middlepoint.

Mac users may have some issues when actually trying to access the mounted file system, most probably 'Permission Denied' problems. That could happen when Mac tries to infer the permission. To resolve this issues, when mounting the fs, ensure to pass -defer_permissions as a parameter to sshfs. Also it would be beneficial to add noappledouble option as well to tell the Mac not to write the .DS_Store files on the remote file system. In short, the final call to sshfs would be something like:

sshfs -p 111 -defer_permissions,noappledouble username@ /local_path

Further note for Mac users: it is assumed that FUSE for OS X is being used.