Ora

How do I set proxy credentials in cURL?

Published in cURL Proxy Configuration 4 mins read

You can set proxy credentials in cURL using command-line flags, environment variables, or a cURL configuration file, each offering different levels of convenience and security.

Using Command-Line Flags

This method is generally recommended for its explicit control and can be more secure as it avoids persisting credentials in environment variables or configuration files.

Basic Proxy Authentication

You can specify the proxy server and its credentials directly using the -x (or --proxy) and -U (or --proxy-user) flags.

curl -x http://your_proxy_ip:port -U username:password http://example.com/resource

Alternatively, you can provide the username and password with the proxy URL:

curl -x http://username:password@your_proxy_ip:port http://example.com/resource

If you provide only the username with -U username:, cURL will securely prompt you for the password. This is often the safest approach when running interactive commands.

curl -x http://your_proxy_ip:port -U username: http://example.com/resource

Specifying Proxy Type

cURL supports various proxy types (HTTP, HTTPS, SOCKS). You can indicate the type by prefixing the proxy address:

  • HTTP Proxy: http://your_proxy_ip:port (default if no prefix)
  • HTTPS Proxy: https://your_proxy_ip:port
  • SOCKS Proxy: socks5://your_proxy_ip:port or socks5h://your_proxy_ip:port (for hostname resolution via proxy)

Example with SOCKS5 proxy:

curl -x socks5://username:password@your_proxy_ip:port http://example.com/resource
Flag Description Example Value
-x, --proxy Specifies the proxy host and port. http://proxy.example.com:8080
-U, --proxy-user Sets the proxy username and password (or prompts). myuser:mypassword

Security Note: While convenient, providing passwords directly on the command line can expose them in shell history or process listings (ps aux). Use this method cautiously, especially in shared environments.

Using Environment Variables

For repeated use, especially in scripts or when you want all cURL commands (and other tools that respect these variables) to use a specific proxy, setting environment variables is a practical approach.

To use cURL proxy environment variables, set the proxy server URL, username, and password as environment variables using the export command in your Terminal. Replace <YOUR_USERNAME>, <YOUR_PASSWORD>, and <YOUR_PROXY_IP:PORT> with the appropriate values for your proxy server.

# For HTTP traffic
export HTTP_PROXY="http://username:password@your_proxy_ip:port"
# For HTTPS traffic
export HTTPS_PROXY="http://username:password@your_proxy_ip:port"
# For all traffic (can be overridden by HTTP_PROXY/HTTPS_PROXY)
export ALL_PROXY="socks5://username:password@your_proxy_ip:port"

# You can also use lowercase versions (http_proxy, https_proxy, all_proxy)
# Some systems prefer lowercase, others honor uppercase. It's often good practice to set both.

# Example:
export HTTP_PROXY="http://myuser:[email protected]:8080"
export HTTPS_PROXY="http://myuser:[email protected]:8080"

# Now, any cURL command will use these proxy settings
curl http://example.com/data

# To unset the variables when no longer needed:
unset HTTP_PROXY
unset HTTPS_PROXY

Security Note: Environment variables can be inherited by child processes and might persist in your shell history or environment. Ensure you unset them if they contain sensitive information and are not needed permanently.

Using a cURL Configuration File

For a more permanent and consistent setup across sessions, you can create a ~/.curlrc file in your home directory. cURL automatically reads this file for default options.

Create or edit ~/.curlrc and add the following:

# Example for HTTP proxy with authentication
proxy = "http://your_proxy_ip:port"
proxy-user = "username:password"

# Alternatively, you can embed credentials directly into the proxy URL:
# proxy = "http://username:password@your_proxy_ip:port"

# For a SOCKS5 proxy with authentication:
# proxy = "socks5://your_proxy_ip:port"
# proxy-user = "username:password"

After saving the file, any subsequent curl commands will use these settings without needing explicit flags.

Security Note: Storing credentials in ~/.curlrc means they are written to a plain text file. It is crucial to set appropriate file permissions to restrict access, for example:

chmod 600 ~/.curlrc

This makes the file readable and writable only by the owner.

Best Practices and Security Considerations

  • Avoid hardcoding passwords: Especially in scripts that might be shared or committed to version control.
  • Prompt for passwords: For interactive use, -U username: allows cURL to prompt for the password securely, preventing it from appearing in shell history.
  • Use configuration files with care: If using ~/.curlrc, ensure strict file permissions (chmod 600 ~/.curlrc) to protect credentials.
  • Understand proxy types: Be aware of the differences between HTTP, HTTPS, and SOCKS proxies and choose the appropriate type for your needs.
  • Official Documentation: For the most detailed and up-to-date information, always refer to the cURL man page.