DEV Community 👩‍💻👨‍💻

Peter Cooper
Peter Cooper

Posted on

How to Make curl Request a Site from a Different IP Than in DNS

One issue I've encountered over the years has been when moving a Web site between systems and the site is set up on two different servers simultaneously (this could also happen if using a CDN). Let's say is set up on and but DNS is pointing solely to

What do we do if we want to request but from

A system wide approach would be adding an entry to the /etc/hosts file:
Enter fullscreen mode Exit fullscreen mode

This usually works but can require you to flush your local DNS cache and/or force refresh in your browser. You have to remember to remove the line as well, it's not very configurable, and so on.

What about if we want to use curl? You could use /etc/hosts but for debugging purposes it'd be nice if we could override the IP address on a single request basis as part of the command. This is possible!

The way I used to do it was to override the Host HTTP header (the way a HTTP client tells a server which host it's looking for) and request from the new IP address, like so:

curl -H "Host:"
Enter fullscreen mode Exit fullscreen mode

Fine in the old days, but in the modern HTTPS world various problems are introduced. Luckily curl has a solution in the form of --resolve!

--resolve <host:port:address[,address]...>
       Provide a custom address for a  specific  host  and  port  pair.
       Using  this,  you  can make the curl requests(s) use a specified
       address and prevent the otherwise normally resolved  address  to
       be  used.
Enter fullscreen mode Exit fullscreen mode

So if we want to request from, we could do this:

curl --resolve 
curl --resolve 
Enter fullscreen mode Exit fullscreen mode

Multiple entries can be added if needed for more complex situations like chains of redirects, say.

It's also possible to use * as a wildcard host so that any host involved in the request goes to the address you specify (and it could also save you a bit of redundancy and typing!):

curl --resolve *:80:
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

vaviloff profile image

Wow, curl never ceases to amaze me, thanks for this awesome tip!

peterc profile image
Peter Cooper

I suspect there is a LOT it can do that hasn't even crossed my mind. I should man curl and read through the options one day :-)

Build Anything...

Use any Linode offering to create something for the DEV x Linode Hackathon 2022. A variety of prizes are up for grabs, inculding $1,000 USD. 👀

Join the Hackathon <-