How to Fix VM Connection Timeout in Azure Quickly
A VM connection timeout in Azure usually happens because the
Network Security Group (NSG) or firewall blocks the port, or the VM is not running. To fix it, ensure the VM is running, the NSG allows inbound traffic on the required port (like RDP 3389 for Windows or SSH 22 for Linux), and the VM's firewall permits the connection.Why This Happens
A connection timeout occurs when your computer tries to reach the Azure VM but cannot get a response. This usually happens because the VM is stopped, the network rules block the connection, or the VM's internal firewall denies access.
For example, if the Network Security Group (NSG) does not allow inbound traffic on port 3389 for Windows RDP or port 22 for Linux SSH, the connection will time out.
bash
az vm stop --name MyVM --resource-group MyResourceGroup
# Trying to connect while VM is stopped
ssh azureuser@myvm-ip-addressOutput
ssh: connect to host myvm-ip-address port 22: Connection timed out
The Fix
First, start the VM if it is stopped. Then, check the NSG rules to allow inbound traffic on the correct port. Finally, verify the VM's internal firewall allows the connection.
This example shows how to start the VM and add an NSG rule to allow SSH (port 22):
bash
az vm start --name MyVM --resource-group MyResourceGroup az network nsg rule create \ --resource-group MyResourceGroup \ --nsg-name MyNSG \ --name AllowSSH \ --protocol Tcp \ --direction Inbound \ --priority 1000 \ --source-address-prefixes '*' \ --source-port-ranges '*' \ --destination-address-prefixes '*' \ --destination-port-ranges 22 \ --access Allow
Output
Starting VM 'MyVM'...
{
"name": "AllowSSH",
"protocol": "Tcp",
"direction": "Inbound",
"priority": 1000,
"access": "Allow",
"destinationPortRange": "22"
}
Prevention
To avoid connection timeouts in the future, always:
- Keep your VM running when you need access.
- Regularly review NSG rules to ensure required ports are open.
- Configure the VM's internal firewall to allow inbound connections on needed ports.
- Use Azure Network Watcher to monitor connectivity.
Related Errors
Other common connection issues include:
- Authentication failures: Wrong username or password causes login errors, not timeouts.
- DNS resolution errors: The VM's IP or DNS name is incorrect or changed.
- Public IP missing: VM has no public IP, so external connections fail.
Key Takeaways
Ensure the VM is running before trying to connect.
Open the correct port in the Network Security Group for inbound traffic.
Check the VM's internal firewall allows the connection.
Use Azure CLI commands to start the VM and update NSG rules.
Regularly monitor network settings to prevent future timeouts.