how to mount local directory to remote like sshfs?

24

6

I know sshfs is used to mount remote directory to local, but I need to mount local directory to remote fs.

I would like to mount a local folder such as:

/home/username/project_directory

onto a remote machine which I have ssh access to, such as:

/var/www/project_directory

The goal being that edits made locally are reflected on the remote filesystem.

Konga Raju

Posted 2013-07-06T04:20:03.073

Reputation: 341

@quinn you should post the solution in that blog as an answer here. It works for me. – brismuth – 2015-05-22T16:08:02.853

@quinn is it working solution? – Konga Raju – 2014-04-18T04:29:44.777

Yes, I'm using it currently, seems to work fine – quinn – 2014-04-18T05:14:18.740

Actually, I have one issue: https://superuser.com/questions/743316/how-can-you-execute-files-mounted-via-sshfs

– quinn – 2014-04-18T11:31:22.260

Answers

20

from: http://mysteriousswede.blogspot.com/2012/01/mount-local-directory-to-server-on.html

How to do it? You set up ssh forwarding by using port 10000 on the machine you log in on to port 22 on your local machine and use sshfs to mount in on the other side.

F.ex. to mount /home/username/mywwwdevelstuff on your local machine to /var/www on the server side:

localusername@localmachine: ssh username@server -R 10000:localmachine:22
username@server: cd /var
username@server: sshfs -p 10000 -o idmap=user,nonempty \
                 localusername@127.0.0.1:~/mywwwdevelstuff www

quinn

Posted 2013-07-06T04:20:03.073

Reputation: 345

3Its worth mentioning that the local machine must be running the ssh server. – Jean Carlo Machado – 2017-04-19T00:10:58.933

3

No.

In order to do this "all" you need to do is reverse your logic. You could, for example, set up a 1 liner which would ssh into the remote machine and then use sshfs to mount the remote directory on the local machine on that box. Of-course this might be simplistic what with NAT, firewall rules etc, but you did not describe your usage case.

There are other protocols like SMB and, even better, NFS - but they will suffer similar issues.

The core of the problem you have is that a machine needs to trust the source of the data, and if you can remotely mount a file system that would break one of the core tenets of internet security.

davidgo

Posted 2013-07-06T04:20:03.073

Reputation: 49 152

3I don't know what you are trying to say. I think for the purpose of the question you can safely assume that the server/client can log on to each other via SSH with keys, i.e. trust each other. The way I understand the question (and the problem I also have) is that creating an SSH connection from the client (dynamic IP, behind NAT which also has a dynamic IP, or possibly even more horrible cases) to the (permanently accessible) server is much easier than the other way round. – Nobody – 2016-11-29T15:34:36.077

1

Based on @Nobody's script, I generalized it with some useful comment. Below is my script.

https://gist.github.com/allenyllee/ddf9be045810572cd809ae3587a23658

#!/bin/bash

##/*
## * @Author: AllenYL 
## * @Date: 2017-11-08 11:37:31 
## * @Last Modified by:   allen7575@gmail.com 
## * @Last Modified time: 2017-11-08 11:37:31 
## */

#
# mount local directory to remote through reverse sshfs
# 
# usage:
#       ./reverse_sshfs.sh [remote_addr] [remote_ssh_port] [remote_user] [local_dir]
# 
# [local_dir] is a path relative to this script
# 
# This script will automatcally create a directory named "project_$LOCAL_USER" in remote user's home dir,
# and mount [local_dir] to this point. When exit, will umount "project_$LOCAL_USER" and deleted it.
# 

##
## linux - how to mount local directory to remote like sshfs? - Super User 
## https://superuser.com/questions/616182/how-to-mount-local-directory-to-remote-like-sshfs
##

# source directory of this script
SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

LOCAL_USER=$(whoami)
REMOTE_USER="$3"

LOCAL_DIR="$SOURCE_DIR/$4"
REMOTE_DIR="./project_$LOCAL_USER"

LOCAL_ADDR="localhost"
REMOTE_ADDR="$1"

LOCAL_PORT="22"
FORWARD_PORT="10000"
REMOTE_PORT="$2"

LOCAL_SSH="-p $FORWARD_PORT $LOCAL_USER@$LOCAL_ADDR"
REMOTE_SSH="-p $REMOTE_PORT $REMOTE_USER@$REMOTE_ADDR"

SSHFS_OPTION="-o NoHostAuthenticationForLocalhost=yes"

###############
## With ssh, how can you run a command on the remote machine without exiting? - Super User 
## https://superuser.com/questions/261617/with-ssh-how-can-you-run-a-command-on-the-remote-machine-without-exiting
##
## Here I use -t to force the allocation of a pseudo-terminal, which is required for an interactive shell. 
## Then I execute two commands on the server: first the thing I wanted to do prior to opening the interactive shell 
## (in my case, changing directory to a specific folder), and then the interactive shell itself. 
## bash sees that it has a pseudo-terminal and responds interactively.
##
###############
## Why does an SSH remote command get fewer environment variables then when run manually? - Stack Overflow 
## https://stackoverflow.com/questions/216202/why-does-an-ssh-remote-command-get-fewer-environment-variables-then-when-run-man
##
## sourcing the profile before running the command
## ssh user@host "source /etc/profile; /path/script.sh"
##
## usage:
##      ssh -t -p 88 root@10.1.53.168 -R 10000:localhost:22 \
##      "source /etc/profile; sshfs  -p 10000 allenyllee@localhost:/media/allenyllee/Project/Project/server_setup/nvidia_docker/project ./project2;bash"
## options:
##       -v Verbose 
##       -X X11 forwarding
##       -t pseudo-terminal for an interactive shell
##
ssh -X -t $REMOTE_SSH -R $FORWARD_PORT:localhost:$LOCAL_PORT \
"source /etc/profile;mkdir $REMOTE_DIR; \
sshfs $SSHFS_OPTION $LOCAL_SSH:$LOCAL_DIR $REMOTE_DIR; bash; \
umount $REMOTE_DIR; rm -r $REMOTE_DIR"

allenyllee

Posted 2013-07-06T04:20:03.073

Reputation: 111

0

In principle it's the same as quinn's answer, but as a working script instead of separate commands which need adaption for each machine/usage.

I don't know about the overhead in this, it seems to me like it encrypts/decrypts everything twice.

#!/bin/bash
# Reverse sshfs. You need ssh servers on both ends, the script logs first
# onto the remote end and then back into the local one
# Usage: sshfsr dir [user@]host:mountpoint  [options]
# [options] are passed on to the remote sshfs

set -e

LOCALPATH=$1
REMOTE=$(echo $2 | grep -o '^[^:]*')
REMOTEPATH=$(echo $2 | grep -o '[^:]*$')
ARGS=${@:3}

LOCALUSER=$(whoami)
PORT=10000

ssh $REMOTE -R $PORT:localhost:22 "sshfs -o NoHostAuthenticationForLocalhost=yes -p $PORT $ARGS $LOCALUSER@localhost:$LOCALPATH $REMOTEPATH" &

I disable HostAuthenticationForLocalhost because obviously localhost can be anything. This is perfectly safe with public key authentication. You shouldn't use passwords anyway, but even with passwords you are connecting to a host you know you control.

Nobody

Posted 2013-07-06T04:20:03.073

Reputation: 239