Azure

Can't Connect to an Azure VM via RDP or SSH? How to Fix It

A step-by-step checklist to find what blocks RDP (3389) or SSH (22) to an Azure virtual machine, using Azure's own diagnostic tools.

Quick answer: Check four things in order: the VM is running and has a public IP (or use Azure Bastion), the network security groups on both the NIC and the subnet allow port 3389 (RDP) or 22 (SSH) from your IP, no Just-in-time policy is closing the port, and the guest OS firewall and service are running. Network Watcher's IP flow verify shows exactly which rule blocks you. Use Reset password or the Serial console if credentials or the OS are the problem.

You click Connect on an Azure VM, open Remote Desktop or run ssh, and nothing happens until it gives up:

ssh: connect to host 20.204.11.75 port 22: Connection timed out

Or Remote Desktop says it "can't connect to the remote computer". Work through these checks in order.

What the error means

A timeout means traffic isn't reaching the VM's RDP or SSH service. The layers that can block it are: the VM state, the public IP, two network security groups (one on the subnet, one on the network interface), Just-in-time access, and the firewall inside the guest OS. An authentication error ("credentials did not work", "Permission denied") means the network is fine and the account is the problem.

How to fix it, step by step

1. Confirm the VM is running

az vm get-instance-view --resource-group my-rg --name my-vm \
  --query "instanceView.statuses[?starts_with(code,'PowerState')].displayStatus" -o tsv

It should say VM running. A "Stopped (deallocated)" VM also usually loses a dynamic public IP.

2. Check the public IP (or use Bastion)

Without a public IP the VM isn't reachable from the internet. Either associate one with the NIC, or connect through Azure Bastion, which needs no public IP on the VM. If the IP is dynamic, it can change after deallocation, so use a static IP for VMs you connect to directly.

3. Check both NSGs, then let Azure tell you which rule blocks you

Traffic must be allowed by the subnet NSG and the NIC NSG. Add an inbound rule for TCP 3389 (RDP) or 22 (SSH) with your public IP as the source, at a lower priority number than any deny rule.

The fastest way to find the blocking rule is IP flow verify:

az network watcher test-ip-flow --resource-group my-rg --vm my-vm \
  --direction Inbound --protocol TCP \
  --local 10.0.0.4:22 --remote 203.0.113.25:60000

The result shows Allow or Deny and the exact rule name. You can also view Effective security rules on the network interface in the portal.

4. Check Just-in-time (JIT) VM access

If Microsoft Defender for Cloud manages JIT on the VM, it adds deny rules for management ports and opens them only on request for a limited time. Request access from the VM's Connect page before connecting.

5. Check the firewall and service inside the VM

If the NSGs allow traffic but it still fails, the guest OS is blocking it. Use Run command, which doesn't need network access to the VM:

# Linux VM
az vm run-command invoke -g my-rg -n my-vm --command-id RunShellScript \
  --scripts "sudo systemctl status sshd; sudo ss -tlnp | grep :22"

# Windows VM
az vm run-command invoke -g my-rg -n my-vm --command-id RunPowerShellScript \
  --scripts "Get-Service TermService; Get-NetFirewallRule -DisplayGroup 'Remote Desktop' | Select DisplayName,Enabled"

6. Reset credentials or SSH key

For "credentials did not work" or "Permission denied (publickey)", open the VM and choose Help → Reset password. It can reset a password or replace the SSH public key. From the CLI:

az vm user update --resource-group my-rg --name my-vm \
  --username azureuser --ssh-key-value ~/.ssh/id_rsa.pub

7. Use boot diagnostics and the Serial console

If the VM doesn't boot properly (disk full, bad fstab, network config), enable boot diagnostics to see the screenshot and log, and use the Serial console to log in and repair it.

How to prevent it

  • Use Azure Bastion or JIT instead of leaving RDP and SSH open to the internet.
  • Define NSGs and VMs in code with Terraform with Azure so rules are reviewed and repeatable.
  • Use static public IPs or DNS names for VMs you connect to directly.
  • Enable boot diagnostics on every VM so the Serial console is available when you need it.

Azure networking, NSGs, Bastion and VM troubleshooting are covered in our Azure Cloud & Azure DevOps training.

Frequently asked questions

Why can't I RDP to my Azure VM?

The most common causes are a missing NSG rule for port 3389 from your IP, no public IP on the VM, a Just-in-time access policy that closed the port, or the Windows firewall or Remote Desktop service inside the VM.

How do I find which NSG rule is blocking my connection?

Use Network Watcher IP flow verify (or az network watcher test-ip-flow). It tests a specific port and source IP and returns the exact rule that allows or denies the traffic.

How do I reset the password or SSH key of an Azure VM?

In the portal open the VM, then Help → Reset password. It can reset a Windows password, a Linux password or a Linux SSH public key. The VM agent must be running.

Can I connect to an Azure VM without a public IP?

Yes. Azure Bastion gives RDP and SSH through the portal over HTTPS, so the VM doesn't need a public IP or open inbound ports.

Learn Azure & Azure DevOps hands-on with expert trainers

Fixing problems like this one is part of our Azure & Azure DevOps training: live online classes for individuals and on-site corporate training for teams, with real projects and labs.

Training available in Hyderabad, Bangalore, Pune, Mumbai, Delhi, Chennai and online across India.