Developer Cheatsheet6 min readcURL & Bash

How to Use Proxies with cURL: HTTP, HTTPS, SOCKS5 & Auth

A practical, copy-paste guide on routing command-line HTTP requests through forward proxies using cURL. Covers basic authentication, SOCKS5 remote DNS resolution, persistent environment variables, and rotating residential proxies.

1. Basic cURL Proxy Command (-x or --proxy)

The primary flag for specifying a proxy in cURL is -x (or the long option --proxy). You provide the protocol, hostname, and port:

curl -x http://proxy.example.com:8080 https://api.ipify.org?format=json

2. Proxy Authentication (Username & Password)

Commercial proxy providers like 2Captcha Proxy require credential authentication. You can pass credentials in the proxy URI or via -U (--proxy-user):

Method A: Embedded in URL (Most Common)

curl -x http://user123:[email protected]:8080 https://api.ipify.org

Method B: Using -U Flag

curl -x http://proxy.2captcha.com:8080 -U "user123:pass456" https://api.ipify.org

3. SOCKS5 vs SOCKS5h (Preventing DNS Leaks)

When using SOCKS5 proxies, there is a critical distinction between socks5:// and socks5h://:

  • socks5://: Resolves hostnames on your local machine, then sends IP packets through the proxy. This leaks your real DNS server location and fails on geofenced internal domains.
  • socks5h://: Passes domain names directly to the remote proxy for DNS resolution (the h stands for host). Always use socks5h:// for web scraping and privacy.
curl -x socks5h://user123:[email protected]:1080 https://api.ipify.org

4. Setting Global Proxy Environment Variables

To route all terminal and script traffic through a proxy without passing -x on every command, set the standard environment variables in Bash or Zsh:

export http_proxy="http://user:[email protected]:8080"
export https_proxy="http://user:[email protected]:8080"
export all_proxy="socks5h://user:[email protected]:1080"

# Now cURL routes automatically:
curl https://api.ipify.org

# To bypass for local addresses:
export no_proxy="localhost,127.0.0.1,local.internal"

5. Rotating Residential Proxies via cURL

With 2Captcha rotating residential proxies, every cURL request automatically receives a fresh residential IP from a pool of 10M+ authentic home ISP connections:

# Request 1 (Routes through US residential IP)
curl -x http://customer-key:[email protected]:8080 https://api.ipify.org

# Request 2 (Fresh residential IP, Germany)
curl -x http://customer-key:[email protected]:8080 https://api.ipify.org

6. Recommended Proxy Service for CLI Automation

2Captcha Proxy for Developers

Rank #1 Top Pick

2Captcha Proxy offers instant API credentials for cURL, Python, and Node.js with native support for HTTP and SOCKS5h protocols.

  • Rate: $3.99 / GB Pay-As-You-Go (No monthly commitment)
  • Locations: 220+ Countries, City & ISP ASN filtering
  • Concurrency: Unlimited concurrent cURL requests without thread throttling
Get 2Captcha Proxy API Key ($3.99/GB) →