我如何处理Multihop SCP传输?

我想从我的机器A复制一个文件到服务器C,但只能通过服务器B访问服务器C.

而不是先传输到服务器B,login然后传输到服务器C,Is可以直接使用SCP或类似程序传输文件吗?

(Emacs tramp-mode有这个function可以远程编辑文件)。

    假设OpenSSH,在.ssh / config中添加到你的SSHconfiguration中

    Host distant ProxyCommand ssh near nc distant 22 

    这将使SSH能够通过代理附近的机器“直接”连接到远程机器。 然后它可以使用像scp和sftp这样的应用程序到远处的机器。

    为了这个工作,你需要在名为near的计算机上安装“nc”aka netcat。 但是很多现代系统已经有了。

    如果你已经记住了tar的语法和操作规则,towo的tar解决scheme对于一次性问题更有效。

    您可以将-o选项添加到scp而不是.ssh/config

     scp -o ProxyCommand="ssh $jump_host nc $host 22" $local_path $host:$destination_path 

    在这种情况下, $jump_host是你的“服务器B”。

    如果在(B)机器附近的服务器上有更新的ssh版本,那么在没有netcat的情况下,

     Host distant ProxyCommand ssh near -W distant:22 

    然而,它将要求AllowTcpForwarding在近(B)机器上是yes(默认)

    编辑:需要B上的OpenSSH 5.4+

    您可以使用类似的东西ssh到服务器B.

     ssh -L 5022:<server C IP>:22 <user_serverB>@<server B IP> 

    然后你可以使用ssh到服务器C.

     ssh -p 5022 <user_serverC>@localhost 

    同样的SCP会使用

     scp -P 5022 foo.txt <user_serverc>@localhost: 

    记住用scp和ssh使用p的正确大小写

    如果你想成为一个邪恶的人,你可以链接ssh和tar,像tar c mydir | ssh server "ssh otherserver | tar x" tar c mydir | ssh server "ssh otherserver | tar x" ,但是这可能会遇到所有问题。

    更简单的方法就是使用SSH的内置方法build立SSH隧道。 请查看联机帮助页中的-D开关,并将某个端口转发到其他服务器的ssh端口。

    你也可以做到相反,也许更容易。

    假设你有一个ssh会话打开你想要发送文件的机器。 这个最远跳的PC,我们称之为hop2。 你的“代理”主机将是hop1。 作为文件来源的PC,我们将称之为原点。

     origin:~/asdf.txt --> hop1 --> hop2:~/asdf.txt 

    您可以构build隧道,使远程PC上的本地端口可用。 因此,我们定义了一个在远程PC上打开的端口,这个端口将redirect到您在build立隧道时与您一起拖出的端口。

    在hop2上:

     ssh -R 5555:127.0.0.1:22 <hop1_user>@<hop1_IP> #this has the effect of building a tunnel from hop2 to hop1, making hop2's port 22 available on hop1 as port 5555 

    现在在打开的隧道会话中,您可以从hop1到file_origin执行相同的操作。

    在hop1上:

     ssh -R 6666:127.0.0.1:5555 <origin_user>@<origin_IP> #this has the effect of building a tunnel from hop1 to origin while also pulling the active tunnel with it, making hop1's port 5555 (hop2's port 22) available on origin as port 6666. 

    您现在从hop2隧道到hop1到原点。 巧合的是,现在端口5555和6666在源端打开,这是redirect到hop2的端口22.在这个会话中,以下两个是到hop2的有效scp路由:

    来源:

     scp -P 6666 ~/asdf.txt <hop2_user>@<127.0.0.1>:~/asdf.txt 

    通过这种方式,您可以在中间跳过任意数量的跳数,而且在连接两跳以上的情况下更容易处理。

    尝试修改以下示例openssh config,以获取可用于多个主机的设置:

     Host uat-* ProxyCommand ssh bastion-uat nc %h %p 

    这假设一组以“uat-”开头的服务器,只能通过跳箱/网关服务器“bastion-uat”访问。 如果您使用密钥login,您可能还需要添加ForwardAgent yes

    即使您需要使用证书进行身份validation(AWS环境中典型),也可能并且相对容易。

    下面的命令将从server2上的remotePath文件直接复制到localPath的计算机上。 在内部,scp请求通过server1代理。

     scp -i user2-cert.pem -o ProxyCommand="ssh -i user1-cert.pem -W %h:%p user1@server1" user2@server2:/<remotePath> <localpath> 

    反过来也可以(上传文件):

     scp -i user2-cert.pem -o ProxyCommand="ssh -i user1-cert.pem -W %h:%p user1@server1" <localpath> user2@server2:/<remotePath> 

    如果您使用密码authentication,请尝试使用

     scp -o ProxyCommand="ssh -W %h:%p user1@server1" user2@server2:/<remotePath> <localpath> 

    如果您在两台服务器上使用相同的用户凭据:

     scp -o ProxyCommand="ssh -W %h:%p commonuser@server1" commonuser@server2:/<remotePath> <localpath>