How to Use curl with SOCKS5 Proxy
This article explains how to use the curl command-line
utility to route network traffic through a SOCKS5 proxy. It covers the
essential command syntax, how to handle DNS resolution through the proxy
to prevent leaks, and how to authenticate with username and password
credentials.
The Basic SOCKS5 curl Command
To route a request through a SOCKS5 proxy, you can use the
-x or --proxy flag followed by the proxy
protocol, IP address, and port.
curl -x socks5://127.0.0.1:1080 https://example.comAlternatively, you can use the equivalent --socks5
command-line option:
curl --socks5 127.0.0.1:1080 https://example.comResolving DNS Through the Proxy (SOCKS5h)
By default, the standard SOCKS5 option resolves the destination’s
domain name (DNS) on your local machine before routing the traffic
through the proxy. To prevent DNS leaks and resolve the domain name
through the proxy server instead, use the socks5h://
protocol or the --socks5-hostname flag.
Using the -x flag:
curl -x socks5h://127.0.0.1:1080 https://example.comUsing the --socks5-hostname flag:
curl --socks5-hostname 127.0.0.1:1080 https://example.comThis is the recommended method for maintaining privacy.
SOCKS5 Proxy with Authentication
If your SOCKS5 proxy requires a username and password, you can pass
these credentials directly in the URL format or by using the
-U (or --proxy-user) flag.
Method 1: Inline Credentials
curl -x socks5h://username:password@127.0.0.1:1080 https://example.comMethod 2: Using the -U Flag
curl -U username:password -x socks5h://127.0.0.1:1080 https://example.com