I am unable to ssh into a VM on GCP Compute Engine
However, when I run the command with the --troubleshoot flag, it seems like everything is okay.
When I connect through the console, I get an error message saying "You cannot connect to the VM instance because of an unexpected error".
Also, other people from my organization are able to connect.
I am unable to figure out what the error is. Any help would be appreciated.
From the error message that you got "Permission denied (publickey)" you can check this documentation for further troubleshooting.
Further more you can investigate also the Identity-Aware Proxy (IAP). If you use Identity-Aware Proxy (IAP) for TCP forwarding, update your custom firewall rule to accept traffic from IAP, then check your IAM permissions.
Update your custom firewall rule to allow traffic from 35.235.240.0/20, the IP address range that IAP uses for TCP forwarding. For more information, see Create a firewall rule.
Grant permissions to use IAP TCP forwarding, if you haven't already done so.
For the error message "You cannot connect to the VM instance because of an unexpected error".
The VM is booting up and sshd is not running yet. You can't connect to a VM before it is running.
To resolve this issue, wait until the VM has finished booting and try to connect again.
The firewall rule allowing SSH is missing or misconfigured. By default, Compute Engine VMs allow SSH access on port 22. If the default-allow-ssh rule is missing or misconfigured, you won't be able to connect to VMs.
To resolve this issue, Check your firewall rules and re-add or reconfigure default-allow-ssh
.
sshd is running on a custom port. If you configured sshd to run on a port other than port 22, you won't be able to connect to your VM.
To resolve this issue, create a custom firewall rule allowing tcp traffic on the port that your sshd is running on using the following command:
gcloud compute firewall-rules create FIREWALL_NAME \
--allow tcp:PORT_NUMBER
For further troubleshooting on SSH you see this documentation on Common SSH errors.