Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Set Up cURL to Permanently Use a Proxy on Linux
cURL is a command-line tool used to request data from servers and transfer it to devices. The user specifies the server URL and any data to be sent. cURL works on Windows, macOS, and Linux, supporting over 25 protocols including SFTP, FTP, HTTPS, and HTTP. It is one of the best open-source tools for API calls and debugging network requests.
A proxy server acts as an intermediary between users and websites, transferring traffic while providing security and functionality layers. When using a proxy, internet requests first go to the proxy server, which evaluates them against predefined rules before forwarding to the destination. Server responses follow the same path back through the proxy to the end user.
Set Up cURL to Permanently Use a Proxy on Linux
There are several methods to configure cURL to use a proxy permanently on Linux systems. Each method has its own advantages depending on your specific use case.
Method 1: Command Line Arguments (Temporary)
You can use proxy settings with command-line arguments. To view all proxy configuration options in cURL's help documentation:
curl --help proxy
This displays various commands, including:
-x, --proxy [protocol://]host[:port]
You can supply proxy details using either -x or --proxy:
curl -x "http://proxy.example.com:8080" "https://httpbin.org/ip"
Or:
curl --proxy "http://proxy.example.com:8080" "https://httpbin.org/ip"
Method 2: Environment Variables
cURL supports environment variables for each protocol scheme. Set the proxy using the export command:
export http_proxy="http://proxy.example.com:8080" export https_proxy="http://proxy.example.com:8080"
After exporting these variables, cURL automatically uses the proxy for remote connections:
curl "http://httpbin.org/ip"
To make this permanent, add the variables to your shell profile (e.g., ~/.bashrc or ~/.profile):
echo 'export http_proxy="http://proxy.example.com:8080"' >> ~/.bashrc echo 'export https_proxy="http://proxy.example.com:8080"' >> ~/.bashrc
To disable the global proxy, unset these variables:
unset http_proxy unset https_proxy
Method 3: Shell Alias
Create an alias to replace the default cURL command with a proxy-enabled version:
alias curl="curl -x http://proxy.example.com:8080"
Add this alias to your shell configuration file to make it persistent:
echo 'alias curl="curl -x http://proxy.example.com:8080"' >> ~/.bashrc
After reloading your shell, cURL will automatically use the proxy:
curl "https://httpbin.org/ip"
Method 4: Configuration File (.curlrc)
The most recommended approach is creating a cURL configuration file. This file contains configuration parameters that cURL loads automatically on startup.
Create or edit the ~/.curlrc file and add:
proxy = "http://proxy.example.com:8080"
You can also add additional options:
proxy = "http://proxy.example.com:8080" user-agent = "Custom-cURL-Agent/1.0" max-time = 30
Comparison of Methods
| Method | Persistence | Scope | Flexibility |
|---|---|---|---|
| Command Line | Temporary | Single command | High |
| Environment Variables | Session/Permanent | All applications | Medium |
| Shell Alias | Permanent | cURL only | Medium |
| .curlrc File | Permanent | cURL only | High |
Additional Options
For SSL certificate issues, use the -k option to ignore certificate errors:
curl -x "http://proxy.example.com:8080" -k "https://example.com"
For authenticated proxies, include credentials in the URL:
proxy = "http://username:password@proxy.example.com:8080"
Conclusion
Setting up cURL to permanently use a proxy on Linux can be accomplished through multiple methods. The .curlrc configuration file method is recommended as it provides the best balance of persistence, scope control, and flexibility. Environment variables work well for system-wide proxy usage, while aliases offer a middle ground for cURL-specific configurations.
