How to Force Curl to Use IPv4 Instead of IPv6
When making network requests, curl may default to IPv6
if it is enabled on your system and supported by the destination server.
However, network configuration issues, firewall rules, or legacy server
limitations often require you to restrict your requests to IPv4. This
article provides a direct, step-by-step guide on how to force
curl to use an IPv4 address using command-line arguments
and configuration files.
Use the -4 or –ipv4 Flag
The easiest and most common way to force curl to resolve
names to IPv4 addresses only is by using the -4 or
--ipv4 flag. This tells the tool to ignore any IPv6
addresses returned by DNS resolution.
To use this flag, append it directly to your command:
curl -4 https://example.comAlternatively, you can use the long-form equivalent:
curl --ipv4 https://example.comIf you are debugging a connection and want to verify that
curl is indeed using IPv4, you can combine the command with
the verbose flag (-v or --verbose):
curl -4 -v https://example.comIn the output, you will see a line indicating the connection was made
to an IPv4 address (e.g.,
* Connecting to example.com (93.184.216.34) port 443).
Make IPv4 the Default for Curl
If you find yourself constantly adding the -4 flag, you
can set it as a default option by modifying your curl
configuration file.
- Open your
curlconfiguration file (usually located at~/.curlrcon Linux/macOS or_curlrcin the%USERPROFILE%directory on Windows) in a text editor. - Add the following line to the file:
ipv4
- Save and close the file.
Once saved, curl will automatically use IPv4 for all
future requests unless you explicitly override it with the
-6 flag.