BudiBadu Logo
Samplebadu

cURL by Example: IPv4 Only

Latest

Force cURL to use IPv4. Resolves DNS to IPv4 addresses only.

Code

# Force IPv4 connection
curl -4 https://example.com

# Useful if IPv6 is misconfigured or slow
curl --ipv4 -v https://google.com

Explanation

By default, cURL employs a dual-stack approach to networking, attempting to use IPv6 if available and automatically falling back to IPv4 if the IPv6 connection fails. This behavior relies on your system's DNS resolver providing both IPv4 (A records) and IPv6 (AAAA records) addresses for the requested hostname. However, various scenarios require explicit control over which IP protocol version to use: troubleshooting IPv6 connectivity issues, working in IPv4-only networks, testing dual-stack implementations, debugging network unreachable errors, or ensuring consistent behavior regardless of DNS configuration.

The -4 flag (or its long form --ipv4) instructs cURL to exclusively use IPv4 addresses for all connections. When this flag is active, cURL will primarily query for IPv4 A records during DNS resolution and establish connections using the IPv4 protocol stack. This is particularly useful when your network environment is IPv4-only, when IPv6 configuration is broken or misconfigured causing connection delays or failures, when you're diagnosing whether connectivity problems are specific to IPv6, or when you need consistent IPv4 behavior for testing or compliance reasons. If a hostname doesn't have an IPv4 address (only AAAA records), the connection will fail immediately with a "Could not resolve host" error.

Common IPv6 Issues Resolved by -4: Many networks, especially older corporate environments or certain ISPs, have partial or broken IPv6 implementation. Symptoms include very slow connection attempts (cURL tries IPv6, times out after several seconds, then falls back to IPv4), "Network is unreachable" errors for IPv6 addresses, or intermittent connectivity. Using -4 forces immediate IPv4 usage, bypassing problematic IPv6 configuration entirely. This is also valuable for comparing performance: time curl -4 example.com vs time curl -6 example.com reveals which protocol performs better on your network. For debugging, verbose mode combined with -4 shows exactly which IPv4 address was resolved and used: curl -4 -v https://google.com.

When to Force IPv4: Use -4 when working with legacy systems that don't support IPv6, when your ISP or network infrastructure doesn't properly support IPv6 (common in many regions), when debugging to eliminate IPv6 as a variable in connectivity issues, when writing scripts that need predictable behavior regardless of IPv6 availability, or when accessing services you know are IPv4-only to avoid unnecessary IPv6 lookup attempts. The --resolve option provides even more control if needed: curl --resolve example.com:443:93.184.216.34 https://example.com manually overrides DNS entirely, forcing cURL to use a specific IP address you provide.

Code Breakdown

2
-4 restricts DNS resolution to A records.
5
--ipv4 is the long form alias.
All
Helps debug "network unreachable" errors related to IPv6.