【问题标题】:using google gcloud to ssh tunnel into linux machine inside network使用 google gcloud 通过 ssh 隧道进入网络内的 linux 机器
【发布时间】:2020-02-08 21:14:50
【问题描述】:

我有一台运行在 OSX 上的 Ubuntu 16.04 VirtualBox 机器(即机器 A),连接到大学校园网络。我想偶尔从我的笔记本电脑 ssh 进入机器以远程协助我的同事,我查看了不同的选项。

似乎其中一个选项是“反向 ssh”(与“端口转发”或“ssh 隧道”有关)。我的笔记本没有固定IP,所以不能直接反向ssh。可能的解决方案是使用代理机器。这个想法是,当我需要帮助我的同事时,他们将输入来自机器 A 的连接指令,这将创建一个正在运行的 GCP 实例,然后我将能够使用此桥接(代理)从外部连接到机器 A ?) GCP 机器。


                                            / Academic intranet
                          +----------+     |  
                          |   GCE    |     |  +----------+
                          | instance |<----|--| Machine A|
                          +----------+     |  +----------+
                                           |  
                                            \ 



                                            / Academic intranet
                          +----------+     |  
+-------------+    ssh    |   GCE    | ssh |  +----------+
| Laptop dynIP|---------->| instance |-----|->| Machine A|
+-------------+           +----------+     |  +----------+
                                           |
                                            \

我们在机器 A 上安装了一个 Google 云帐户和gcloud。据我所知,GCP 已经有一种非常简单的方法可以在 GCP 中设置隧道:

https://cloud.google.com/community/tutorials/ssh-tunnel-on-gce

我试过了,效果很好。这让我猜想最后一步在 GCP 上应该也是可以的:让我能够在正在运行的 GCP 实例上打开一个 SSH 浏览器窗口,这样我就可以从那里 ssh 到机器 A。

有什么想法吗?

已编辑:

这是我在 gce 说明中遵循 ssh 隧道的距离:

在机器 A 上:

gcloud compute instances create --zone us-west1-a tunnel
gcloud compute ssh --zone us-west1-a tunnel -- -N -p 22 -D localhost:2210

在我的笔记本电脑上,我可以打开https://console.cloud.google.com/compute/instances,然后打开浏览器窗口进行 SSH 连接。

从 GCP 实例主机名 tunnel,我想我缺少类似:

ssh-into-machine-A-from-here

这是我遗漏的最后一个命令。或者gcloud 中的 ssh 隧道可能需要额外的标志/参数。

【问题讨论】:

  • 它看起来应该可以工作,但我没有看到任何需要在机器 A 上运行 gcloud。请参阅以下内容...blog.devolutions.net/2017/3/what-is-reverse-ssh-port-forwarding 您最初的目标似乎是通过 ssh 进入 GCP Compute使用本机 ssh 的引擎 (CE) ... 请参阅 cloud.google.com/compute/docs/instances/connecting-advanced
  • 它的工作原理是我可以 ssh 进入机器 A 创建的隧道机器。但是从那里,我不知道如何 ssh 进入机器 A。我想我是缺少最后一个 ssh 命令,或者设置隧道需要其他标志/参数。
  • 看起来这是关键...blog.devolutions.net/2017/3/what-is-reverse-ssh-port-forwarding 如果我没看错的话,你可以从机器 A 使用特殊标志通过 SSH 连接到 GCP 的 CE。这将导致 CE 上的 SSH 开始侦听本地端口。然后,您将登录到 CE 并在 CE 上执行本地 SSH,然后使用第一个命令设置的现有连接。
  • 在您上次更新之后 .... 看起来如果您登录到您的 CE,您应该能够运行 ssh -p 2210 username@localhost 其中用户名是机器 A 上的用户名
  • 谢谢@Kolban 我需要在我的问题中更改上述任何参数还是只需发出两个gcloud 命令,然后在GCP 实例中执行ssh -p 2210 username@localhost

标签: ssh google-cloud-platform remote-desktop


【解决方案1】:

我不能 100% 确定我得到了您的确切问题,但据我了解,创建 VPN 应该是最适合您的解决方案。将 GCE 实例与机器 A 连接的最佳和最安全的方式。

您可以找到here 对同类实现的讨论。

另一个具有相同精神的选项是在机器 A 上使用像 OpenSSH 这样的虚拟专用服务器。Here 有一个关于如何使用像 OpenSSH 这样的虚拟专用服务器以及如何配置它的指南.

【讨论】:

    【解决方案2】:

    0) 使用如下命令在 GCP 上创建实例:

    gcloud compute instances create --zone us-west1-a tunnel
    

    0b) 点击https://console.cloud.google.com/compute/instances 上的“SSH”链接以打开浏览器窗口。

    0c) 在浏览器窗口中,编辑 sshd_config 文件以启用GatewayPorts yes

    0d) 设置gcloud CLI并首次连接如下图:

    gcloud compute ssh --zone us-west1-a tunnel
    

    这将在$HOME/.ssh/google_compute_engine 中创建 ssh 密钥。与它断开连接。现在已创建密钥,请按照以下步骤操作。

    1) 建立从 GCE 到机器 A 的转发:在机器 A 上运行以下命令:

    ssh -i ~/.ssh/google_compute_engine -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -f -N -R 2022:*:22 gce_user@gce_address
    

    2) 现在,要从您的笔记本电脑连接到机器 A,您可以使用带有 GCP 实例的浏览器窗口并执行以下操作:

    ssh -p 2022 A_machine_user@localhost
    

    这应该会询问 A_machine_user 的密码并将您连接到机器 A。

    【讨论】:

    • 我会试试@konstantin-svintsov。如何从隧道 GCE 机器的运行 ssh 会话中获取 gce_address?以及如何在 GCE sshd 配置上启用 GatewayPorts。这是gcloud compute instances create 命令或gcloud compute ssh 命令上的标志吗?提前致谢。
    • * sshd_config 位于 /etc/ssh/sshd_config * gce_user - 我设法仅为此创建一个新用户,并在 VM 实例详细信息中添加公共 ssh 密钥和 ssh 密钥。 * 我必须使用防火墙设置公开新端口:docs.bitnami.com/google/faq/administration/use-firewall
    猜你喜欢
    • 1970-01-01
    • 2018-11-18
    • 2020-12-10
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2011-08-12
    • 2020-02-09
    • 2014-12-12
    相关资源
    最近更新 更多