如何配置cURL使其默认始终使用SOCKS代理?
Got it, let's get this sorted so you don't have to type that tedious --socks5 flag every time you run cURL! There are two solid ways to set a default SOCKS proxy for cURL—one that's persistent (via a config file) and another that's flexible (via environment variables). Here's how each works:
This is the best option for permanent defaults, since it lives in a file and applies to all your cURL requests automatically.
- Linux/macOS: Create or edit the file
~/.curlrc(it might not exist yet, so just make it withtouch ~/.curlrcif needed). - Windows: Create or edit
%APPDATA%\curl\.curlrc(orC:\Users\<YourUsername>\.curlrcif that path doesn't exist). - Add this line to the file, replacing
<proxy name>and<proxy port>with your actual proxy details:
Example: If your proxy is running on localhost port 1080, it would besocks5 = socks5://<proxy name>:<proxy port>socks5 = socks5://localhost:1080. - Save the file, and that's it! Now any
curl <url>command you run will automatically use the SOCKS proxy.
Pro tip: If you ever need to bypass the proxy temporarily, just add the --noproxy "*" flag:
curl --noproxy "*" https://example.com
This is great if you want to set the proxy only for the current terminal session, or if you prefer not to edit config files.
Linux/macOS (Bash/Zsh)
- For a temporary session: Run this in your terminal:
export ALL_PROXY=socks5://<proxy name>:<proxy port> - For permanent global setup: Add the above line to your
~/.bashrc,~/.zshrc, or shell profile file, then runsource ~/.bashrc(or the corresponding file) to apply changes immediately.
Windows
- Command Prompt (CMD):
- Temporary: Run
set ALL_PROXY=socks5://<proxy name>:<proxy port> - Permanent: Go to This PC → Properties → Advanced System Settings → Environment Variables, add a new user variable named
ALL_PROXYwith the proxy address as its value.
- Temporary: Run
- PowerShell:
- Temporary: Run
$env:ALL_PROXY = "socks5://<proxy name>:<proxy port>" - Permanent: Edit your PowerShell profile with
notepad $PROFILE, add the line above, save, and restart PowerShell.
- Temporary: Run
Bonus: Proxy with Authentication
If your SOCKS proxy requires a username and password, use this format for either the config file or environment variable:
socks5://<username>:<password>@<proxy name>:<proxy port>
内容的提问来源于stack exchange,提问作者ssharma

