Wget Proxy: How to run Wget behind a Proxy server

Since, proxy servers are very common today - in companies, universities etc. almost all of the applications today have proxy support. As far as wget is concerned, there are two ways of doing so, either set the appropriate environment variables and wget will act according to them or configure the wget configuration file - wgetrc.

Wget Proxy: 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.

[shredder12]$ env | grep proxy

http_proxy=http://foo.bar:3128
no_proxy=localhost,127.0.0.1

If no proxy environment variable is set, it will go for direct connections. Lets 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. variable first

[shredder12]$ export http_proxy="http://foo.bar:8080"

Now, run Wget and it should work like a charm. If your proxy also requires authentication i.e username & password then use this format

[shredder12]$ export http_proxy="http://username:password@foo.bar:8080"

Similarly, for other type of proxies - ftp_proxy, https_proxy etc. All we need to do is set the appropriate environment variable.

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 ~/.bashrc file.

[shredder12]$ 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.

Wget Proxy: Configuring the wgetrc file

Like most of the applications wget has a configuration file too - wgetrc:

  • /etc/wgetrc,
  • ~/.wgetrc.

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.

[shredder12]$ vim ~/.wgetrc

Now, add the following statement in the file

http_proxy=http://foo.bar:8080

Wget Proxy: 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 --no-proxy argument in the command

[shredder12]$ wget --no-proxy http://10.0.0.1/file.tgz

Wget Proxy: 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, /etc/wgetrc or ~/.wgetrc

If the authentication you are using is static, then its better t 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 --proxy-user & --proxy-password options.

[shredder12]$ 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.

proxy_user=username
proxy_password=password

In a similar way, you can set the ftp proxy along with the authencation credentials.

How to ovewrite a preset proxy in Wget

If you see the first proxy example, we are actually overwriting the pre-defined environment variable(if any). But since, we are executing the export command, rather than editing any configuration file, the proxy setting will be removed as soon as we close the terminal ( in case of GUI, gnome-terminal etc.) or logout of the session.

And, if for some reason we are interested in using separate proxy for wget, then instead of overwriting the variable we can just edit the ~/.wgetrc or /etc/wgetrc file. Please note that, a local wgetrc file (~/.wgetrc) will overwrite the settings on /etc/wgetrc. Once again, remember that ~/.wgetrc is a local/user-specific setting where as /etc/wgetrc is a global one.

Finally, the high command in this hierarchy are the variables provided at command execution time. (--proxy-password, --no-proxy etc.), i.e.

(Exec. time command options) > ~/.wgetrc > /etc/wgetrc > environment

This is just a big picture, only for proxy settings. It might not be the same in all the cases.

1 Comment

Zaid Abdulla (not verified)
October 18th, 2010 07:20 pm
Or you could just use proxychains.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.