Set Up cURL to Permanently Use a Proxy on Linux


cURL is used to request data from a server and send it to a device. Through this command, the user specifies the server URL and the data it needs to send the URL (the location to which the request is sent). Curl works on Windows, MacOS, and Linux and supports over 25+ protocols, including SFTP, FTP, HTTPS, and HTTP. Regarding API calls and debugging network requests, curl is one of the best open-source and free tools.

The word 'Proxy' stands for 'act on behalf of another.' Similarly, the proxy server also acts on behalf of the end user.

Proxy servers act as intermediate servers between websites and users to transfer traffic. Proxies isolate the websites that end-user clients browse and provide multiple levels of security and functionality. When using a proxy server, all requests on the internet first go to this server, which it evaluates.

Applying a set of rules on these, it forwards the request to the internet as and when required. Similarly, server responses are returned to the first proxy, which processes and evaluates these and only sends them to the end user. Sometimes proxy servers provide greater anonymity on the internet by using a series of servers.

Set Up cURL to Permanently Use a Proxy on Linux

We'll look at several ways to permanently use curl to set up proxies.

Through Command Line Argument (Temporary solution)

You can use a proxy using simple command-line arguments. You can filter through all proxy configuration options in Curl's help documentation using the command line. You can view the documentation for proxy settings by using the following command −

curl --help proxy

On running the above command, you will get a list of commands, one of which will be −

-x, --proxy [protocol://]host[:port] 

The 'x' in the above command is case-sensitive. With curl, you can supply proxy details using either '-x' or '--proxy.'

curl -x "[protocol://]host[:port]" 

Or,

curl --proxy "[protocol://]host[:port]" 

Note − Double quotes surround the target URL and the proxy URL.

Through Environmental Variables

Curl supports an environment variable setting [scheme]_proxy for each protocol. Using the http_proxy (environmental variable), you can set the curl to access a proxy server. As a result, you must set this variable with the export command −

export http_proxy="[protocol://][host][:port]" (access the address that used http protocol)
export https_proxy="[protocol://][host][:port]" (access the address that used https protocol)

Note − If you notice the SSL certificate errors, you can ignore them by adding the -k option to the above command.

curl -x "[protocol://][host][:port]" -k [URL]

After exporting the environment variables, the curl command automatically uses the above proxy during remote connections.

curl "http://httpbin.org/ip"

Bonus Tip − You can make it permanent by adding this variable to the shell profile. Since our default shell is bash, so here we will add it to '~/.profile' as follows −

http_proxy=http://127.0.0.1:8080

These variables apply to the entire system. If this behavior is not desired, we recommend you to turn off the global proxy by unsetting these variables −

unset http_proxy
unset https_proxy

Through Alias

Using an alias, you can execute any system command with another. Replace the curl call with a proxy command in this way. There may be differences in the shell file you use with environment variables depending on your operating system.

alias curl="curl -x "[protocol://][host][:port]"

Upon reloading the Shell, running curl uses the proxy server by invoking our alias.

curl https://xyz.com

Through .curlrc

You need to pass the proxy address to curl every time. To avoid the issue, you can create a .curlrc (curl configuration file) which contains many configuration parameters. It can be stored in your home directory. When you start the curl command, it looks for the ~/.curlrc file and loads the options from all the configuration files present in the file.

We can add the below line in ~/.curlrc to use the proxy server permanently.

proxy = "[protocol://][host][:port]"

Note − If the ~/.curlrc file doesn't exist, you can create and add it.

Conclusion

In this article, we explained how to set up cURL to permanently use a proxy on Linux and send all requests to curl through a proxy server. Here we looked at several permanent solutions to configure proxy with curl. Out of which curl configuration file is recommended. Furthermore, you can also access the required data through the other approaches.

Updated on: 18-May-2023

724 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements