In this article we will discuss using the Linux wget command to respect our system proxy settings. There are multiple ways to accomplish this. Let's dive into them.

Set the http_proxy Environmental Variable

Environmental variables are stored pieces of information that pass data to programs launched from a command shell. The http_proxy variable tells other programs and shell commands where to look for a proxy server. By setting this variable, we can give wget the information it needs.

We cover this extensively in a past article, please refer to:

How to Set the http_proxy Variable in Linux

Configure wget To Work with your Proxy

This option is the preferred way if you only need wget to work with the proxy from the command line, or you need wget to use a different proxy from the one set in the environment with option 1. 

You can edit the wget configuration file to tell it what proxy server to use for http, https and ftp connections. This file resides at /etc/wgetrc on most Linux systems. This was tested on Red Hat, CentOS, Fedora and Ubuntu.

# You can set the default proxies for Wget to use for http, https, and ftp.
# They will override the value in the environment.
#https_proxy = http://proxy.yoyodyne.com:18023/
#http_proxy = http://proxy.yoyodyne.com:18023/
#ftp_proxy = http://proxy.yoyodyne.com:18023/

You will need to uncomment (remove the leading pound sign) these lines and add the correct information like so:

# You can set the default proxies for Wget to use for http, https, and ftp.
# They will override the value in the environment.
https_proxy = http://proxy.yourdomain.com:8080/
http_proxy = http://proxy.yourdomain.com:8080/
ftp_proxy = http://proxy.yourdomain.com:8080/

If your proxy requires authentication, add the following lines to your wgetrc file:

http_proxy=http://proxy.yourdomain.com:8080/
proxy_user=user
proxy_password=password

Save the file and now wget will work on the command line through the proxy you just set.

Set User Specific Proxy Settings for wget

Changing the /etc/wgetrc file will make the change global. If you do not have elevated (root or sudo) privileges, or want a user specific proxy setting, you can create a wgetrc file in your home directory at ~/.wgetrc and add the following lines.

https_proxy = http://proxy.yourdomain.com:8080/
http_proxy = http://proxy.yourdomain.com:8080/
ftp_proxy = http://proxy.yourdomain.com:8080/

Again, if you need authentication, just add the necessary lines.

proxy_user=user
proxy_password=password

Conclusion

The preferred method for configuring wget to use a proxy server is by using the wget specific configuration file. It will however respect the http_proxy environmental variable if it exists.