How to ssh between two instances created on Google Compute Engine?

Viewed 7230

I have created two instances on Google Compute Engine:

Instance A
hostname: robot-a
ip addr: 10.111.0.11

Instance B
hostname: robot-b
ip addr: 10.222.0.22

I can log in to both instances from my local machine. But how can I log in to the other instance from one of them?


I tried the following, but failed:

robot-a$ ssh robot-b
The authenticity of host 'robot-b (10.111.0.11)' can't be established.
ECDSA key fingerprint is 3a:1a:f1:23:6a:83:ab:db:d8:a1:e8:7d:f5:65:c8:c5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'robot-b' (ECDSA) to the list of known hosts.
Permission denied (publickey).
5 Answers

I launched 5 new instances using Template groups, I needed to share some commands via SSH, and manually I wasnt able to connect between instances:

gcloud compute ssh rapids-instances-dj6p --zone us-central1-b

WARNING: The public SSH key file for gcloud does not exist.
WARNING: The private SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: SSH keygen will be executed to generate a key.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/username/.ssh/google_compute_engine.
Your public key has been saved in /home/username/.ssh/google_compute_engine.pub.
The key fingerprint is:
SHA256:SLaTY/4PMgpzWcM/oJDnhNJq02Uqnd06ZT6ChOAnCUU username@rapids-instances-pr0c
The key's randomart image is:
+---[RSA 2048]----+
| .E              |
|  .              |
| .    o          |
|o. o + +         |
|= B oo% S        |
| BoB**.O         |
|.+*=*.B.+        |
|. o= +.* o       |
|    ..o o..      |
+----[SHA256]-----+
Updating project ssh metadata...⠹Updated [https://www.googleapis.com/compute/v1/projects/my-project].                                                                      
Updating project ssh metadata...done.                                                                                                                                         
Waiting for SSH key to propagate.

ssh: connect to host 104.155.167.207 port 22: Connection timed out
ERROR: (gcloud.compute.ssh) Could not SSH into the instance.  It is possible that your SSH key has not propagated to the instance yet. Try running this command again.  If you still cannot connect, verify that the firewall and instance are set to accept ssh traffic.

All these instances have Public address, gcloud ssh was trying to connect via external network, I created the following function:

function gssh() {
  gcloud compute ssh $@ --internal-ip
}

And then use it like this:

gssh <hostname>

It's quiet simple if you have 2 instance in google cloud platform, automatically you have the guest environment installed (gcloud command lien), with it you can ssh through all you ssh inside your project:

Just run the following command line for inside your instance A to reach the Instance B

[user@Instance-A]$ gcloud compute ssh Instance-B

That it, if it's not working let me know, and verify if your firewall rule let internal traffic.

You need to add the public key of the machine (where you are trying to ssh from) to the the ~/.ssh/authorized_keys of the remote machine(one you are trying to connect to).

Another way is - in the GCP console - select and edit the destination machine and add the SSH key of the client machine.

Related