driftavalii
driftavalii

Reputation: 1339

Connect to particular GCP account

I have been using the GCP console to connect to a cloud instance and want to switch to using SSH through powershell as that seems to maintain a longer persistence. Transferring my public key through cloud shell into authorized_key file seems to be temporary since once cloud shell disconnects, the file doesn't persist. I've tried using os-login but that generates a completely different user from what I've been using through cloud shell (Cloud shell creates a user: myname while gcloud creates a user: myname_domain_com. Is there a way to continue using the same profile created by cloud shell when logging in through gcloud. I am using the same email and account in both the console and gcloud [email protected]. The alternative is to start all over from gcloud and that would be a pain.

Upvotes: 1

Views: 170

Answers (1)

Babuji Abraham
Babuji Abraham

Reputation: 67

If you want to SSH to different instances of a google cloud project (from a mac or Linux), do the following:

Step 1. Install SSH keys without password

Use the following command to generate the keys on your mac

ssh-keygen -t rsa -f ~/.ssh/ -C For example private-key-name can be bpa-ssh-key. It will create two files with the following names in the ~/.ssh directory

bpa-ssh-key bpa-ssh-key.pub Step 2. Update the public key on your GCP project

Goto Google Cloud Console, choose your project, then

VMInstances->Metadata->SSH Keys->Edit->Add Item

Cut and paste the contents of the bpa-ssh-key.pub (from your mac) here and then save

Reset the VM Instance if it is running

Step 3. Edit config file under ~/.ssh on your mac Edit the ~/.ssh/config to add the following lines if not present already

Host * PubKeyAuthentication yes IdentityFile ~/.ssh/bpa-ssh-key Step 4. SSHing to GCP Instance

ssh username@gcloud-externalip It should create a SSH shell without asking for the password (since you have created the RSA/SSH keys without a password) on the gcloud instance.

Since Metadata is common across all instances under the same project, you can seam-lessly SSH into any of the instances by choosing the respective External IP of the gcloud instance.

Upvotes: 1

Related Questions