Google CloudJunior

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 refused
  • USERNAME@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

  1. 1
    Let gcloud check for you
    gcloud compute ssh VM_NAME --troubleshoot

    Add --tunnel-through-iap if you connect through Identity-Aware Proxy.

  2. 2
    See more detail from the client
    gcloud compute ssh VM_NAME --ssh-flag="-vvv"
  3. 3
    List firewall rules
    gcloud compute firewall-rules list
  4. 4
    Test 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)'.

  5. 5
    Check for memory errors in the serial output
    gcloud compute instances get-serial-port-output VM_NAME | grep -i -e "out of memory" -e "oom"
  6. 6
    Check 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 default default-allow-ssh rule 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 sshd shows 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_engine is missing or corrupted, a custom ~/.ssh/config overrides your settings, or $HOME, $HOME/.ssh or authorized_keys have 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

Sources

Google Cloud documentation (docs.cloud.google.com), read 18 Nov 2026.

Search

Search cheat sheets and projects