How to Force wget to Use IPv4 Only?
When downloading files via the command line, you may encounter
networking issues where a server prefers IPv6, but your local network or
ISP doesn’t properly support it. This can cause the wget
command to hang, time out, or fail entirely. This article provides a
quick overview of how to force wget to connect using only
IPv4 protocols, covering the temporary command-line flag, a permanent
configuration fix, and troubleshooting steps to ensure a smooth download
process.
The Quick Command Line Solution
The fastest way to force wget to use IPv4 for a single
download is to use the -4 flag (or its long-form
equivalent, --inet4-only). This tells the utility to
completely ignore any IPv6 addresses associated with the domain
name.
wget -4 https://example.com/file.zipAlternatively, you can use the long-form version of the command:
wget --inet4-only https://example.com/file.zipUsing this flag resolves the common issue where wget
gets stuck on the “Connecting to…” stage because it is trying to route
traffic through an unconfigured IPv6 tunnel.
Making the IPv4 Preference Permanent
If you find yourself constantly adding the -4 flag
because your network environment lacks stable IPv6 routing, you can make
this behavior the default by modifying your wget
configuration file (.wgetrc).
Step 1: Open or Create the Configuration File
Open the wget configuration file in your home directory
using a text editor like nano:
nano ~/.wgetrcStep 2: Add the Prefer Family Directive
Add the following line to the file, which instructs wget
to always prefer or restrict connections to IPv4 addresses:
prefer_family = IPv4
Step 3: Save and Exit
Save the file and exit the editor. From this point forward, running a
standard wget command will automatically favor IPv4
connections without requiring you to manually type the flag every
time.
Verification and Troubleshooting
To verify that your command is working as intended, look at the terminal output immediately after initiating the download.
- Successful IPv4 connection: The terminal will
display a resolving message followed by a standard dotted-quad IP
address (e.g.,
Connecting to example.com (example.com)|192.0.2.1|:443... connected.). - IPv6 connection (unwanted): If you see a
hexadecimal address containing colons (e.g.,
2001:db8::1), the restriction was not applied correctly.
If wget still fails after forcing IPv4, the issue likely
stems from a broader local network misconfiguration, a DNS resolution
failure, or the remote server being temporarily offline rather than an
IP protocol mismatch.