Resolving 'Connection Refused' on Azure VMs: Throttling & Network Diagnostics
Fix Azure VM 'connection refused' errors. Learn how to diagnose NSG rules, identify SNAT port exhaustion, and resolve Azure network throttling issues.
- Network Security Group (NSG) misconfigurations are the most common cause of connection refused errors.
- High outbound connection rates can lead to SNAT port exhaustion, resulting in Azure VM network throttling and dropped packets.
- Guest OS firewall rules (iptables, firewalld, or Windows Firewall) might be blocking the port even if Azure NSGs allow it.
- Quick Fix: Verify NSG inbound port rules and check VM effective security rules using Azure Network Watcher.
| Method | When to Use | Time | Risk |
|---|---|---|---|
| Azure Network Watcher IP Flow Verify | Initial diagnosis of NSG/routing issues | 5 mins | Low |
| Guest OS Firewall Inspection | NSGs are correct but connection still fails | 10 mins | Low |
| SNAT Port Allocation Scaling | Experiencing outbound connection throttling/drops | 15 mins | Medium |
| Service Status & Port Binding Check | VM is reachable but application is not responding | 5 mins | Low |
Understanding the Error
When you encounter a Connection refused (or ECONNREFUSED in Node.js/Python) error while trying to connect to an Azure Virtual Machine, it explicitly means that the connection attempt reached a host, but the host actively rejected it. This is fundamentally different from a connection timeout, where packets are silently dropped. However, in the context of cloud infrastructure like Azure, 'connection refused' can also bubble up from intermediate load balancers or due to severe network throttling.
Common Error Messages
You might see errors like:
ssh: connect to host 203.0.113.5 port 22: Connection refusedcurl: (7) Failed to connect to 203.0.113.5 port 80: Connection refusedjava.net.ConnectException: Connection refused (Connection refused)
Step 1: Diagnose Network Security Groups (NSGs)
Azure NSGs act as a virtual firewall for your VM. If an NSG rule denies traffic, Azure typically drops the packet (resulting in a timeout), but if the traffic reaches the VM and the OS firewall blocks it, or no service is listening, you get a refusal.
First, check the Effective Security Rules for the VM's Network Interface (NIC). This shows the actual rules applied after merging subnet and NIC-level NSGs.
Use the Azure CLI to verify IP flow:
az network watcher test-ip-flow \
--resource-group MyResourceGroup \
--vm MyVm \
--nic MyVmNic \
--direction Inbound \
--protocol TCP \
--local 10.0.0.4:80 \
--remote 203.0.113.10:45678
Step 2: Investigate Azure VM Throttling and SNAT Port Exhaustion
If you are seeing intermittent Connection refused errors, especially on outbound connections to APIs or database services, you might be hitting Azure VM throttling limits, specifically SNAT (Source Network Address Translation) port exhaustion.
Azure Standard Load Balancers allocate a default number of SNAT ports per VM. If your application opens too many concurrent outbound connections (e.g., failing to use connection pooling), you will exhaust these ports.
Symptoms of SNAT Exhaustion:
- Intermittent connection failures.
- Azure Monitor metrics showing high
SNAT Connection CountorAllocated SNAT Ports.
Resolution:
- Implement Connection Pooling: Ensure your application reuses HTTP or database connections.
- Use NAT Gateway: Assign an Azure NAT Gateway to your subnet. NAT Gateways provide up to 64,512 SNAT ports per public IP address and vastly reduce the risk of exhaustion.
- Scale Out: Distribute the load across multiple VMs or use Virtual Machine Scale Sets (VMSS).
Step 3: Guest OS Configuration
If the Azure network layer allows the traffic, the issue is within the VM.
Verify the service is running and listening on the correct interface: Often, services default to listening on
127.0.0.1(localhost) instead of0.0.0.0(all interfaces).Check the Guest OS Firewall: On Linux,
iptables,ufw, orfirewalldmight be blocking the port. On Windows, check the Advanced Security settings in Windows Defender Firewall.
Frequently Asked Questions
# 1. Check if the service is listening on all interfaces (0.0.0.0)
sudo ss -tulpn | grep LISTEN
# 2. Check local UFW firewall status (Ubuntu/Debian)
sudo ufw status verbose
# 3. Check firewalld status (RHEL/CentOS)
sudo firewall-cmd --list-all
# 4. Azure CLI: Check effective NSG rules for a specific VM NIC
az network nic list-effective-nsg \
--resource-group MyResourceGroup \
--name MyNicName \
--output table
# 5. Test specific port connectivity bypassing ICMP blocks
curl -v telnet://<azure-vm-public-ip>:80Error Medic Editorial
Error Medic Editorial comprises seasoned Site Reliability Engineers and Cloud Architects dedicated to dissecting complex infrastructure issues and providing pragmatic, production-ready solutions.