在单个命令中通过中间服务器通过隧道进行 ssh 登录?

在单个命令中通过中间服务器通过隧道进行 ssh 登录?

有没有办法使用单个 SSH 命令通过 SSH 通过中间服务器登录到远程服务器?本质上,我需要创建一个到我的“桥接服务器”的隧道,并通过隧道登录到远程服务器。

例如,我尝试将以下内容压缩为单个 ssh 命令:

  1. ssh -N -L 2222:remoteserver.com:22[电子邮件受保护]
  2. ssh -p 2222 远程用户ID@localhost

目前这可行,但我宁愿能够将所有内容压缩到一个命令中,这样如果我退出 ssh shell,我的隧道会同时关闭。

我在我的配置中尝试了以下操作,但没有成功:

Host axp
  User          remote_userid
  HostName      remoteserver.com
  IdentityFile  ~/.ssh/id_rsa.eric
  ProxyCommand  ssh -W %h:%p  [email protected]

根据@jasonwryan 评论和透明多跳链路,我能够使以下命令正常工作:

ssh -A -t [email protected] ssh -A [email protected]

但现在我想将其整齐地打包到我的 .ssh/config 文件中,并且不太确定我需要使用什么作为我的 ProxyCommand。我在网上看到了几个链接以及 @boomshadow 的答案,需要nc,但不幸的是,我用作桥接机的 AIX 服务器上没有安装 netcat。

答案1

ProxyCommand 正是您所需要的。在我的公司,所有 DevOps 技术人员都必须使用“跳转站”才能访问 VPC 的其余部分。跳转站受 VPN 访问控制。

我们已经将 SSH 配置设置为自动通过跳转站。

这是我的 .ssh/config 文件的编辑版本:

Host *.internal.company.com
User jacob
IdentityFile ~/.ssh/id_rsa
ProxyCommand ssh -q -A jacob@company-internal-jumphost  nc -q0 %h %p

每次我对“内部”子域上的服务器进行“ssh”操作时,它都会首先自动跳过跳转站。

编辑: 这里是全部的“内部”VPC 的 .ssh/config 部分供我们登录:

# Internal VPC
Host company-internal-jumphost
   Hostname 10.210.x.x  #(edited out IP for security)
   IdentityFile ~/.ssh/id_rsa
Host 10.210.*
   User ubuntu
   IdentityFile ~/.ssh/company-id_rsa
   ProxyCommand ssh -q -A jacob@company-internal-jumphost  nc -q0 %h %p
Host *.internal.company.com
   User jacob
   IdentityFile ~/.ssh/id_rsa
   ProxyCommand ssh -q -A jacob@company-internal-jumphost  nc -q0 %h %p

答案2

如果使用 OpenSSH 7.3 或更高版本,则可以ProxyJump这样使用:

$ ssh -o ProxyJump=user1@gateway user2@remote

如果省略任一用户,则隐含本地用户。


间接登录主题的一个变体是间接文件传输。您可以使用scpandrsync间接ssh通过中间服务器复制文件。

要通过网关复制,请使用scp

$ scp -oProxyJump=root@gateway myfile user@remote:path

如果user省略,则使用本地用户。

ProxyJump是在 OpenSSH 7.3 中引入的。另一种方法是使用ProxyCommand

$ scp -oProxyCommand="ssh -W %h:%p root@gateway" myfile user@remote:path

要通过网关复制,请使用rsync

$ rsync -av -e 'ssh -o "ProxyJump root@gateway"' myfile user@remote@path

或者

$ rsync -av -e 'ssh -o "ProxyCommand ssh -A root@gateway -W %h:%p"' myfile user@remote@path

我解释一下其他答案(关于极好的用户)覆盖间接scp间接rsync更详细地说。

答案3

如果您只需要通过另一台计算机进行 SSH 连接,则无需创建隧道或编辑 .ssh/config。

我能找到的最简单的命令是使用“jump host”ssh 参数

ssh -J [email protected] [email protected]

注意:从 2016 年 8 月 1 日发布的 OpenSSH 7.3 开始支持此功能

http://www.openssh.com/txt/release-7.3

相关内容