AzureJunior

Azure Linux VM: SSH connection failed or refused

Work through the NSG rule for port 22, the SSH configuration reset, user credentials, resource health and the serial console, with az CLI commands.

What you see

  • SSH connection failed
  • SSH connection refused
  • Permission denied (publickey) when Microsoft Entra ID manages SSH logins

What it means

Microsoft's quick path is a fixed order: reset the SSH configuration, reset the user's credentials, confirm the network security group allows SSH, check the VM's resource health, restart, then redeploy. Retry after each step.

How to confirm the cause

  1. 1
    Check whether sshd is running (from the serial console or Run Command)
    sudo systemctl status sshd.service

    Read the Active: line. It also shows the port and addresses it listens on.

  2. 2
    Or check the listening socket
    sudo ss -ltpn | grep sshd

    Microsoft recommends ss because netstat is deprecated and not always installed.

  3. 3
    Check which port sshd uses
    grep -i port /etc/ssh/sshd_config

    No output, or only commented lines, means the default of port 22 on all addresses.

Causes and fixes

  • SSH configuration is broken

    HOW TO TELL
    sshd is not running, or listens somewhere unexpected.
    FIX
    Reset the SSH configuration to defaults. This does not change user names, passwords or keys.
    az vm user reset-ssh --resource-group myResourceGroup --name myVM
  • User credentials need resetting

    HOW TO TELL
    sshd is fine but the login fails.
    FIX
    Reset the password or the public key for the user.
    az vm user update --resource-group myResourceGroup --name myVM \
      --username myUsername --ssh-key-value "$(cat ~/.ssh/id_rsa.pub)"
  • NSG does not allow port 22, or a route blocks traffic

    HOW TO TELL
    Connections time out.
    FIX
    Make sure an inbound Allow rule exists for the SSH port (default TCP 22) with a suitable priority. Use Network Watcher's IP flow verify and Next hop to see what blocks traffic. Port redirection or mapping is not possible without an Azure load balancer.
  • Entra ID login without a role

    HOW TO TELL
    Permission denied (publickey) and the VM uses Microsoft Entra login.
    FIX
    Assign the user the Virtual Machine Administrator Login or Virtual Machine User Login role on the resource group that holds the VM and its resources.
  • Underlying compute problem

    HOW TO TELL
    Resets did not help and the VM's resource health looks wrong.
    FIX
    Restart the VM, then redeploy it. Redeploying loses ephemeral disk data and may change dynamic IP addresses.
    az vm restart --resource-group myResourceGroup --name myVM
    az vm redeploy --resource-group myResourceGroup --name myVM

How to stop it coming back

  • Enable boot diagnostics and know how to open the serial console before you need it.
  • Keep SSH NSG rules narrow.

Keep going

Sources

Microsoft Learn (learn.microsoft.com), read 18 Nov 2026. The page's own metadata shows it was last updated 16 Jul 2026.

Search

Search cheat sheets and projects