Console9

curl: (7) Failed to connect — Connection refused

Fix curl error 7 when the remote server refuses the TCP connection on the target port.

curl: (7) Failed to connect — Connection refused

curl produces error 7 when the remote server refuses the TCP connection on the target port.

When curl Produces This Error

curl displays "Failed to connect to {host} port {port}: Connection refused" when the TCP SYN packet reaches the server but no process is listening on the target port. The server responds with a TCP RST (reset) packet.

What Causes Connection Refused Error 7 in curl

The target service is not running, is listening on a different port, or is bound to a different network interface (e.g., 127.0.0.1 instead of 0.0.0.0). A firewall that actively rejects (not drops) packets also produces this error.

How to Fix Connection Refused Error 7 in curl

  1. Verify the target service is running:

    sudo systemctl status nginx
  2. Check which port the service listens on:

    sudo ss -tlnp | grep nginx
  3. Verify the firewall allows traffic on the target port:

    sudo ufw status

How to Verify the Fix

A successful connection returns an HTTP response:

curl -I https://example.com