LinuxMedium
Fix: Linux TCP Connection Refused
curl/wget returns 'Connection refused'
!Symptoms
- curl/wget returns 'Connection refused'
- Application unable to connect to a local or remote service
- Telnet to the port immediately returns 'Connection refused'
- Service appears to be running but not accepting connections
?Root Causes
- Service not listening on the expected port
- Service bound to 127.0.0.1 (localhost) instead of 0.0.0.0
- Firewall (iptables/nftables/ufw) blocking the port
- Service crashed and restarted but not yet accepting connections
- Wrong port number — service running on a different port
#Diagnosis Steps
- 1Check if port is listening: `ss -tlnp | grep <port>`
- 2Check what address it's bound to: `ss -tlnp | grep <port>` — look for 0.0.0.0 vs 127.0.0.1
- 3Check firewall: `sudo iptables -L -n` or `sudo ufw status`
- 4Check if service is running: `systemctl status <service>`
- 5Try connecting locally: `curl http://127.0.0.1:<port>/`
>Fix
- 1Start the service: `systemctl start <service>`
- 2Change bind address from 127.0.0.1 to 0.0.0.0 in the service config
- 3Open the port in the firewall: `sudo ufw allow <port>/tcp`
- 4Fix the connection string to use the correct port
- 5Wait for the service to fully start before connecting
*Prevention
- Use health check scripts that verify port availability after deployment
- Configure services to bind to 0.0.0.0 in production (or the specific interface)
- Document expected ports in service configuration
- Include firewall rules in infrastructure automation
- Set up monitoring for port availability on all critical services
Related Error Messages
Connection refusedconnect ECONNREFUSEDFailed to connect to localhost portNo connection could be made because the target machine actively refused it