我可以将这些 SSH 隧道命令合并为一个命令吗? [英] Can I combine these SSH tunneling commands into one command?

查看:34
本文介绍了我可以将这些 SSH 隧道命令合并为一个命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 SSH 访问某个服务器的两步解决方案:

第 1 步,在 bash 中:

ssh -L 127.0.0.1:5000:server2.com:22 server1.com

第 2 步,在一个新的 bash 会话中:

ssh -P 5000 127.0.0.1 # 这让我进入 server2.com

Q1:有什么办法可以将这两个命令合并为一个 ssh 命令,然后...
Q2:我可以在我的 ~/.ssh/config 中为这个连接设置一个单一的主机条目(允许我只输入例如 ssh my-tunnel)?>

我想这归结为以某种方式链接主机.我对此很陌生,无法完全弄清楚...

解决方案

我遇到了 这个问题 并且对 ssh 支持跳转主机这一事实感到惊讶.

您可以使用单个命令连接到目标服务器,而 ssh 将负责中间跃点.

ssh -J server1.com server2.com

<块引用>

-J [user@]host[:port]通过首先与跳转主机建立 ssh 连接,然后建立 TCP 连接到目标主机从那里转发到最终目的地.可以指定多个跳跃跳跃逗号字符.这是指定 ProxyJump 配置指令的快捷方式

这里是SSH config对应的跳转主机配置:

主机跳转主机主机名 server1.com用户 $YOUR_USERNAME端口 22托管我的隧道主机名 server2.com用户 $YOUR_USERNAME端口 22ProxyJump 跳转主机

...启用命令:ssh my-tunnel

I have a two step solution to access a certain server via SSH:

Step 1, in bash:

ssh -L 127.0.0.1:5000:server2.com:22 server1.com

Step 2, in a new bash session:

ssh -P 5000 127.0.0.1  # This gets me into server2.com

Q1: Is there any way I can combine these two commands into one ssh command, and...
Q2: can I set up one single host entry in my ~/.ssh/config for this connection (allowing me to just type e.g. ssh my-tunnel)?

I suppose this comes down to chaining the hosts in some way. I am new to this and can't quite figure this out...

解决方案

I came accross this question and was surprised by the fact that ssh supports jump hosts.

You can use single command to connect to the target server while ssh will take care about intermediate hop.

ssh -J server1.com server2.com

-J [user@]host[:port] Connect to the target host by first making a ssh connection to the jump host and then establishing a TCP forwarding to the ultimate destination from there. Multiple jump hops may be specified separated by comma characters. This is a shortcut to specify a ProxyJump configuration directive

And here is the corresponding jump host configuration for SSH config:

Host jumphost
    Hostname server1.com
    User $YOUR_USERNAME
    Port 22
Host my-tunnel
    Hostname server2.com
    User $YOUR_USERNAME
    Port 22
    ProxyJump jumphost

...enabling the command: ssh my-tunnel

这篇关于我可以将这些 SSH 隧道命令合并为一个命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