Skip to content
Loopedin Labs

Connection refused (ECONNREFUSED)

Your client can't connect. Work out where the connection dies.

Mini Rush · practice

Three questions before you read

Guessing first makes the answers stick. No sign-in.

A fast failure carries more information than a slow one. Read it before you touch the network.

What it means

The client sent a TCP SYN and got an immediate rejection back. Usually that's a TCP reset (RST) from a host that is up but has no socket listening on that IP and port. Clients show it as ECONNREFUSED, Connection refused, or curl: (7) Failed to connect.

Compare it with the other connect failures:

SymptomUsual meaning
Refused, instantlyhost reached, nothing listening, or a firewall REJECT
Hangs, then times outpackets dropped: security group, NACL, firewall DROP, host down
No route to hostrouting problem, or a REJECT with icmp-host-prohibited
Could not resolve hostDNS, not TCP

Usual causes

  • The process is down, crashed, or still starting.
  • Wrong port: config says 8080, the app listens on 3000.
  • The app is bound to 127.0.0.1 and you're connecting to the machine's real IP.
  • Inside a container, localhost is the container itself, not the host or another container.
  • localhost resolves to ::1 while the server listens only on IPv4.
  • A Kubernetes Service with no ready endpoints, or a targetPort that doesn't match the container port.
  • A host firewall rule with REJECT.

Debug commands

On the target host, see what is actually listening:

ss -ltnp | grep 8080          # LISTEN on 127.0.0.1 or 0.0.0.0 / [::]?
systemctl status app
journalctl -u app -n 50

From the client, test the exact host and port the app uses:

nc -vz 10.0.0.7 8080
curl -v http://10.0.0.7:8080/health
getent ahosts localhost       # does it return ::1 first?

In Kubernetes:

kubectl get endpointslices -l kubernetes.io/service-name=api
kubectl get svc api -o jsonpath='{.spec.ports}'
kubectl get pod -l app=api -o wide

If ss shows the listener but clients are still refused, check the host firewall:

sudo iptables -S | grep -i reject
sudo nft list ruleset | grep -i reject

Fixes

  1. Start or fix the process, and make it fail loudly when it can't bind.
  2. Bind to 0.0.0.0 (or ::) if remote clients must connect.
  3. Use service names between containers, not localhost.
  4. Make the Service selector and targetPort match the pods.
  5. Use 127.0.0.1 instead of localhost when the server is IPv4-only.

Common wrong fix

Opening the port in the security group or cloud firewall. A security group that blocks traffic drops it, and the client times out. If you get an instant refusal, the packet already reached the host, so the cloud firewall is not the problem. Run ss -ltnp on the target before you touch the network rules.