Linode 服务器 IP 被墙,通过 ssh jump host 参数一键直连

更新日期: 2024-06-11 阅读次数: 423 字数: 623 分类: Linux

问题现象

新购的 Linode 海外服务器,发现无法通过 ssh 登录。显示错误:

ssh: connect to host x.x.x.x port 22: Resource temporarily unavailable

而通过另外一台可以登录的 DigitalOcean 的海外服务器,在那台服务器上是可以通过 ssh 登录上去的。

看来是这台 Linode 服务器的 IP 之前被人建站用过,IP 已经被国内的密码机构墙了。

这就很蛋疼了,总不能每次都通过另一台服务器上进行操作吧,主要是网络也不太好,在上面敲命令也很卡。

我想是否可以通过什么工具或者命令,可以直接在本地 ssh 上那台被墙的服务器,也许操作就没那么卡顿了。

ssh 命令

ssh username@目标机IP -J username@跳板机IP

执行时,首先会要求输入跳板机的登录密码,然后再提示输入目标机的登录密码。

如果想省去输入密码只需要

  1. 将本地的 ~/.ssh/id_rsa.pub 文件内容,复制到跳板机的 ~/.ssh/authorized_keys 文件中。
  2. 然后,将跳板机的 id_rsa.pub,复制到目标机的 authorized_keys 中。这里的理解应该是错误的,因为我换到了另一个电脑,这个电脑的 pub key 在跳板机上,但是不在目标服务器上,依然需要输入密码。所以,应该是当前主机的 pub key 需要在目标主机上。(为了省事,可以把跳板机和本机的 pub key 都复制到目标主机中)

使用这个命令,就可以直接登录到目标服务器了。省去了很多麻烦。

然后这个这个 ssh 命令保存到一个 shell 脚本,就可以一键登录了。

注意:如果不把跳板机的 id_rsa.pub 复制到目标机,则会报错:

kex_exchange_identification: Connection closed by remote host

-J 参数说明

通过 man ssh 可以看到 -J 参数的说明:

-J destination
Connect to the target host by first making a ssh connection to the jump host described by destination 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.  Note that configuration directives supplied on the command-line generally apply to the destination host and not any specified jump hosts.  Use ~/.ssh/config to specify configuration for jump hosts.

~/.ssh/config

虽然我看推荐使用 ssh config 文件来设置 proxy host,但是我感觉不如 shell 脚本保存更简洁直观。

参考

https://mp.weixin.qq.com/s/FS3RpNYl0TbF400gGckekA

微信关注我哦 👍

大象工具微信公众号

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式