Force wget To Use IPV4 or IPV6 Connection to Download Anything

Force Wget To Use Either IPv4 Or IPv6 Connection To Download Anything

Wget is the command-line tool through which you can download anything from the internet using HTTP, HTTPS, and FTP protocols. It supports IPv4 (Internet Protocol version 4) as well as IPv6 (Internet Protocol 6) addresses to connect to the internet.

If for some reason you wish to connect with either IPv4 or IPv6 protocols, then you have to specify it each time you use the wget command, or you would have to change the default settings. In this article, we will take a look at exactly that procedure.

Also read: wget vs. curl – What is the difference between the wget and curl commands?

Installing wget

The tool is installed in most the Linux distributions by default, so first, check its version by typing the following commands in the terminal:

wget --version
Look Up The Installed Version Of Wget On Your OS
Look Up The Installed Version Of Wget On Your OS

If the above result does not show up, you can install the tool from the command line, depending upon your Linux distribution, as follows :

For Debian and Ubuntu-based distributions

In the terminal, type and execute the following commands :

sudo apt update && sudo apt install wget

On Fedora Workstation

Fedora has dnf and yum package manager, so you can install any application from the terminal like this :

sudo dnf install wget

On Arch Linux and Arch-based distributions :

On Arch and its derivative operating systems, pacman is used as the package manager. So, the installation process looks like this :

sudo pacman -S wget

Enforcing IPv4 & IPv6 protocols for a single-use

If you simply use the wget command along with a URL, then by default, it connects to the internet using both 32-bit IPv4 addresses and 128-bit IPv6 addresses such as:

wget https://ftp.gnu.org/gnu/fdisk/fdisk-0.9.1.tar.bz2
By Default Wget Connects To Both IPv4 And Ipv6
By Default, Wget Connects To Both IPv4 And Ipv6

To use the IPv4 protocol, we need to use the –inet4-only flag along with the command:

wget --inet4-only https://ftp.gnu.org/gnu/fdisk/fdisk-0.9.1.tar.bz2
This Time, Download Occurred Over IPv4 Only
This Time, Download Occurred Over IPv4 Only

Similarly, you can use the –inet6-only flag to force the download to commence via IPv6 protocol like this:

wget --inet6-only https://ftp.gnu.org/gnu/fdisk/fdisk-0.9.1.tar.bz2
Downloading Files Over IPv6 Only
Downloading Files Over IPv6 Only

Enforcing IPv4 & IPv6 protocols permanently

To enforce anything permanently, you have to modify the .wgetrc file located at ~/.wgetrc or at /etc/wgetrc for a single user or for all users, respectively. Open the file using a text editor such as vim or nano:

vim ~/.wgetrc
# or vim /etc/wgetrc
Opening Wgetrc File In A Text Editor
Opening Wgetrc File In A Text Editor

Now, just add (or uncomment by removing #) the following to the file to force IPv4 or IPv6:

inet4_only = on/off

# or

inet6_only = on/off

Summary

I’d urge that you should only use the temporary flags to use IPv4 or IPv6 unless you know what you’re doing. If you force the IPv6 and the host DNS does not have that connection, then you will encounter ‘Failed: No address associated with hostname’ error.

References

Archwiki – Wget