How to Use Wget with a Proxy Environment Variable?

This article provides a quick overview and practical guide on how to configure and use the wget command-line utility with a proxy server defined via environment variables. You will learn how to set temporary or permanent environment variables for both HTTP and HTTPS traffic, how to prompt wget to automatically recognize these settings, and how to bypass them when necessary. By the end of this guide, you will be able to seamlessly route your command-line downloads through your organization’s or personal proxy server.

Setting Up Proxy Environment Variables

To make wget use a proxy server, the utility looks for specific environment variables in your system. You can define these variables in your terminal session.

To set these variables in a Linux or macOS terminal, use the export command:

export http_proxy="http://proxy.example.com:8080"
export https_proxy="http://proxy.example.com:8080"

If your proxy server requires authentication, you can include your username and password directly within the URL format:

export http_proxy="http://username:password@proxy.example.com:8080"

Verifying Wget Proxy Initialization

By default, wget is built to automatically look for and read the http_proxy and https_proxy environment variables. When you run a standard wget command, it will automatically route the request through the specified server.

wget http://example.com/file.zip

You can explicitly tell wget to use environmental proxy settings by modifying its configuration file (~/.wgetrc) or by ensuring the proxy feature is not disabled. The configuration file uses the following syntax to activate proxy reading:

use_proxy = on

How to Bypass the Proxy Configuration

There are times when you might have proxy environment variables active, but you need to bypass them for a specific request (for example, downloading a file from a local network server).

Option 1: The Command-Line Flag

You can override the environment variables on a per-command basis by using the --no-proxy flag:

wget --no-proxy http://local-server.internal/data.csv

Option 2: The no_proxy Environment Variable

Alternatively, you can define a no_proxy environment variable. This variable contains a comma-separated list of domains or IP addresses that wget should always connect to directly, ignoring the proxy settings:

export no_proxy="localhost,127.0.0.1,local-server.internal"

Making the Proxy Settings Permanent

The export command only applies to your current terminal session. If you close the terminal window, the settings are lost. To make these proxy environment variables permanent for your user account, append the export commands to your shell’s profile configuration file, such as ~/.bashrc, ~/.bash_profile, or ~/.zshrc.

echo 'export http_proxy="http://proxy.example.com:8080"' >> ~/.bashrc
echo 'export https_proxy="http://proxy.example.com:8080"' >> ~/.bashrc

After updating the file, apply the changes to your current session by sourcing the file:

source ~/.bashrc