curl Resolve: Override DNS Without Breaking TLS

A laptop displaying a terminal beside a simple network diagram.

Written by

in

You can point curl at any server you choose for a single request, without touching DNS records or your hosts file, and still keep HTTPS working correctly. That matters when you need to check a new origin, a single load-balanced node, or a staging box before a cutover, and the certificate has to validate against the real hostname.

A laptop displaying a terminal beside a simple network diagram.

Use curl --resolve host:port:address with the original URL: curl connects to the IP address you supply while sending the URL’s hostname in the Host header and TLS SNI, and verifying the certificate against that hostname. The override lives only inside that one curl process, so no other application on your machine sees it.

At PacketTools, this is one of the first commands we reach for during network troubleshooting, because it isolates “is the server healthy?” from “is DNS pointing at the right server?” The examples below use documentation-reserved names and addresses (example.com, 192.0.2.0/24, 2001:db8::/32), so swap in your own values when you run them.

How Do You Send a Request to a Specific IP Address?

A laptop connects along a highlighted network path to a server.

You send a request to a specific IP address by adding one --resolve entry that maps the URL’s exact hostname and port to that address, then requesting the normal URL. The mapping goes into curl’s internal DNS cache, and --verbose or -w '%{remote_ip}' confirms where the connection landed.

Read the –resolve host:port:address Syntax

