'Can't connect to GCP VM Permission denied (publickey) error

I'm creating a new VM instance. I've clean all the meta data. Then I'm running the following command in the cloud shell:

gcloud beta compute ssh --zone "europe-west2-c" "vmname"  --project "myprojectname"

then I've been asking to enter a passphrase (which I don't know). I press enter until I get the following error Permission denied (publickey) error

I've delete and recreated my instance multiple time but I always have the same error. What should I do?



Solution 1:[1]

The possible causes for a Permission denied (publickey) error are:

  • Your key expired and Compute Engine deleted your ~/.ssh/authorized_keys file.
  • You used an SSH key stored in metadata to connect to a VM that has OS Login enabled.
  • You used an SSH key stored in an OS Login profile to connect to a VM that doesn't have OS Login enabled.
  • You connected using a third-party tool and your SSH command is misconfigured.
  • The sshd daemon isn't running or isn't configured properly.

You can find more information on how to troubleshoot SSH key errors in this link

Solution 2:[2]

I have the same issue sometimes . Cause and solution according to GCP troubleshooting link is:

Your key expired and Compute Engine deleted your ~/.ssh/authorized_keys file. If you manually added SSH keys to your VM and then connected to your VM using the Google Cloud Console, Compute Engine created a new key pair for your connection. After the new key pair expired, Compute Engine deleted your ~/.ssh/authorized_keys file in the VM, which included your manually added SSH key.

To resolve this issue, try one of the following:

Connect to your VM using the Google Cloud Console or the gcloud command-line tool. Re-add your SSH key to metadata. For more information, see Add SSH keys to VMs that use metadata-based SSH keys.

I use terraform so in this case I instructed the workflow to destroy the VM and rebuild it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nahuel
Solution 2 Iftimie Tudor