how to mount local directory to remote like sshfs?

Solution 1:

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 \
                 [email protected]:~/mywwwdevelstuff www

Solution 2:

In order to avoid extra authentication and double encryption, you can reverse-forward the port to another port than 22 and run the /usr/libexec/openssh/sftp-server via the -e option of ncat on that port locally first. Then on the remote end use the 'directport' option of sshfs via the ssh command.

ncat -l -p 34567 -e /usr/libexec/openssh/sftp-server &
ssh -t -R 34568:localhost:34567 $REMOTE "sshfs localhost: $MPOINT -o directport=34568; bash"

You will get a remote shell with your local files in the $MPOINT directory and you don't need to log-in twice. How convenient is that. As a bonus, it allows mounting only once, so if someone else on the remote machine tries to open the port 34568, she will not get access to your files. Make sure the port number 34567 is not in use locally and 34568 is not in use remotely, else choose some other numbers >1024. And make sure the $MPOINT directory exists on the remote end.

Solution 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.

Solution 4:

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:   [email protected] 
## * @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 [email protected] -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"