The format has three colon-separated fields:

  • host: the hostname exactly as it appears in the URL
  • port: the port curl will connect to for that URL (80 for http://, 443 for https://, or whatever explicit port the URL carries)
  • address: the numeric IP address curl should use, with no hostname allowed

--resolve inserts the address into curl’s DNS cache, so curl behaves as if a real lookup returned your address. The substitution only fires when the hostname and port pair in the URL matches the entry exactly. A mapping for port 80 does nothing for an https:// URL.

If you want a refresher on how addresses are structured before you pick one, the IP address basics cover IPv4 and IPv6 notation.

Try an HTTP Example With a Documentation-Reserved Address

Here is a plain HTTP request sent to a test server at 192.0.2.10:

curl --resolve example.com:80:192.0.2.10 http://example.com/

curl skips the system resolver for example.com on port 80, opens a TCP connection to 192.0.2.10, and sends Host: example.com. Name-based virtual hosting on the target server picks the right site, just as it would for real traffic.

Add -o /dev/null if you only care about the status and timing. Without it, curl prints the body to your terminal; with it, curl shows its progress meter instead, which is a quick way to see transfer size and speed.

Test HTTPS While Keeping the Original URL Hostname

For HTTPS, change the port to 443 and keep https:// in the URL:

curl --resolve example.com:443:192.0.2.10 https://example.com/

Three things stay tied to example.com: the Host header, the SNI field in the TLS handshake, and the name curl checks against the server certificate. Only the destination IP changes. If the certificate on 192.0.2.10 does not cover example.com, curl fails, and that failure is useful information, so leave certificate verification on.

Non-default ports follow the same rule. For https://example.com:8443/, the entry is example.com:8443:192.0.2.10.

Verify the Destination With –verbose and remote_ip

Run the same command with -v and read the first lines:

curl -v --resolve example.com:443:192.0.2.10 https://example.com/ -o /dev/null

You should see a line noting that the entry was added to the DNS cache, followed by a connection line showing 192.0.2.10 port 443. If the connection line shows a different address, the mapping did not match.

For scripts, the -w write-out variable %{remote_ip} shows the address curl connected to:

curl -s -o /dev/null -w '%{http_code} %{remote_ip}n' 
  --resolve example.com:443:192.0.2.10 https://example.com/

One line of output gives you the status code and the IP address, which is enough to loop over a list of backends.

Why Does Keeping the Hostname Matter for TLS?

Keeping the hostname in the URL matters because curl takes the SNI name and the certificate-verification name from the URL, before any HTTP header is sent. Put an IP address in the URL and the TLS handshake carries the wrong identity; keep the hostname and use --resolve, and the handshake matches what real clients send.

How the URL Hostname Controls Host, SNI, and Certificate Checks

A TLS server hosting many sites picks a certificate from the Server Name Indication value in the ClientHello. That happens before encryption is established, so the server has no HTTP request to look at yet.

curl extracts the name for both SNI and certificate verification from the URL, and server certificates are rarely issued for bare IP addresses. With --resolve, the URL still says example.com, so:

Element Value with --resolve
TCP destination 192.0.2.10 (your override)
TLS SNI example.com
Certificate name check example.com
HTTP Host header example.com

Every identity signal matches production traffic; only the routing differs.

Why Changing Only the Host Header Is Not Equivalent

A Host-header test looks like this:

curl -H "Host: example.com" https://192.0.2.10/

The HTTP layer sees example.com, but the TLS layer sees 192.0.2.10. curl sends no useful SNI name, the server may hand back its default certificate, and verification fails because the certificate does not list the IP. A typical result is curl: (51) SSL: no alternative certificate subject name matches target host name.

Adding -k hides that error and also hides any real certificate problem on the new server, which defeats the point of the test. Keep verification enabled and fix the method.

The Host-header approach also breaks redirect following: with -L, curl sends your custom Host header on every hop, even when the redirect goes to another host. Plain HTTP on a single host is the only case where a Host-header test gives you a comparable answer.

When Should You Use –connect-to Instead?

Use --connect-to when you know the backend’s name and want curl to resolve it for you. It maps a source host and port to a destination host and port:

curl --connect-to example.com:443:node1.example.com:443 https://example.com/

curl looks up node1.example.com, connects there, and still uses example.com for SNI, certificate checks, and the Host header. That suits load-balanced pools whose node names are stable but whose addresses change.

Pick --resolve when you already have the exact IP address. Pick --connect-to when you have a name, or when you need to change the port too, such as sending example.com:443 traffic to node1.example.com:8443.

How Do You Test Redirects, IPv6, and Application Requests?

A software engineer tests network connections on a laptop.

You test redirects, IPv6, and application requests by stacking one --resolve entry per hostname and port the request touches, bracketing IPv6 addresses, and adding your normal method, cookie, and auth flags. Each extra flag behaves exactly as it would against production, because the override only affects where curl connects.

Add a Mapping for Each Redirect Hostname and Port

curl does not follow redirects unless you pass -L, and with it enabled curl follows up to 30 redirects by default. Each hop resolves its own hostname and port, so a redirect from http://example.com to https://www.example.com needs two entries:

curl -IL 
  --resolve example.com:80:192.0.2.10 
  --resolve www.example.com:443:192.0.2.10 
  http://example.com/

Without the second entry, the HTTPS hop quietly goes to whatever public DNS returns, and you end up testing two different servers in one command. Check every Location: header in the -I output and confirm each hop has a matching mapping.

Use Bracketed IPv6 Addresses and Multiple Destination Addresses

IPv6 addresses go inside square brackets in the address field:

curl --resolve 'example.com:443:[2001:db8::10]' https://example.com/

Quote the argument so your shell does not interpret the brackets. You can also list several addresses separated by commas, and curl tries them in turn:

curl --resolve 'example.com:443:192.0.2.10,[2001:db8::10]' https://example.com/

Mixing families is handy for dual-stack checks, but if one family fails silently and curl falls back, run the test twice with -4 and -6 and read %{remote_ip} to see which path served the response.

Compare CDN and Origin Responses Before a DNS Cutover

Before you change a record, request the same URL through the CDN edge and straight to the origin, then diff the results:

curl -sI https://example.com/ > cdn.txt
curl -sI --resolve example.com:443:198.51.100.20 https://example.com/ > origin.txt
diff cdn.txt origin.txt

Look at status codes, Cache-Control, Location, and security headers. Mismatches there are what users would notice right after the switch. Once the change is live, a DNS propagation checker tells you when public resolvers have picked up the new address, so you know when your --resolve tests stop being predictions.

Preserve POST, PUT, Cookies, and Authentication During Testing

Application requests need no special handling. The override sits below HTTP, so methods, bodies, and headers pass through unchanged:

curl --resolve api.example.com:443:192.0.2.10 
  -X PUT -H "Content-Type: application/json" 
  -H "Authorization: Bearer $TOKEN" 
  -b cookies.txt -c cookies.txt 
  -d '{"status":"ok"}' https://api.example.com/v1/items/42

Cookies match api.example.com because that is still the URL hostname. Watch two behaviors on redirects: a 301, 302, or 303 turns POST into GET unless you add --post301, --post302, or --post303, and curl withholds credentials from a different host unless you pass --location-trusted.

If the target sits behind rate limiting keyed on client IP, your test traffic counts against the same limit as real users from your address. You can confirm which public address that is with What Is My IP.

What Should You Check When the Override Does Not Work?

When the override seems ignored, the cause is almost always a mismatch between the entry and the URL, a proxy doing the resolving, or a TLS failure being mistaken for a routing failure. Work through them in that order, using -v output as your evidence.

Check Hostname Matches, Ports, Punycode, and Address Families

Compare your entry field by field with the URL:

  • Hostname: www.example.com and example.com are different keys. A trailing dot also counts as different.
  • Port: an https:// URL with no port needs :443:, not :80:.
  • Punycode: internationalized names must be in ASCII form. The libcurl documentation states that IDN names need to be converted to punycode first, so use the xn-- form in the entry.
  • Address family: if you pass -4 with an IPv6-only entry (or -6 with IPv4), curl will not use that address.

The verbose “Added … to DNS cache” line confirms curl parsed the entry. If it appears but the connection line shows another IP, the key did not match.

Determine Whether a Proxy Is Resolving the Destination

With a proxy configured, curl hands the hostname to the proxy, and the proxy resolves it, so your local DNS cache entry never comes into play. Users report that --resolve works correctly without an HTTP proxy and stops taking effect once one is in the path.

Check for http_proxy, https_proxy, and ALL_PROXY environment variables, plus a ~/.curlrc file. Then rerun with --noproxy '*' to bypass them. If the proxy is mandatory on your network, run the test from a host that can reach the destination directly.

Separate TLS Certificate Failures From Connection Failures

Read the curl exit code and the last verbose lines before changing anything:

Symptom Meaning Next step
(7) Failed to connect TCP never opened Check firewall, listener, and port on the target
(28) Timeout Packets dropped or filtered Run a ping test and traceroute to the IP
(35) handshake error TLS negotiation failed Check protocol versions and SNI handling on the server
(51) or (60) Certificate name or chain problem Fix the certificate on the target

A certificate error proves that routing worked: curl reached your address and received a certificate. The fix belongs on the server, so install the correct certificate and chain there and leave -k out of the command.

Cross-Check DNS and HTTPS With PacketTools

Once curl confirms the target serves correctly, compare that with what the public sees. PacketTools’ DNS Lookup shows the records resolvers return today, and the SSL/TLS Checker validates the certificate chain from outside your network, with no queries or test data logged or stored.

If public answers disagree with what you expect, the steps to troubleshoot DNS issues and a local nslookup command against specific resolvers will show where the stale or wrong record comes from.

How Do You Apply Custom Resolution in libcurl?

In libcurl you set CURLOPT_RESOLVE to a curl_slist of HOST:PORT:ADDRESS strings before calling curl_easy_perform. It uses the same format as the command line and adds controls for cache expiry and entry removal.

Build a CURLOPT_RESOLVE List and Perform a Transfer

A minimal transfer looks like this:

#include <curl/curl.h>

int main(void)
{
  struct curl_slist *hosts = NULL;
  hosts = curl_slist_append(hosts, "example.com:443:192.0.2.10");
  hosts = curl_slist_append(hosts, "example.com:443:[2001:db8::10]");

  CURL *curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_RESOLVE, hosts);
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "%sn", curl_easy_strerror(res));
    curl_easy_cleanup(curl);
  }
  curl_slist_free_all(hosts);
  return 0;
}

