How to Fix ‘curl: (6) Could not resolve host’ in Linux

Executing HTTP requests or API calls in Linux using curl only to receive the terminal error: curl: (6) Could not resolve host: example.com stops scripts, webhooks, and package installations. Curl exit code 6 indicates that the local resolver subsystem was unable to translate the provided hostname into an IP address. While this error occasionally stems from simple typos in the target URL, in servers, cloud instances (AWS EC2, DigitalOcean), and Docker containers, it is predominantly caused by broken DNS nameserver configurations in /etc/resolv.conf, stalled systemd-resolved caching daemons, or missing default network gateway routes.

Why Does curl Fail with Exit Code 6 (Could Not Resolve Host)?

Curl relies on the standard C library (glibc) resolver functions (getaddrinfo) to query Domain Name System (DNS) servers. Resolution fails due to:

  • Misconfigured Nameservers in /etc/resolv.conf: Pointing to unreachable, non-responsive, or offline upstream DNS server IP addresses.
  • Stalled systemd-resolved Daemon: Linux’s internal caching DNS stub resolver (127.0.0.53) becoming deadlocked after network interface changes.
  • Missing Protocol Prefix or Extra Quotes in URL: Passing URLs with protocol formatting errors (e.g. curl "https:// example.com" with accidental whitespace).
  • Firewall Dropping Outbound UDP Port 53 Traffic: UFW or iptables rules blocking outbound DNS query datagrams to external resolvers.

Step 1: Test DNS Resolution with dig or nslookup

Isolate whether the resolution failure is exclusive to curl or affects the entire operating system.

  1. Open your Terminal shell.
  2. Query the domain using nslookup or dig:
    nslookup google.com
    # or
    dig +short google.com
  3. If the command outputs: ‘connection timed out; no servers could be reached’ or ‘server can’t find domain’, your system has lost all DNS resolution capability.
  4. If the lookup succeeds and returns an IP, verify the syntax of your curl command to ensure no illegal characters or trailing whitespaces exist in the URL string.

Step 2: Check and Update /etc/resolv.conf Nameservers

Verify that your system references valid, public upstream DNS resolvers.

  1. Inspect your current DNS nameservers:
    cat /etc/resolv.conf
  2. If the file is empty, contains invalid internal IPs, or only lists broken local stubs, edit the configuration:
    sudo nano /etc/resolv.conf
  3. Add reliable public DNS nameservers (such as Google Public DNS and Cloudflare DNS):
    nameserver 8.8.8.8
    nameserver 1.1.1.1
  4. Save the file (Ctrl + O, Enter) and exit (Ctrl + X).
  5. Test your curl request again to verify immediate resolution.

Step 3: Restart and Flush systemd-resolved

Modern Linux distributions (Ubuntu 20.04+, Debian, Fedora) manage DNS resolution via the systemd-resolved service.

  1. Flush stale DNS resolution caches:
    sudo resolvectl flush-caches
  2. Check active DNS server status across all network links:
    resolvectl status
  3. Restart the resolver service:
    sudo systemctl restart systemd-resolved
  4. Verify that the service is running actively without errors:
    sudo systemctl status systemd-resolved

Step 4: Verify Default Network Gateway and Internet Connectivity

If your system cannot route packets outside the local subnet, all external DNS queries will time out.

  1. Ping an external public IP address directly to verify network routing without DNS:
    ping -c 4 8.8.8.8
  2. If the ping fails with ‘Network is unreachable’, inspect your routing table:
    ip route show
  3. If no default via <IP> entry exists, re-establish your default gateway or restart NetworkManager:
    sudo systemctl restart NetworkManager

Step 5: Verify Firewall Rules Allow Outbound Port 53

Ensure outbound UDP and TCP traffic on port 53 is not blocked by local firewalls.

  1. Check active UFW firewall rules:
    sudo ufw status verbose
  2. If outbound traffic is restricted, allow DNS queries explicitly:
    sudo ufw allow out 53
  3. Re-run your curl command to confirm clean network delivery.

Get the best tech tips delivered straight to your inbox.

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