![在单个命令中通过中间服务器通过隧道进行 ssh 登录?](https://linux22.com/image/68966/%E5%9C%A8%E5%8D%95%E4%B8%AA%E5%91%BD%E4%BB%A4%E4%B8%AD%E9%80%9A%E8%BF%87%E4%B8%AD%E9%97%B4%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%80%9A%E8%BF%87%E9%9A%A7%E9%81%93%E8%BF%9B%E8%A1%8C%20ssh%20%E7%99%BB%E5%BD%95%EF%BC%9F.png)
有没有办法使用单个 SSH 命令通过 SSH 通过中间服务器登录到远程服务器?本质上,我需要创建一个到我的“桥接服务器”的隧道,并通过隧道登录到远程服务器。
例如,我尝试将以下内容压缩为单个 ssh 命令:
- ssh -N -L 2222:remoteserver.com:22[电子邮件受保护]
- 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
如果省略任一用户,则隐含本地用户。
间接登录主题的一个变体是间接文件传输。您可以使用scp
andrsync
间接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
答案3
如果您只需要通过另一台计算机进行 SSH 连接,则无需创建隧道或编辑 .ssh/config。
我能找到的最简单的命令是使用“jump host”ssh 参数
ssh -J [email protected] [email protected]
注意:从 2016 年 8 月 1 日发布的 OpenSSH 7.3 开始支持此功能