Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).
like image 401
ROBOT AI Avatar asked Sep 17 '17 00:09

ROBOT AI


People also ask

How do I SSH from one instance to another in GCP?

To connect to an instance without an external IP address, use the gcloud compute ssh command with the --internal-ip flag. In the Google Cloud console, go to the VM Instances page and find the internal IP address for the instance that you want to connect to. Connect to the instance.

How to SSH to Google Compute Engine?

Log in to the Google Cloud Console and select your project. Navigate to the “Compute Engine -> VM Instances” page and select the server you wish to connect to. Click the “Edit” link in the top control bar. On the resulting page, copy and paste your public SSH key into the “SSH Keys” field.

How do I use Google SSH?

In the console, go to the VM instances page. In the list of VMs, click the arrow_drop_down drop-down next to the SSH button of the VM that you want to connect to. Click Open in browser window using provided private SSH key. The SSH-in-browser window opens.


3 Answers

GCE instances have gcloud set up by default. then, the easiest way to go is.

gcloud compute ssh [INSTANCE_NAME] [--ZONE [INSTANCE_ZONE]]

the zone flag might be needed because gcloud init haven't been run before in that instance.

like image 102
Nilo_DS Avatar answered Nov 16 '22 03:11

Nilo_DS


See managing instance access with SSH key pairs. Basically, if you need to ssh from robot-a to robot-b, you need to generate a key pair on robot-a, add robot-a's public key to robot-b (by login to robot-b, and edit the .ssh/authorized_keys file), then robot-b recognizes robot-a.

Then access by name:

robot-a$ ssh robot-b

or by internal IP:

robot-a$ ssh 10.222.0.22

A more general help: how to set up ssh so that you are not asked for a password

like image 23
Dagang Avatar answered Nov 16 '22 02:11

Dagang


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>
like image 42
gogasca Avatar answered Nov 16 '22 03:11

gogasca