How to Fix ‘curl: (7) Failed to connect to localhost port: Connection refused’ in Linux

Testing a local web server, containerised service, or REST API endpoint in Linux using curl http://localhost:8080 only to be met with the error: ‘curl: (7) Failed to connect to localhost port 8080: Connection refused’ halts local development and deployment testing. In TCP/IP networking, a Connection refused (errno 111 / ECONNREFUSED) error indicates that the kernel network stack reached the target IP address (127.0.0.1 or ::1), but no active process was listening on the designated TCP port, prompting the operating system to send an immediate TCP Reset (RST) packet back to curl.

Why Does curl Return (7) Connection Refused on Localhost?

A TCP RST packet indicates the host exists but no service is bound to that port. Causes include:

  • Target Service Is Not Running: The backend web server (e.g. Nginx, Apache, Node.js, Python, or Docker) has stopped, crashed, or failed to start.
  • Service Bound to IPv4 vs IPv6 (localhost resolution mismatch): The server listening strictly on 127.0.0.1 while curl resolves localhost to IPv6 ::1 (or vice versa).
  • Container Port Mapping Omitted in Docker: Running a Docker container without exposing the port using the -p HOST:CONTAINER flag.
  • Service Bound to Wrong Network Interface: The application listening strictly on an internal socket or private IP rather than 0.0.0.0 or 127.0.0.1.
  • Local Firewall (iptables / ufw) Blocking Port: Strict loopback firewall rules rejecting local TCP packets.

Step 1: Verify Which Services Are Listening with ss or netstat

Determine whether any active process is actually listening on your requested port.

  1. Open your terminal and run the socket statistics command:
    sudo ss -tulpn | grep 8080
    (Replace 8080 with your target port number).
  2. Inspect the output:
    • If the output is completely blank, no application is listening on that port.
    • If an entry is listed, observe the Local Address:Port column (e.g. 127.0.0.1:8080 or :::8080).

Step 2: Check Service Status with systemctl

If the service should be running as a background daemon, verify its systemd state.

  1. Check service status:
    sudo systemctl status nginx       # Or apache2, docker, etc.
  2. If the service is inactive (dead) or failed, restart it:
    sudo systemctl restart nginx
  3. If it fails to start, inspect recent startup logs:
    sudo journalctl -u nginx -e --no-pager

Step 3: Test Direct IPv4 vs IPv6 Explicitly

Because localhost can resolve to both IPv4 (127.0.0.1) and IPv6 (::1), test explicit IP addresses.

  1. Force curl to use IPv4:
    curl -4 -v http://127.0.0.1:8080
  2. Force curl to use IPv6:
    curl -6 -v http://[::1]:8080
  3. If connecting via 127.0.0.1 succeeds, update your application configuration to bind to 0.0.0.0 (all interfaces) or adjust /etc/hosts.

Step 4: Verify Docker Container Port Mappings

If accessing an application running inside a Docker container, confirm external port publication.

  1. List running containers and their port configurations:
    docker ps
  2. Look at the PORTS column:
    • If it displays 8080/tcp without a leading host address, the port is only accessible inside the container network.
    • To access from host localhost, the container must be launched with:
      docker run -d -p 8080:8080 image-name

Step 5: Check Local UFW Firewall Loopback Rules

Ensure local firewall policies are not rejecting loopback traffic.

  1. Check active firewall status:
    sudo ufw status verbose
  2. Allow the specific port locally if blocked:
    sudo ufw allow in on lo to any port 8080 proto tcp
  3. Re-run your curl request:
    curl -I http://localhost:8080

Verifying active socket listeners with ss, restarting failed services, testing IPv4 loopbacks directly, and checking Docker port bindings resolves ‘curl: (7) Connection refused’ in Linux.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.