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

Eri*_* B. 10 ssh ssh-tunneling port-forwarding

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

例如,我试图将以下内容压缩到一个 ssh 命令中:

  1. ssh -N -L 2222:remoteserver.com:22 bridge_userid@bridgemachine.com
  2. ssh -p 2222 remote_userid@localhost

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

我在我的配置中尝试了以下但无济于事:

Host axp
  User          remote_userid
  HostName      remoteserver.com
  IdentityFile  ~/.ssh/id_rsa.eric
  ProxyCommand  ssh -W %h:%p  bridge_userid@bridgemachine.com
Run Code Online (Sandbox Code Playgroud)

根据@jasonwryan 的评论和透明多路链接,我可以使用以下命令:

ssh -A -t bridge_userid@bridgemachine.com ssh -A remote_userid@remoteserver.com
Run Code Online (Sandbox Code Playgroud)

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

小智 9

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

我们的 SSH 配置设置可以自动通过 Jumpstation。

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

Host *.internal.company.com
User jacob
IdentityFile ~/.ssh/id_rsa
ProxyCommand ssh -q -A jacob@company-internal-jumphost  nc -q0 %h %p
Run Code Online (Sandbox Code Playgroud)

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

编辑:这是“内部”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
Run Code Online (Sandbox Code Playgroud)

  • 有一个基于 ProxyCommand 和 -W 选项的更优雅的解决方案,它允许您执行诸如“ssh user@hostA/hostB/hostC”之类的操作,以通过 2 个中间件连接到 hostC,请参阅:http://dmitry.khlebnikov.net /2015/08/transparent-ssh-host-jumping-advanced.html (2认同)

sta*_*fry 6

如果使用 OpenSSH 7.3 或更高版本,那么您可以ProxyJump像这样使用:

$ ssh -o ProxyJump=user1@gateway user2@remote
Run Code Online (Sandbox Code Playgroud)

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


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

使用scp以下方法通过网关进行复制:

$ scp -oProxyJump=root@gateway myfile user@remote:path
Run Code Online (Sandbox Code Playgroud)

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

ProxyJump在OpenSSH的7.3推出。另一种方法是使用ProxyCommand

$ scp -oProxyCommand="ssh -W %h:%p root@gateway" myfile user@remote:path
Run Code Online (Sandbox Code Playgroud)

使用rsync以下方法通过网关进行复制:

$ rsync -av -e 'ssh -o "ProxyJump root@gateway"' myfile user@remote@path
Run Code Online (Sandbox Code Playgroud)

或者

$ rsync -av -e 'ssh -o "ProxyCommand ssh -A root@gateway -W %h:%p"' myfile user@remote@path
Run Code Online (Sandbox Code Playgroud)

我更详细地解释了其他答案(关于超级用户),这些答案涵盖了间接scp间接rsync