Forcing wget to use a specific IP version keeps downloads consistent when servers, firewalls, or proxies handle IPv4 and IPv6 differently. Explicit selection of the address family isolates connectivity issues to one stack and clarifies whether failures originate in DNS resolution, routing, or upstream services. Predictable addressing is particularly important for automation, mirror infrastructure, and scheduled jobs that must behave the same way on every run.

By default, wget asks the system resolver for both A and AAAA records and relies on the operating system’s address selection policy to decide which address to contact. The command-line options -4 and -6, and their long equivalents --inet4-only and --inet6-only, override this policy for the lifetime of a single invocation. Persistent configuration directives such as inet4_only and inet6_only in configuration files provide the same control without repeating options on every command line.

The examples use the GNU build of wget on Linux, where configuration is read first from /etc/wgetrc and then from per-user files such as ~/.wgetrc. Forcing a family that the local network does not support produces immediate connection errors instead of graceful fallback, and system-wide changes in /etc/wgetrc affect every script and user on the host. Testing commands in a private configuration file before applying a global policy avoids surprises on dual-stack and IPv6-only systems.

Steps to force IPv4 or IPv6 in wget:

  1. Call wget with the -4 option to perform a one-off download over IPv4 only.
    $ wget -4 -O /tmp/index-ipv4.html https://example.com/
    --2025-12-07 10:15:00--  https://example.com/
    Resolving example.com (example.com)... 93.184.216.34
    Connecting to example.com (example.com)|93.184.216.34|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1256 [text/html]
    Saving to: '/tmp/index-ipv4.html'
    
    /tmp/index-ipv4.html  100%    1256  ##### snipped #####

    Flag -4 is equivalent to --inet4-only and restricts connections to IPv4 A records even if AAAA records are present.

  2. Call wget with the -6 option to perform a one-off download over IPv6 only.
    $ wget -6 -O /tmp/index-ipv6.html https://example.com/
    --2025-12-07 10:16:00--  https://example.com/
    Resolving example.com (example.com)... 2606:2800:220:1:248:1893:25c8:1946
    Connecting to example.com (example.com)|2606:2800:220:1:248:1893:25c8:1946|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1256 [text/html]
    Saving to: '/tmp/index-ipv6.html'
    
    /tmp/index-ipv6.html  100%    1256  ##### snipped #####

    IPv6-only mode fails immediately with errors such as Network is unreachable or Temporary failure in name resolution on systems without functional IPv6 connectivity.

  3. Enable a persistent IPv4 preference for the current account by turning inet4_only on in ~/.wgetrc.
    $ printf "inet4_only = on\n" >> ~/.wgetrc
    $ tail -n3 ~/.wgetrc
    ##### snipped #####
    inet4_only = on

    Setting inet4_only = on in /etc/wgetrc forces IPv4-only behaviour globally, which can prevent access to IPv6-only resources and break scripts that expect dual-stack resolution.

  4. Prefer IPv6 persistently instead by disabling inet4_only and enabling inet6_only in the same configuration file.
    $ printf "inet4_only = off\ninet6_only = on\n" >> ~/.wgetrc
    $ grep -E "inet[46]_only" ~/.wgetrc
    inet4_only = off
    inet6_only = on

    Only one of inet4_only or inet6_only should be set to on to avoid confusing resolution rules.

  5. Confirm that the desired IP family is in use by running a verbose download and inspecting the resolved address in the output.
    $ wget -S -O /dev/null https://example.com/
    --2025-12-07 10:17:00--  https://example.com/
    Resolving example.com (example.com)... 93.184.216.34
    Connecting to example.com (example.com)|93.184.216.34|:443... connected.
      HTTP/1.1 200 OK
    ##### snipped #####

    An IPv4 dotted-decimal address indicates IPv4 use, while a colon-separated address indicates IPv6.

Discuss the article:

Comment anonymously. Login not required.