How to connect VSCode to GCP instances
Solution 1
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:
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
Then, replacing the values with the above:
Host compute_engine
HostName [[IP]]
IdentityFile ~/.ssh/google_compute_engine
User [[USER]]
Port 22
Solution 2
Simply running gcloud compute config-ssh
, and you would get example-instance.us-central1-a.MY-PROJECT
in your vs code ssh targets. Details at config-ssh
jss367
Updated on July 07, 2022Comments
-
jss367 almost 2 years
I am trying to connect VSCode to my GCP instances but am unable to. 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. When I created the config in VSCode I did this: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. I haven't even told VSCode that it's a GCP instance. 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. I found a GCP-Service.json file with the following keys:{ "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.
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. What am I supposed to do to connect VSCode to my GCP instance?
-
jss367 over 3 yearsI tried this approach although I must have misunderstood a step because I wasn't able to get it to work. I edited my question with what I tried.
-
DazWilkin over 3 yearsYou need to replace
IP
andUSER
with the actual values for your account and VM using the commands I showed -
rafee almost 3 yearsThis is IMHO the better answer
-
Tariq over 2 yearsSorry complete noob here - can you explain step by step
-
M.Innat about 2 years@Tariq read this learn.canceridc.dev/cookbook/virtual-machines/…
-
StanleyZheng almost 2 yearsthis is awesome, you can check and run
gcloud compute config-ssh
--project <MY PROJECT> and simplycat ~/.ssh/config
to see all your configs listed, it might be relevant to actually cat this into another file and define it and then inherit than overwhelm your config file -
LonelySoul almost 2 yearsWonderful answer. Someone should made a video on it