Fix Curl: Failed to connect to host Connection refused

If you’ve ever worked with web servers, APIs, or command-line tools, you’ve almost certainly run into this frustrating message: curl: (7) Failed to connect to host... Connection refused.

It’s a common roadblock that can stop you in your tracks. But what does it actually mean? And more importantly, how do you fix it?

Think of it like knocking on a door. “Connection refused” doesn’t mean no one is home (that would be a “timeout” error).

It means someone is home, but they’ve actively denied your request to enter. The server you’re trying to reach is there, but it’s not letting you in through the specific “door” (the port) you’re trying to use.

This guide will walk you through the most common reasons for this error and provide simple, step-by-step solutions to get you back on track.


First Things First: What is curl?

Before we dive into troubleshooting, let’s quickly clarify what curl is.

curl (which stands for “Client URL”) is a powerful command-line tool used to transfer data to or from a server.

Developers use it for everything from testing API endpoints and downloading files to automating web-related tasks. It’s a fundamental part of a developer’s toolkit.

Now, let’s figure out why that server is giving you the cold shoulder.

The Top 3 Reasons for “Connection Refused” (And How to Fix Them)

There are two primary culprits behind this error: either nothing is listening at your destination, or a firewall is blocking the way. Let’s break this down into a simple checklist.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

1. Is the Server or Application Actually Running?

This might sound obvious, but it’s the most common reason for the error. You can’t connect to a service that isn’t running.

  • What’s Happening: The application or web server (like Apache, Nginx, or a custom app) on the remote machine has crashed, been stopped, or failed to start.
  • How to Check:
    • If you have access to the server, use a command like ssh to log in.
    • Check the status of your application. For example, with a service managed by systemd (common on modern Linux), you would run:
      sudo systemctl status your-application-name


    • Look at the application’s logs for any startup errors.
  • How to Fix:
    • If the service is stopped, simply start it.
      sudo systemctl start your-application-name


    • If it failed, examine the logs to debug the problem. It could be a typo in a configuration file or a missing dependency.

2. Are You Knocking on the Right Door (Port)?

A server can have thousands of ports, and a service will only “listen” for connections on the specific port it’s configured to use. Web servers typically use port 80 (for HTTP) and 443 (for HTTPS). If you try to connect to the wrong port, the server will refuse the connection.

  • What’s Happening: Your curl command is pointing to a port where no service is actively listening for new connections.
  • How to Check (on the server):
    • You can use the netstat or the more modern ss command on the server to see which ports are being used. These commands list all active network connections and listening ports.
      # This command shows all listening TCP and UDP ports and the programs using them
      sudo netstat -tulpn | grep LISTEN


    • Look through the output for the port number you’re trying to connect to. If you don’t see it, nothing is listening there.
  • How to Fix:
    • Double-check your curl command: Ensure you have the correct port number. For example: curl http://your-domain.com:8080. If you don’t specify a port, curl defaults to port 80 for HTTP.
    • Check your server application’s configuration: Make sure your application is configured to listen on the port you expect.

3. Is There a Firewall in the Way?

Firewalls are the security guards of networks. They stand between you and the server, deciding whether to let your connection request through. A misconfigured firewall is a very common cause of “Connection refused” errors.

  • What’s Happening: A firewall on the server itself (like ufw, firewalld, or iptables) or a network firewall (like an AWS Security Group) is blocking incoming traffic on the port you’re trying to access.
  • How to Check:
    • On the server: Check the status and rules of the local firewall.
      • For ufw (common on Ubuntu): sudo ufw status
      • For firewalld (common on CentOS/RHEL): sudo firewall-cmd --list-all
    • For cloud providers: Log in to your cloud console (AWS, Google Cloud, Azure) and inspect the firewall rules or security groups associated with your server instance. Ensure there’s an “inbound rule” that allows traffic on your target port from your IP address (or from anywhere, using 0.0.0.0/0).
  • How to Fix:
    • Add a rule to the firewall to allow incoming connections on the necessary port. For example, to allow traffic on port 8080 with ufw:
      sudo ufw allow 8080


Putting It All Together: A Simple Troubleshooting Workflow

Feeling overwhelmed? Just follow these steps in order:

  1. Check Your Command: Is the hostname and port number in your curl command correct? Typos happen!
  2. Check the Server Application: Log in to the server and confirm that your application is running without errors.
  3. Check for Listening Ports: While on the server, use sudo netstat -tulpn to see if any application is listening on the port you’re targeting.
  4. Check the Firewall: If an application is listening, the firewall is the most likely suspect. Check both the server’s local firewall and any network-level firewalls.

By following this simple checklist, you can quickly diagnose the root cause of the “Connection refused” error and get your services talking to each other again.

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 546

Leave a Reply

Your email address will not be published. Required fields are marked *