How to Test URL Connectivity with Wget on Ubuntu
This article provides a concise guide on verifying network connectivity to web addresses using the wget utility in Ubuntu. It outlines the specific command flags required to check server availability without downloading content. Readers will learn how to interpret the output to determine if a URL is reachable or if network errors exist.
To test the connectivity of a URL without downloading the actual
file, use the --spider option with the wget command. This
mode tells wget to check if the file exists on the server without
retrieving it. The basic syntax is as follows:
wget --spider https://example.com
When you run this command, wget sends a HEAD request to the server. If the server responds with a success status, the terminal will display a message indicating the remote file exists. If the server is unreachable or the URL is invalid, wget will return an error message detailing the connection failure.
For more robust testing within scripts, you can combine the spider option with timeout and try limits. This prevents the command from hanging indefinitely if the network is unresponsive. Use the following command structure:
wget --spider --tries=1 --timeout=5 https://example.com
In this example, --tries=1 ensures wget attempts the
connection only once, and --timeout=5 sets a five-second
limit for the server response. If the command exits with a status code
of 0, the connectivity test was successful. A non-zero exit code
indicates a connectivity issue, allowing for easy integration into
automated monitoring tools.