A “proxy server” is a computer system or application that acts as an intermediary between a client and a server. Its purpose is to facilitate communication between different networked devices by forwarding requests from clients to servers and then returning responses.
This article will illustrate methods to force wget to use a proxy server without modifying system files in Linux.
Method 1: Using Environment Variables to Forcefully Use a Proxy Server (Temporary)
To force wget to use a proxy server, there is a need to set environment variables “http_proxy” and “https_proxy”. These variables are utilized to specify the proxy server via “HTTP” and “HTTPS” requests. These variables tell “wget” where to find the proxy server.
Syntax:
The basic syntax to force wget for using a proxy server is provided below:
$ export http_proxy=http://proxyserver:port/
$ export https_proxy=https://proxyserver:port/
In this syntax, “proxyserver” refers to the “hostname” or “IP address” of the proxy server, and “port” represents the “port number” that proxy server is listening on. In addition, the “http_proxy” uses the HTTP protocol, while “https_proxy” uses the https protocol.
Example:
An example is considered to replace the “proxyserver” and “port” variables with the IP address “192.168.157.132” and “22” port:
$ export http_proxy=http://192.168.157.132:22/
$ export https_proxy=https://192.168.157.132:22/
In this way, a proxy server can be used without modifying system files.
Use wget With the Proxy Server
Now, use “wget” to download files with the proxy server. For instance, specify the “http://google.com” in the following command:
$ wget http://google.com
The above command downloads the file from “google.com” using the proxy server specified in the environment variables.
Note: The environment variables set are only valid for the current session. If a user opens a new terminal or logs out, set the environment variables again.
Method 2: Using .bashrc file to Forcefully Use a Proxy Server (Permanent)
To make the environment variables permanent, modify the “.bashrc” file by adding a script to it. The practical implementation is given below:
Example:
The nano text editor is utilized by specifying the file name “.bashrc” in the terminal:
$ sudo nano ~/.bashrc
It navigates to the text editor and adds the following lines to the “.bashrc” file. These lines set the environment variables every time when opening a new terminal:
$ export http_proxy=http://192.168.157.132:22/
$ export https_proxy=https://192.168.157.132:22/
Save and Exit the editor.
Use wget With the Proxy Server
To confirm the working of the proxy server, download the HTML content of “http://itslinuxfoss.com” using the “wget” utility as follows:
$ wget http://itslinuxfoss.com
The output shows that the “index.html.1” file has been downloaded.
Conclusion
To forcefully use wget for a proxy server without modifying system files, execute the “<export http_proxy=http://proxyserver:port/>” and “<export https_proxy=https://proxyserver:port/>” commands. Additionally, set environment variables in the “.bashrc” file to permanently use a proxy server.
This guide has explained possible methods to download files with wget using a proxy server.