Compute Engine SSH: connection timed out, refused or publickey denied
When SSH to a Compute Engine VM fails, run gcloud compute ssh --troubleshoot, then check firewall rules, sshd, OS Login roles and the serial console.
What you see
port 22: Connection timed out.port 22: Connection refusedUSERNAME@VM_EXTERNAL_IP: Permission denied (publickey).ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].- Console:
We are unable to connect to the VM on port 22.
What it means
Exit code 255 is generic. It also appears when the VM closes a session, for example after a restart. The message before it decides where to look: a timeout points at the network or firewall, a refusal means nothing is listening on the port, and a publickey error means the connection worked and authentication failed.
How to confirm the cause
- 1Let gcloud check for you
gcloud compute ssh VM_NAME --troubleshoot
Add
--tunnel-through-iapif you connect through Identity-Aware Proxy. - 2See more detail from the client
gcloud compute ssh VM_NAME --ssh-flag="-vvv"
- 3List firewall rules
gcloud compute firewall-rules list
- 4Test the TCP handshake
curl -vso /dev/null --connect-timeout 5 EXTERNAL_IP:22
Get the external IP with
gcloud compute instances describe VM_NAME --format='get(networkInterfaces[0].accessConfigs[0].natIP)'. - 5Check for memory errors in the serial output
gcloud compute instances get-serial-port-output VM_NAME | grep -i -e "out of memory" -e "oom"
- 6Check sshd from the serial console
systemctl status sshd.service
Google recommends reviewing the serial console logs for connection errors.
Causes and fixes
The SSH firewall rule is missing
- HOW TO TELL
- No rule allows
tcp:22. The defaultdefault-allow-sshrule has been deleted. - FIX
- Recreate it.
gcloud compute firewall-rules create default-allow-ssh --allow tcp:22
The VM is still booting or sshd is not running
- HOW TO TELL
- A timeout or refusal soon after creation, or
sshdshows inactive in the serial console. - FIX
- Wait for boot to finish, then check
sshd. If sshd listens on a custom port, a firewall rule must allow that port.
OS Login is on but your account lacks the IAM roles
- HOW TO TELL
Permission denied (publickey)on a VM that uses OS Login.- FIX
- Grant the required OS Login IAM roles. Do not use metadata SSH keys on an OS Login VM.
Key or file problems
- HOW TO TELL
- The key pair in
~/.ssh/google_compute_engineis missing or corrupted, a custom~/.ssh/configoverrides your settings, or$HOME,$HOME/.sshorauthorized_keyshave wrong ownership or permissions. - FIX
- Test without your client config using
ssh -F none USERNAME@EXTERNAL_IP, then fix the key files and permissions.
Boot disk full or VM out of memory
- HOW TO TELL
- The serial output shows out-of-memory messages, or the disk is full.
- FIX
- Free memory or disk, or resize the VM, then retry.
How to stop it coming back
- Use IAP tunnelling or OS Login so SSH does not need a wide-open firewall rule.
- Check serial console access works before you need it.
Keep going
- Runbook: EC2 SSH: Permission denied (publickey) and unprotected key file
- Runbook: Azure Linux VM: SSH connection failed or refused
- Cheat sheet: gcloud
- Cheat sheet: SSH
- Lesson: the cloud topic in Learn
Sources
Google Cloud documentation (docs.cloud.google.com), read 18 Nov 2026.