TLS behaves as on the command line: SNI and certificate checks use example.com. A quick shortcut is to append --libcurl out.c to a working curl command, and curl writes an equivalent C template for you.

Manage List Lifetime, Cache Expiration, and Entry Removal

libcurl does not copy the list, so keep it allocated until every transfer that uses the handle has finished, then free it with curl_slist_free_all.

Entries behave like this:

  • HOST:PORT:ADDRESS never expires from the DNS cache.
  • +HOST:PORT:ADDRESS times out like a normal cache entry.
  • -HOST:PORT removes an earlier entry; host and port must match exactly.
  • * as the host matches every name, and specific entries take priority over it.

Setting the option again replaces the previous list, and setting it to NULL turns it off. Because entries persist in the handle’s cache, a reused handle keeps routing to your address until you remove them.

Check Protocol Support and Version Requirements

CURLOPT_RESOLVE affects every protocol libcurl supports, so the same override works for FTP, FTPS, SFTP, SCP, SMTP, and WebSocket transfers, not only HTTP. Use the port that protocol connects on (22 for SFTP, 25 or 587 for SMTP).

Feature support arrived over several releases:

Feature Minimum version
Option added 7.21.3
-HOST:PORT removal 7.42.0
Bracketed addresses 7.57.0
Multiple addresses per entry 7.59.0
+ expiring entries 7.75.0
IPv6 address as the host field 8.13.0

Check curl --version on older distributions, since an LTS release can predate the bracketed or multi-address forms. The curl man page lists the matching command-line behavior for your installed version.

Test the Destination Without Changing Its Identity

curl --resolve lets you choose where a request goes while everything else about it stays real. Keep the hostname and port in the URL, map that exact pair to your target address, and curl sends the correct Host header, the correct TLS SNI, and verifies the certificate for that name.

Use a Host-header-only test just for plain HTTP, and switch to --connect-to when you know a backend’s name instead of its IP address. For redirects, add one entry per hostname and port; for IPv6, bracket and quote the address.

When something fails, read -v output first: confirm the cache entry was added, check %{remote_ip}, rule out proxies, and treat certificate errors as server fixes with verification left on.

Comments

Leave a Reply

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