如何将本地目录挂载到像sshfs这样的远程目录?


23

我知道sshfs用于将远程目录安装到本地,但是我需要将本地目录安装到远程fs。

我想挂载本地文件夹,例如:

/home/username/project_directory

到我可以通过ssh访问的远程计算机上,例如:

/var/www/project_directory

目的是将本地进行的编辑反映在远程文件系统上。


@quinn是可行的解决方案吗?
Konga Raju 2014年

是的,我目前正在使用它,似乎工作正常
quinn 2014年

其实,我有一个问题:superuser.com/questions/743316/...
奎因

@quinn,您应该在此处将该解决方案发布在该博客中作为答案。这个对我有用。
布里斯托斯

Answers:


19

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

怎么做?您可以通过使用登录到本地计算机上端口22的计算机上的端口10000来设置ssh转发,并使用sshfs在另一侧安装。

F.ex. 将本地计算机上的/ home / username / mywwwdevelstuff挂载到服务器端的/ var / www上:

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

3
值得一提的是,本地计算机必须正在运行ssh服务器。
Jean Carlo Machado

3

没有。

为了做到这一点,您需要做的就是颠倒您的逻辑。例如,您可以设置一个1衬板,该衬板将ssh装入远程计算机,然后使用sshfs将远程目录安装在该计算机上的本地计算机上。当然,对于NAT,防火墙规则等来说,这可能很简单,但是您没有描述使用情况。

还有其他协议,例如SMB,甚至更好的NFS,但它们也会遇到类似的问题。

您遇到的问题的核心是,一台计算机需要信任数据源,并且如果您可以远程安装一个文件系统,那将破坏互联网安全的核心原则之一。


2
我不知道你想说什么。我认为出于这个问题的目的,您可以放心地假设服务器/客户端可以使用密钥通过SSH通过彼此登录,即彼此信任。我理解问题(以及我也遇到的问题)的方式是,从客户端(动态IP,位于NAT之后的NAT,该NAT也具有动态IP,甚至可能更可怕的情况)与SSH服务器建立连接(永久访问)比其他方法容易得多。
没人

1

基于@Nobody的脚本,我用一些有用的注释对其进行了概括。下面是我的脚本。

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 
## /superuser/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 
## /superuser/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 
## /programming/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"

0

原则上,它与quinn的答案相同,但作为有效的脚本,而不是需要针对每台机器/用途进行调整的单独命令。

我不知道这样做的开销,在我看来,它像加密/解密两次一样。

#!/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" &

我禁用HostAuthenticationForLocalhost,因为显然localhost可以是任何东西。使用公钥身份验证完全安全。无论如何,您都不应该使用密码,但是即使使用密码连接到您知道可以控制的主机。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.