Commands.page Logo

How to Set Wget Connection Timeout in Ubuntu

This article provides a quick guide on configuring connection timeouts for wget on Ubuntu Linux. It covers the specific command-line flags required to limit how long wget waits to establish a connection before failing. Using these settings helps automate scripts and prevent hanging processes when servers are unreachable.

Use the –connect-timeout Flag

To set a timeout specifically for the connection attempt, use the --connect-timeout option. This flag defines the maximum time wget will wait to connect to the server before aborting. The value is specified in seconds.

Run the following command in your terminal:

wget --connect-timeout=5 https://example.com/file.zip

In this example, wget will stop trying to connect after 5 seconds if no response is received from the server.

Difference Between Connect and Read Timeout

It is important to distinguish between connection timeouts and general timeouts. The --connect-timeout flag only applies to the initial connection phase. Once connected, the download will continue regardless of speed unless you also set a read timeout.

To set a timeout for the entire operation, including connection and data transfer, use the --timeout flag:

wget --timeout=10 https://example.com/file.zip

If you need both, you can combine them:

wget --connect-timeout=5 --timeout=10 https://example.com/file.zip

Make Settings Permanent

If you want to apply these timeout settings globally for every wget command on your Ubuntu system, you can edit the .wgetrc configuration file.

  1. Open the file in a text editor:

    nano ~/.wgetrc
  2. Add the following line to set the default connection timeout:

    connect_timeout = 5
  3. Save and exit the file.

Future wget commands will now use this timeout value by default without needing additional flags.