1. Overview

wget is a common network command-line utility for downloading resources from the web.

In this quick tutorial, we’ll take a closer look at its timeout settings.

2. Introduction to wget Timeouts

wget has several options to control different timeouts:

  • –dns-timeout=time (seconds)
  • –connect-timeout=time (seconds)
  • –read-timeout=time (seconds)
  • –timeout=time (seconds)

It’s worth mentioning that all the options above accept decimal values and subsecond values — for example, 0.8.

Usually, we shouldn’t set subsecond timeouts. However, they can be helpful for checking network latency or server response times.

Next, let’s go through the options above and understand what they can control.

3. The –dns-timeout Option

The –dns-timeout option specifies the maximal time DNS lookup can take. Let’s consider an example:

$ wget --dns-timeout=5 https://github.com/eugenp/tutorials/blob/master/README.md

Here, –dns-timeout=5 means DNS lookups that don’t complete within five seconds will be considered fails.

There’s no timeout on DNS lookup by default.

4. The –connect-timeout Option

The –connect-timeout option is for TCP connections. If a TCP connection takes longer than the given value to establish, it will be aborted:

$ wget --connect-timeout=5 https://github.com/eugenp/tutorials/blob/master/README.md

By default, there is no TCP connect timeout.

5. The –read-timeout Option

During a download, wget keeps reading data from the server until the download is finished. However, sometimes, wget receives no data from the server, in which case it’ll wait.

The –read-timeout option refers to the waiting time:

$ wget --read-timeout=5 https://github.com/eugenp/tutorials/blob/master/README.md

In the example above, if wget has waited for data longer than five seconds, it will abort and restart the download.

The default read timeout is 900 seconds.

6. The –timeout Option

The –timeout option means network timeout. Setting –timeout=x is equivalent to setting –dns-timeout=x, –connect-timeout=x, and –read-timeout=x at the same time. Let’s see it in action:

$ wget --timeout=5 https://github.com/eugenp/tutorials/blob/master/README.md

This example sets the DNS timeout, TCP CONNECT timeout, and the read timeout with the same value: 5 seconds.

If we want to disable all timeouts, we can set –timeout=0.

7. Timeout Settings and the Durations of Downloads

So far, we’ve learned various timeout options of wget. However, we should note that these options don’t directly affect the duration of downloads.

And, of course, download durations can be greater than the timeout settings.

Next, let’s see an example:

$ time wget --timeout=5 https://mirrors.xtom.de/archlinux/iso/2021.10.01/archlinux-2021.10.01-x86_64.iso
--2021-10-14 19:09:37--  https://mirrors.xtom.de/archlinux/iso/2021.10.01/archlinux-2021.10.01-x86_64.iso
SSL_INIT
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving localhost (localhost)... 127.0.0.1, 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:8888... connected.
Proxy request sent, awaiting response... 200 OK
Length: 887435264 (846M) [application/octet-stream]
Saving to: ‘archlinux-2021.10.01-x86_64.iso’

archlinux-2021.10.01-x86_64.iso     100%[=================================>] 846.32M  2.22MB/s    in 6m 36s  

2021-10-14 19:16:13 (2.14 MB/s) - ‘archlinux-2021.10.01-x86_64.iso’ saved [887435264/887435264]

real 396.29
user 7.67
sys 15.47

As the example shows, we use wget to download the ISO installation file of Arch Linux. We’ve set five seconds as the timeout (–timeout=5) and used the time command to measure the download duration.

The download was not broken due to a small timeout setting (5 seconds), even though the full download has taken about 400 seconds.

8. Conclusion

In this article, we’ve discussed the timeout options of the wget command.

Usually, we can just take the default timeout settings to start downloads.

However, when we are in a particular network environment, such as one with unstable connections or servers, understanding these timeout options may help us tune the timeouts to finish the downloads successfully.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.