Unable to ssh to GCE: "Permission denied (publickey)"

Solution 1:

After I was able to ssh via Google web console, I did the following steps to resolve this:

  1. Generate ssh key using

    ssh-keygen

  2. Copy the key.pub file contents

  3. Append the contents to ~/.ssh/authorized_keys file

    sudo nano ~/.ssh/authorized_keys

Solution 2:

I faced the same situation because of the user. On google web shh my user name was showing something first part of my email. So, I was trying ssh like this

ssh <first_part_of_gmail>@google_vm_external_ip

Later, I discover that, google creates a user based on the ssh key that you put on the google vm setting. So, first check the user at the end of the public key, and try following

ssh <user_name_at_the_end_of_public_key>@google_vm_external_ip

Solution 3:

When your instance is first created, it will not have any SSH keys in it by default, so you have to transfer them there, e.g., by using gcloud to connect to it the first time as described in this SO answer or by manually creating SSH keys and manually adding them to your instance as described in another SO answer.


Solution 4:

Make sure you don't have OS Login enabled. Docs read:

If you manage your SSH keys by using OS Login on instances, metadata-based SSH key configurations on those instances are disabled

and

Caution: Enabling OS Login on instances disables metadata-based SSH key configurations on those instances. Disabling OS Login restores SSH keys that you have configured in project or instance metadata.

To verify, go to project-level metadata (Compute Engine -> Metadata) and ensure that you have either no enable-oslogin key or that it is set to FALSE


Solution 5:

I had the same issue, and used gcloud command to login for the first time and added to /etc/ssh/sshd_config.

PubkeyAcceptedKeyTypes  +ssh-dss

After I restarted the service:

systemctl restart sshd