Permission denied (publickey). (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255]. exited with return code [255]

Solution 1:

The permission denied (publickey) indicates your authentication is not correct. Be sure you are using the proper public/private key pair.

See also:

https://cloud.google.com/sdk/gcloud/reference/compute/ssh#--ssh-key-file

Solution 2:

There could be various reasons because of which the ssh is not connecting:

  1. Firewall allow rule not added for ssh on the instance.
  2. The ssh server is off or malfunctioned on the system after restart.
  3. The keys are not added on .ssh folder in the user’s directory.
  4. If OS login is enabled may cause ssh to malfunction.

Try logging in as a different user with the gcloud tool by specifying another-username with the SSH request. The gcloud tool updates the project's metadata to add the new user and allow SSH access.

gcloud compute ssh another-username@$PROB_INSTANCE

I recommend you to review with the SSH troubleshooting steps as described in the documentation

Additionally, you can take a look at the following guide that explains how to control access to Linux instances by manually creating SSH keys and editing public SSH key metadata as alternative.

I hope this information would be useful to you

Solution 3:

There may be several reasons why you can't connect. Firewall shouldn't be one of them since you're getting connected but authentication fails.

If you have OS login enabled you control access to your VM's by granting appropriate roles in the IAM. Most convinient & centralised way to grant SSH acces to your OS enabled VM's would be to create a dedicated service account and grant it SSH access.

It's good to try to update your SSH keys: gcloud compute os-login ssh-keys update

If you have OS login disabled (default setting, unless your organisation forces it enabled) then you can try update your SSH keys with gcloud compute config-ssh.

If you're still unable to connect you can have a look at the general SSH troubleshhoting documentation.

Still can't log in ?

Find out what's going on with your VM, check serial console output - this may give you a hint. Your OS may not boot up properly (full disk, corrupted files, many other reasons). Have a look at one of my answers here how to troubleshoot this.

If you see that your system disk has ran out of space you can make it bigger with gcloud compute disks resize example-disk-1 --size=11GB

Another way to get into the VM is by manually addind a user/password (with no ssk-key) and logging in "the old way" - see my other answer how to add a user using startup script.

There may be some other reasons like routes on your VM (which may not sound very obvious) - I've found such an example here.