常用代理设置命令
2025年2月27日大约 4 分钟
全面代理设置命令指南
终端环境代理设置
Linux/MacOS 终端代理设置
设置代理
# HTTP/HTTPS 代理
export http_proxy=http://127.0.0.1:7897
export https_proxy=http://127.0.0.1:7897
export HTTP_PROXY=http://127.0.0.1:7897
export HTTPS_PROXY=http://127.0.0.1:7897
# Socks5 代理
export all_proxy=socks5://127.0.0.1:7897
export ALL_PROXY=socks5://127.0.0.1:7897
# 如果要持久化,可以将上述命令添加到 ~/.bashrc 或 ~/.zshrc
取消代理
unset http_proxy
unset https_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
unset all_proxy
unset ALL_PROXY
Windows CMD 代理设置
设置代理
set http_proxy=http://127.0.0.1:7897
set https_proxy=http://127.0.0.1:7897
set HTTP_PROXY=http://127.0.0.1:7897
set HTTPS_PROXY=http://127.0.0.1:7897
取消代理
set http_proxy=
set https_proxy=
set HTTP_PROXY=
set HTTPS_PROXY=
PowerShell 代理设置
设置代理
$env:http_proxy="http://127.0.0.1:7897"
$env:https_proxy="http://127.0.0.1:7897"
$env:HTTP_PROXY="http://127.0.0.1:7897"
$env:HTTPS_PROXY="http://127.0.0.1:7897"
# 添加到 PowerShell 配置文件使其持久化
# 在 $PROFILE 文件中添加上述命令
取消代理
Remove-Item Env:http_proxy
Remove-Item Env:https_proxy
Remove-Item Env:HTTP_PROXY
Remove-Item Env:HTTPS_PROXY
开发工具代理设置
Git 代理设置
设置代理
# HTTP/HTTPS 代理
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy http://127.0.0.1:7897
# Socks5 代理
git config --global http.proxy socks5://127.0.0.1:7897
git config --global https.proxy socks5://127.0.0.1:7897
# 只对 GitHub 设置代理
git config --global http.https://github.com.proxy http://127.0.0.1:7897
取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset http.https://github.com.proxy
NPM 代理设置
设置代理
# HTTP/HTTPS 代理
npm config set proxy http://127.0.0.1:7897
npm config set https-proxy http://127.0.0.1:7897
# 设置源
npm config set registry https://registry.npmjs.org/
取消代理
npm config delete proxy
npm config delete https-proxy
Yarn 代理设置
设置代理
yarn config set proxy http://127.0.0.1:7897
yarn config set https-proxy http://127.0.0.1:7897
取消代理
yarn config delete proxy
yarn config delete https-proxy
Maven 代理设置
设置代理
在 ~/.m2/settings.xml
中添加:
<settings>
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<host>127.0.0.1</host>
<port>7897</port>
</proxy>
</proxies>
</settings>
取消代理
删除或注释掉上述配置。
Gradle 代理设置
设置代理
在 ~/.gradle/gradle.properties
中添加:
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=7897
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=7897
取消代理
删除或注释掉上述配置。
Python 相关代理设置
pip 代理设置
设置代理
# 临时使用
pip install --proxy http://127.0.0.1:7897 [package_name]
# 永久设置
pip config set global.proxy http://127.0.0.1:7897
# 或在 pip.conf(Linux/MacOS)或 pip.ini(Windows)中添加:
[global]
proxy = http://127.0.0.1:7897
取消代理
pip config unset global.proxy
Conda 代理设置
设置代理
conda config --set proxy_servers.http http://127.0.0.1:7897
conda config --set proxy_servers.https http://127.0.0.1:7897
取消代理
conda config --remove-key proxy_servers.http
conda config --remove-key proxy_servers.https
容器相关代理设置
Docker 代理设置
设置代理
- Docker 守护进程代理(Linux): 创建或编辑
/etc/systemd/system/docker.service.d/http-proxy.conf
:
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7897"
Environment="HTTPS_PROXY=http://127.0.0.1:7897"
Environment="NO_PROXY=localhost,127.0.0.1"
- Docker 客户端代理: 在
~/.docker/config.json
中添加:
{
"proxies": {
"default": {
"httpProxy": "http://127.0.0.1:7897",
"httpsProxy": "http://127.0.0.1:7897",
"noProxy": "localhost,127.0.0.1"
}
}
}
取消代理
- 删除 http-proxy.conf 文件
- 删除 config.json 中的 proxies 配置
Kubernetes 代理设置
设置代理
在 Pod 配置中添加环境变量:
env:
- name: HTTP_PROXY
value: "http://127.0.0.1:7897"
- name: HTTPS_PROXY
value: "http://127.0.0.1:7897"
- name: NO_PROXY
value: "localhost,127.0.0.1"
取消代理
删除相关环境变量配置。
其他工具代理设置
Wget 代理设置
设置代理
# 临时使用
wget -e "http_proxy=http://127.0.0.1:7897" -e "https_proxy=http://127.0.0.1:7897" [URL]
# 永久设置,在 ~/.wgetrc 中添加:
http_proxy=http://127.0.0.1:7897
https_proxy=http://127.0.0.1:7897
取消代理
删除 .wgetrc 中的配置或不使用代理参数。
Curl 代理设置
设置代理
# 临时使用
curl -x http://127.0.0.1:7897 [URL]
curl --proxy http://127.0.0.1:7897 [URL]
# 永久设置,在 ~/.curlrc 中添加:
proxy=http://127.0.0.1:7897
取消代理
删除 .curlrc 中的配置或不使用代理参数。
Apt (Ubuntu/Debian) 代理设置
设置代理
创建或编辑 /etc/apt/apt.conf.d/proxy.conf
:
Acquire::http::Proxy "http://127.0.0.1:7897";
Acquire::https::Proxy "http://127.0.0.1:7897";
取消代理
删除 proxy.conf 文件或注释掉配置。
Yum (CentOS/RHEL) 代理设置
设置代理
编辑 /etc/yum.conf
:
proxy=http://127.0.0.1:7897
取消代理
删除或注释掉相关配置。
Ruby Gems 代理设置
设置代理
# 临时使用
gem install --http-proxy http://127.0.0.1:7897 [gem_name]
# 永久设置
gem sources --add https://rubygems.org/ --http-proxy http://127.0.0.1:7897
取消代理
gem sources --remove-all --add https://rubygems.org/
Composer (PHP) 代理设置
设置代理
composer config --global http-basic.proxy.domain username password
composer config --global github-protocols https
取消代理
composer config --global --unset http-basic.proxy.domain