<p>I'd like to solve the following problem using command line:</p>

<p>I'm trying to run the following PoC script from a GCE VM in project-a.</p>

<pre><code>gcloud config set project project-b
gcloud compute instances create gce-vm-b --zone=us-west1-a
gcloud compute ssh --zone=us-west1-a gce-vm-b -- hostname
</code></pre>

<p>The VM is created successfully:</p>

<pre><code>NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS 
gce-vm-b us-west1-a n1-standard-16 10.12.34.56 12.34.56.78 RUNNING 
</code></pre>

<p>But get the following error when trying to SSH:</p>

<pre><code>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. 
Your identification has been saved in /root/.ssh/google_compute_engine. 
Your public key has been saved in /root/.ssh/google_compute_engine.pub. 
The key fingerprint is: 
...
Updating project ssh metadata... 
.....................Updated [https://www.googleapis.com/compute/v1/projects/project-b]. 
&gt;.done. 
&gt;Waiting for SSH key to propagate. 
&gt;ssh: connect to host 12.34.56.78 port 22: Connection timed out 
&gt;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. 
</code></pre>

<p>Running <code>gcloud compute config-ssh</code> hasn't changed anything in the error message. It's still <code>ssh: connect to host 12.34.56.78 port 22: Connection timed out</code></p>

<p>I've tried adding a firewall rule to the project:</p>

<pre><code>gcloud compute firewall-rules create default-allow-ssh --allow tcp:22 
</code></pre>

<p>.</p>

<pre><code>Creating firewall... 
...........Created [https://www.googleapis.com/compute/v1/projects/project-b/global/firewalls/default-allow-ssh]. 
done. 
NAME NETWORK DIRECTION PRIORITY ALLOW DENY 
default-allow-ssh default INGRESS 1000 tcp:22
</code></pre>

<p>The error is now <code>Permission denied (publickey)</code>.</p>

<pre><code>gcloud compute ssh --zone=us-west1-a gce-vm-b -- hostname 
</code></pre>

<p>.</p>

<pre><code>Pseudo-terminal will not be allocated because stdin is not a terminal. 
Warning: Permanently added 'compute.4123124124324242' (ECDSA) to the list of known hosts. 
Permission denied (publickey). 
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].
</code></pre>

<p>P.S. The project-a "VM" is a container run by Prow cluster (which is run by G<strong>K</strong>E).</p>

Ark-kun
<p>"Permission denied (publickey)" means it is unable to validate the public key for the username. </p>

<p>You haven't specified the user in your command, so the user from the environment is selected and it may not be allowed into the instance gce-vm-b. Specify a valid user for the instance in your command according to the <a href="https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys#edit-ssh-metadata" rel="nofollow noreferrer">public SSH key metadata</a>.</p>
