Wget is a popular command that allows you to download files from URLs. You can use it to download files to current directory, or another specific folder. Wget even allows you to enter username/password for downloads that require authentication. But sometimes you may need to download files that are behind a proxy. In this article, we will learn how to use wget to download file via proxy. Basically, you need to set proxy for wget utility, and thereafter it will properly download files via proxy.
How to Use Wget to Download File Via Proxy
We will need to configure proxy servers for wget in order to be able to download files via proxy. There are 3 ways to do this. You can configure it wget’s configuration files, using terminal commands, or by adding them bash configuration file. We will look at each of these approaches one by one.
1. Using Wget Configuration File
Here are the different configuration files used by wget, along with their priorities.
- ~/.wgetrc: User startup file.
- /etc/wgetrc: Default location of the global startup file.
- Set proxy variables in shell for current pseudo-terminal.
- ~/.bash_profile: User specific environment.
- /etc/profile: System wide environment.
wget will pick configuration values from whichever file has higher priority. For example, if you add configuration to /etc/wgetrc and not ~/.wgetrc then those values will be used by wget command.
In order to configure wget proxy, open its configuration file ~/.wgetrc or /etc/wgetrc in a text editor.
$ vi ~/.wgetrc OR $ vi /etc/wgetrc
Add the following lines to it. Replace Proxy_Server with the IP address or domain name of proxy server, and port with the port number of proxy server.
http_proxy = http://[Proxy_Server]:[port] https_proxy = http://[Proxy_Server]:[port] ftp_proxy = http://[Proxy_Server]:[port]
Save and close the file. Wget will start using the new proxy settings to download files. If it doesn’t work, then try creating a new session, or reboot the system to apply changes. These changes are permanent and remain even when you start a new session or reboot the system.
2. Using Terminal Commands
You can also set proxy server’s IP/domain and port using export command. Here are the commands to do it.
$ export http_proxy=http://[Proxy_Server]:[port] $ export https_proxy=$http_proxy $ export ftp_proxy=$http_proxy
In this case, please note, the changes will be applicable only as long as your session lasts. They will be lost when you terminal the session or start another session.
3. Using bash profile
Open ~/.bash_profile or /etc/profile in text editor.
$ vi ~/.bash_profile OR $ vi /etc/profile
Add the following lines to it.
export http_proxy=http://[Proxy_Server]:[port] export https_proxy=http://[Proxy_Server]:[port] export ftp_proxy=http://[Proxy_Server]:[port]
Save and close the file. Run the following command to reload bash profile settings.
$ source ~/.bashrc OR $ . ~/.bashrc
Please note, these changes are permanent and remain even after you start a new session or reboot your system, unless you remove them.
In this article, we have learnt how to configure proxy server for wget utility.
Also read:
How to get Hostname/Domain Name from IP in Linux
How to Check if String Matches Regular Expression
How to Modify XML File in Python
Plot Graph from CSV Data in Python
How to Encrypt Password in Python
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.