简体   繁体   English

如何将 VSCode 连接到 GCP 实例

[英]How to connect VSCode to GCP instances

I am trying to connect VSCode to my GCP instances but am unable to.我正在尝试将 VSCode 连接到我的 GCP 实例,但无法连接。 From a terminal, I can ssh into the machines with gcloud compute ssh my_machine_name but I'm not sure how to translate that into what VSCode Remote-SSH is looking for.从终端,我可以 ssh 进入机器gcloud compute ssh my_machine_name但我不确定如何将其转换为 VSCode Remote-SSH 正在寻找的内容。 When I created the config in VSCode I did this:当我在 VSCode 中创建配置时,我这样做了:

Host my_machine_name
    HostName my_machine_name
    User me@my_company.com

But the HostName is wrong because it's just the name of the machine and not the full HostName or IP address.但是 HostName 是错误的,因为它只是机器的名称,而不是完整的 HostName 或 IP 地址。 I haven't even told VSCode that it's a GCP instance.我什至没有告诉 VSCode 这是一个 GCP 实例。 How do I find the HostName?如何找到主机名? I imagine there's a connection between my_machine_name and the true HostName somewhere in my configs, but I can't find it.我想我的配置中某处的my_machine_name和真实主机名之间存在联系,但我找不到它。 I found a GCP-Service.json file with the following keys:我找到了一个包含以下键的 GCP-Service.json 文件:

{
  "type": 
  "project_id": 
  "private_key_id": 
  "private_key": 
  "client_email": 
  "client_id": 
  "auth_uri": 
  "token_uri": 
  "auth_provider_x509_cert_url": 
  "client_x509_cert_url": 
}

but I don't see anything that looks like a HostName or IP address.但我没有看到任何看起来像主机名或 IP 地址的东西。

Just to see, I tried to connect and got the following error:只是为了看看,我尝试连接并收到以下错误:

Could not establish connection to "my_machine_name": Permission denied (publickey).

(Note sure if this is relevant but sometimes when I first try to connect I get the following, but after I click "Retry" it goes back to the publickey message again): (请注意这是否相关,但有时当我第一次尝试连接时,我会得到以下信息,但在我单击“重试”后,它会再次返回到公钥消息):

"Could not establish connection to "my_machine_name": Remote host key has changed, port forwarding is disabled."

So I tried to add a private key like so:所以我试着像这样添加一个私钥:

Host my_machine
    HostName my_machine
    User user@my_company.com
    IdentityFile ~/.ssh/id_rsa

I also tried the private key from my GCP-Service.json file as well but got the same result.我也尝试了我的 GCP-Service.json 文件中的私钥,但得到了相同的结果。 What am I supposed to do to connect VSCode to my GCP instance?我应该怎么做才能将 VSCode 连接到我的 GCP 实例?

Simply running gcloud compute config-ssh , and you would get example-instance.us-central1-a.MY-PROJECT in your vs code ssh targets.只需运行gcloud compute config-ssh ,您就会在 vs code ssh 目标中获得example-instance.us-central1-a.MY-PROJECT Details at config-ssh config-ssh 上的详细信息

Per my comment, this is straightforward and worked for me.根据我的评论,这很简单,对我有用。

Assuming:假设:

PROJECT=[[YOUR-PROJECT]]
INSTANCE=[[YOUR-INSTANCE]]
ZONE=[[YOUR-ZONE]]

This is slightly hacky but either:这有点hacky,但要么:

USER=$(\
  gcloud compute ssh ${INSTANCE} \
  --zone=${ZONE} \
  --project=${PROJECT} \
  --command "whoami") && echo ${USER}

Or:或者:

gcloud auth list --format="value(account)"
[[USER]]@[[DOMAIN]]

And:和:

IP=$(\
  gcloud compute instances describe ${INSTANCE} \
  --zone=${ZONE} \
  --project=${PROJECT} \
  --format='value(networkInterfaces[0].accessConfigs[0].natIP)') && echo ${IP}

NOTE the above assumes a single network interface and a public IP注意以上假设一个网络接口和一个公共 IP

Then, replacing the values with the above:然后,用上面的值替换这些值:

Host compute_engine
    HostName [[IP]]
    IdentityFile ~/.ssh/google_compute_engine
    User [[USER]]
    Port 22

It is also possible to use gcloud beta compute start-iap-tunnel INSTANCE_NAME 22 --listen-on-stdin to tunnel ssh access.也可以使用gcloud beta compute start-iap-tunnel INSTANCE_NAME 22 --listen-on-stdin来隧道 ssh 访问。 (even without public ip) (即使没有公网IP)

gcloud iap tunnel docs gcloud iap 隧道文档

Your ssh config file:您的 ssh 配置文件:

Host YOUR_SERVER_NAME
    IdentityFile ~/.ssh/google_compute_engine
    User YOUR_USERNAME
    HostName YOUR_INSTANCE_NAME
    ProxyCommand gcloud beta compute start-iap-tunnel %h 22 --listen-on-stdin

Note: If you are running vscode client on windows, you may need to have something like this注意:如果你在 windows 上运行 vscode 客户端,你可能需要有这样的东西

ProxyCommand powershell -Command "gcloud beta compute start-iap-tunnel %h 22 --listen-on-stdin"

You can always test your ssh connection with ssh YOUR_SERVER_NAME -vvv , (and see if ProxyCommand works or not).您始终可以使用ssh YOUR_SERVER_NAME -vvv测试您的 ssh 连接(并查看 ProxyCommand 是否有效)。


I have this in https://github.com/martinwang2002/gcp-vscode-server with start on ssh connection and stop if inactive feature.我在https://github.com/martinwang2002/gcp-vscode-server中有这个,在 ssh 连接上启动,如果不活动则停止功能。

If you already init gcloud successfully then you just need to type如果你已经成功初始化 gcloud 那么你只需要输入

gcloud compute config-ssh

Now you can access with ssh HOSTNAME & it also visiable on you remote ssh vs code plugin.现在您可以使用ssh HOSTNAME进行访问,它在您的remote ssh vs 代码插件上也可见。 From your vs code you can assess with ctrl+shift+p & connect to host chose your host where you want to connect从您的 vs 代码中,您可以使用ctrl+shift+p进行评估并connect to host ,选择您要连接的主机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将气流 [docker] 连接到 GCP? - How to connect airflow[docker] to GCP? 连接 VSCode 和 GCP - Connecting VSCode and GCP 如何在 GCP 中列出项目的最多计算实例信息? - How to list the most compute instances' info for a project in GCP? 如何远程连接到 GCP ML Engine/AWS Sagemaker 托管笔记本? - How to remotely connect to GCP ML Engine/AWS Sagemaker managed notebooks? 如何在 2 个 ec2 实例上连接 2 个不同的 docker 容器? - How can I connect 2 different docker containers on 2 ec2 instances? GCP - 什么是 mysql-access 实例? - GCP - what are mysql-access instances? 如何在 GCP 中创建 ClusterPodMonitoring? - How to create ClusterPodMonitoring in GCP? GCP 为具有公共 ip 的实例创建实例模板 - GCP create instance-template for instances with public ip 使用 Rest api 创建 N 个 GCP 计算实例 - Create N number of GCP compute instances using Rest api 使用 SQL alchemy 和 GCP 服务帐户连接到 GCP Cloud SQL Auth Proxy - Connect to a GCP Cloud SQL Auth Proxy using SQL alchemy and a GCP service account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM