There are two ways of running
wget behind a proxy. Either set the appropriate environment variables and
wget will act according to them or configure the
wget configuration file -
Set the environment variable
Before setting the environment variables, lets first see if there are any preset proxy variables present. We can get that information using the following command.
env | grep proxy
If no proxy environment variable is set, it will go for direct connections. Let's take an example of my college. We use a HTTP proxy server. Running
wget without setting the
http_proxy environment variable just fails. So, lets set this env (environment variable). variable first
wget and it should work like a charm. If your proxy also requires authentication i.e username and password then use this format to set the env:
Similarly, for other type of proxies, set the corresponding env -
Please note that, the above commands will be in effect only till the user session expires, i.e. by running the above commands we are just setting the proxy env. for a session, not permanently. And usually we wouldn't want to run the command everytime we login. So, to make it permanent, we can either set the value in wget's configuration -
wgetrc file or set the environment in
echo "export http_proxy=http://foo.bar:8080/" | tee -a ~/.bashrc
It should be noted that, this way we might be affecting the user's proxy settings. A better way is to set the settings permanently in wget's configuration file, this way no other app. is affected.
Like most of the applications
wget has a configuration file too -
The former is for global changes and the latter one is for local settings(user specific). We will go into the details later, lets just see how to apply the proxy settings. Its similar to setting the proxy environment variable, just exclude the export command. Open the file
~/.wgetrc file. If one doesn't exist, then create it.
Now, add the corresponding statement in the file. Read the section "Set the environment variable" to find this.
How to disable/turn-off the proxy settings
Once you have configured the proxy settings in the
wgetrc file, everytime you run
wget, it will automatically connect to the proxy server. Sometimes for local/LAN based downloads, you may not need the proxy server. In such cases we can turn off the proxy settings using the
--no-proxy argument in the command:
wget --no-proxy http://10.0.0.1/file.tgz
If the proxy requries authentication - Username & Password
We already know the format for setting the environment variable with username and password. Wget provides you two methods to mention the username and password for proxy server.
- Mention it in the command in the form of arguments.
- Set the values in the configuration file,
If the authentication you are using is static, then its better set it in the configuration file. If its just one time thing or if you want to overwrite the settings in configuration file then you can use the
wget --proxy-user=username --proxy-password=password http://foo.bar/file.tgz
For permanent changes, its always advised to change the configuration file.
Once again, for global changes modify the
/etc/wgetrc file otherwise go for
~/.wgetrc file. Open any of the file and append/add the following lines in it.