如何使用PyCharm通过IAP隧道远程连接到虚拟机

5
我有一个没有分配外部IP地址的Google VM实例。我打算通过在本地机器上安装的PyCharm(运行macOS)建立SSH连接。
可以通过终端使用gcloudIAP隧道来完成此操作。
gcloud compute ssh <instance_name> --tunnel-through-iap

为该实例添加到~./ssh/config的条目如下:

Host compute.<instance_id>
  HostName compute.<instance_id>
  IdentityFile /Users/<user_name>/.ssh/google_compute_engine
  CheckHostIP no
  HostKeyAlias compute.<instance_id>
  IdentitiesOnly yes
  StrictHostKeyChecking yes
  UserKnownHostsFile /Users/<user_name>/.ssh/google_compute_known_hosts
  ProxyCommand /Users/<user_name>/miniconda3/bin/python3 -S /Users/<user_name>/google-cloud-sdk/lib/gcloud.py beta compute start-iap-tunnel <instance_name> %p --listen-on-stdin --project=<project_name> --zone=us-central1-a --verbosity=warning
  ProxyUseFdpass no
  User <user_name>

使用 VS Code 的Remote-SSH插件, 这个设置可以直接用于建立SSH连接 (示例).

然而,我在 PyCharm 中设置连接时遇到了困难。 SSH配置选项卡需要:

 - Host: compute.<instance_id>
 - User name: compute.<instance_id>
 - Port: 22
 - Authentication type: key pair
 - Private key file: path to ~/.ssh/google_compute_engine

并且会因为主机格式不正确而抛出异常。如果我尝试将VM实例的内部IP地址作为主机,连接会超时。
是否有类似于VS Code中的Remote-SSH插件的PyCharm插件可以与IAP隧道正常工作?或者还有其他方法可以设置,而无需将外部IP公开或分配给VM实例?

我找到了这个博客,如果你觉得有用的话可以看看 https://blog.doit-intl.com/remote-development-with-pycharm-and-google-cloud-1a8bdab3433f - DamPlz
谢谢,@DamPlz,但是通过IAP隧道的SSH是不同的,因为没有外部IP暴露。 - Mahmoud
2个回答

0

我知道已经有一段时间了,但我刚刚在做同样的事情。我在~./ssh/config中使用了相同的配置条目,但是PyCharm正在进行一些检查,以确保顶级Host值是有效的(即使它没有被使用)。我用能够通过他们的验证检查的内容替换了它,但我知道我实际上永远不会使用它(以避免潜在的冲突)。

例如,您可以更新为以下内容:

Host mahmoud.local
  HostName compute.<instance_id>
  IdentityFile /Users/<user_name>/.ssh/google_compute_engine
  CheckHostIP no
  HostKeyAlias compute.<instance_id>
  IdentitiesOnly yes
  StrictHostKeyChecking yes
  UserKnownHostsFile /Users/<user_name>/.ssh/google_compute_known_hosts
  ProxyCommand /Users/<user_name>/miniconda3/bin/python3 -S /Users/<user_name>/google-cloud-sdk/lib/gcloud.py beta compute start-iap-tunnel <instance_name> %p --listen-on-stdin --project=<project_name> --zone=us-central1-a --verbosity=warning
  ProxyUseFdpass no
  User <user_name>

然后当您在PyCharm中配置SSH连接时,您将希望使用主机= mahmoud.local


0

是的,我也用 ~/.ssh/config 主机成功了。起初我遇到了指纹错误,但我关闭了 StrictHostkeyChecking 就解决了:

Host lukas-notebook-gpu
    HostName compute.1234
    IdentityFile /Users/lbatteau/.ssh/google_compute_engine
    CheckHostIP no
    HostKeyAlias compute.1234
    IdentitiesOnly yes
    StrictHostKeyChecking no
    HashKnownHosts no
    UserKnownHostsFile /Users/lbatteau/.ssh/google_compute_known_hosts
    ProxyCommand /Users/lbatteau/.config/gcloud/virtenv/bin/python3 /Users/lbatteau/google-cloud-sdk/lib/gcloud.py compute start-iap-tunnel lukas-notebook-gpu %p --listen-on-stdin --project=myproject --zone=europe-west4-a --verbosity=warning
    ProxyUseFdpass no

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接