How Does wget Handle IPv4 and IPv6 Connections?
The GNU wget utility is a powerful command-line tool for
downloading files from the web, built to navigate both IPv4 and IPv6
network environments seamlessly. By default, wget
automatically resolves domain names to available IP addresses,
prioritizing IPv6 when available but gracefully falling back to IPv4 if
the connection fails. Network administrators and users can also
explicitly control this behavior using specific command-line flags to
force a particular protocol or manage fallback settings.
Default Resolution and Dual-Stack Behavior
In modern networking environments, many domains use a dual-stack
configuration, meaning they possess both IPv4 (A records) and IPv6 (AAAA
records) addresses. When you initiate a request, wget
leverages the operating system’s underlying name resolution functions
(such as getaddrinfo).
- IPv6 Preference: If the operating system and the
destination server both support IPv6,
wgetwill attempt to establish the connection using the IPv6 address first. - Automatic Fallback: If the IPv6 connection times
out or returns a connection-refused error,
wgetautomatically drops back to the IPv4 address to complete the download without interrupting the user.
Forcing a Specific IP Version
There are scenarios where you must bypass wget’s
automatic selection—such as troubleshooting a broken IPv6 tunnel or
testing a specific network path. You can force wget to use
one protocol exclusively by employing the following flags:
- Force IPv4 Only: Use the
-4or--inet4-onlyflag. This restrictswgetto resolving and connecting only via IPv4.
wget -4 https://example.com/file.zip- Force IPv6 Only: Use the
-6or--inet6-onlyflag. This restrictswgetto resolving and connecting only via IPv6. If the host does not have an AAAA record, the command will fail.
wget -6 https://example.com/file.zipHandling Explicit IP Addresses in URLs
When bypassing DNS entirely and passing raw IP addresses directly
into the wget command, the syntax changes based on the
protocol version.
- IPv4 Syntax: Standard dot-decimal notation requires no special formatting.
wget http://192.0.2.1/file.zip- IPv6 Syntax: Because IPv6 addresses contain colons
(
:), which conflict with the standard URI port separator, you must enclose the IPv6 address in square brackets.
wget http://[2001:db8::1]/file.zipPersistent Configuration
If you consistently operate in an environment where one protocol is
unreliable, you can modify your global or user-specific configuration
file (/etc/wgetrc or ~/.wgetrc). Adding the
line prefer-family = IPv4 or
prefer-family = IPv6 alters the default sorting order of
resolved addresses, giving you permanent control over how
wget navigates dual-stack networks.