How to force IPv4 or IPv6 in wget

Dual-stack name resolution can hide the real cause of a failed download because one address family may work while the other stalls or times out. Forcing wget onto IPv4 or IPv6 makes that difference explicit, which is useful when debugging DNS, routing, firewall policy, or upstream listener problems.

The -4 and -6 flags map to --inet4-only and --inet6-only. They tell wget to ignore the other family completely for that run, and the startup-file directives inet4_only = on and inet6_only = on provide the same behavior when one account needs a persistent preference.

Only one strict family should be enabled at a time. Keep the change on the command line until the same host set consistently needs one family, because a persistent default can break unrelated downloads that rely on normal dual-stack fallback.

Steps to force IPv4 or IPv6 in wget:

  1. Confirm that the current build exposes both family-selection flags before testing a real endpoint.
    $ wget --help | grep -n 'inet4-only\|inet6-only'
    66:  -4,  --inet4-only                connect only to IPv4 addresses
    67:  -6,  --inet6-only                connect only to IPv6 addresses
  2. Run an IPv4-only spider request and confirm that the connection line shows an IPv4 address.
    $ wget -4 --spider https://downloads.example.net/ 2>&1 | grep -E 'Resolving|Connecting to|HTTP request sent'
    Resolving downloads.example.net (downloads.example.net)... 198.51.100.24, 198.51.100.25
    Connecting to downloads.example.net (downloads.example.net)|198.51.100.24|:443... connected.
    HTTP request sent, awaiting response... 200 OK

    With -4 active, wget ignores AAAA records for that run and refuses IPv6 URL literals.

  3. Re-run the same request with IPv6-only mode and confirm that the connection line switches to an IPv6 address.
    $ wget -6 --spider https://downloads.example.net/ 2>&1 | grep -E 'Resolving|Connecting to|HTTP request sent'
    Resolving downloads.example.net (downloads.example.net)... 2001:db8:100::24, 2001:db8:100::25
    Connecting to downloads.example.net (downloads.example.net)|2001:db8:100::24|:443... connected.
    HTTP request sent, awaiting response... 200 OK

    IPv6-only mode fails immediately when the host has no usable IPv6 route, which is the point when isolating stack-specific failures.

  4. Apply the same preference for one command with startup-file syntax when a wrapper script already uses --execute overrides.
    $ wget --execute='inet4_only = on' --execute='inet6_only = off' --spider https://downloads.example.net/ 2>&1 | grep -E 'Resolving|Connecting to|HTTP request sent'
    Resolving downloads.example.net (downloads.example.net)... 198.51.100.24, 198.51.100.25
    Connecting to downloads.example.net (downloads.example.net)|198.51.100.24|:443... connected.
    HTTP request sent, awaiting response... 200 OK

    The --execute form is useful when a launcher already manages other temporary wgetrc directives for one run only.

  5. Persist the working family in /$HOME/.wgetrc/ only when the same account consistently needs that policy.
    ~/.wgetrc
    inet4_only = on
    inet6_only = off

    Do not set both directives to on at the same time. Related: How to configure default options in ~/.wgetrc

  6. Verify the final family choice from the connection line before using it in automation.
    $ wget --server-response --spider --inet4-only https://downloads.example.net/ 2>&1 | grep -E 'Resolving|Connecting to|HTTP/1.1 200'
    Resolving downloads.example.net (downloads.example.net)... 198.51.100.24, 198.51.100.25
    Connecting to downloads.example.net (downloads.example.net)|198.51.100.24|:443... connected.
      HTTP/1.1 200 OK

    Dotted-decimal addresses confirm IPv4, while colon-separated addresses confirm IPv6. Related: How to debug wget connections
    Related: How to set connection and read timeouts in wget