A proxy server mediates requests, potentially bypassing filters, enhancing privacy, or circumventing regional restrictions. HTTP proxies handle standard web traffic, while SOCKS proxies offer more flexible tunneling options.
By specifying the proxy server and port, cURL routes traffic through this intermediary, enabling scenarios like testing restricted endpoints, simulating various network conditions, or masking direct connections.
Managing proxy usage in cURL ensures controlled and secure data transfers. Environment variables, configuration files, or command-line parameters customize proxy settings for consistent and convenient application.
Steps to use HTTP and SOCKS proxies in cURL:
- Open a terminal.
- Set an HTTP proxy using --proxy.
$ curl --proxy "http://proxy-server:port" "https://www.example.com/"
Replace proxy-server and port with actual proxy details.
- Use a SOCKS5 proxy with --socks5-hostname.
$ curl --socks5-hostname "socks5-server:port" "https://www.example.com/"
SOCKS5 proxies can handle DNS resolution through the proxy.
- Prevent DNS resolution through the proxy using --socks5 instead.
$ curl --socks5 "socks5-server:port" "https://www.example.com/"
--socks5 resolves DNS locally, not through the proxy.
- Use a SOCKS4 proxy if needed.
$ curl --socks4 "socks4-server:port" "https://www.example.com/"
SOCKS4 does not handle DNS, so queries are resolved on the client side.
- Provide authentication credentials to the proxy.
$ curl --proxy "http://proxy-server:port" --proxy-user "username:password" "https://www.example.com/" $ curl --socks5 "socks5-server:port" --proxy-user "username:password" "https://www.example.com/"
Replace username and password with valid credentials.
- Set environment variables for persistent proxy usage.
$ export http_proxy="http://proxy-server:port" $ export https_proxy="http://proxy-server:port" $ curl "https://www.example.com/"
All cURL requests in this session use the proxy unless overridden.
- Configure a proxy in ~/.curlrc for all requests.
$ echo "proxy = http://proxy-server:port" >> ~/.curlrc
This applies the proxy to every cURL command run by this user.
- Test the proxy settings by making a request.
$ curl "https://www.example.com/"
Check responses or headers to confirm that requests route through the proxy.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.