在 windows powershll

在 Ubuntu 下通过 bash 设置代理,只需配置环境变量 http_proxyhttps_proxy 即可。以下是详细方法,适用于 命令行工具(如 curl、apt、git 等)走代理:

一次性临时设置(当前终端有效)

1
2
export http_proxy="http://127.0.0.1:7890"
export https_proxy="http://127.0.0.1:7890"

如果你使用的是 SOCKS5 代理:

1
export all_proxy="socks5h://127.0.0.1:7890"

⚠️ 注意 socks5h:// 可以解析域名,推荐使用。

验证代理是否生效:

1
curl https://www.google.com

永久生效设置(添加到 .bashrc 或 .zshrc)

打开配置文件:

1
nano ~/.bashrc

在末尾添加:

1
2
3
export http_proxy="http://127.0.0.1:7890"
export https_proxy="http://127.0.0.1:7890"
export all_proxy="socks5h://127.0.0.1:7890"

保存并使其生效:

1
source ~/.bashrc

特殊情况:Git 设置代理(全局)

1
2
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

要取消代理:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

windows 终端设置代理

在 Windows 终端(CMD 或 PowerShell) 中设置代理,主要是通过设置环境变量 http_proxyhttps_proxy。下面是详细方法,适用于 curl、git、npm 等工具通过 HTTP 或 SOCKS 代理访问外网。

方法一:临时设置(当前窗口有效)

CMD 中:

1
2
set http_proxy=http://127.0.0.1:7890
set https_proxy=http://127.0.0.1:7890

或者使用 SOCKS5(注意 Git 不支持 socks,需其他工具支持):

1
set all_proxy=socks5h://127.0.0.1:7890

PowerShell 中:

1
2
3
$env:http_proxy = "http://127.0.0.1:7890"
$env:https_proxy = "http://127.0.0.1:7890"
$env:all_proxy = "socks5h://127.0.0.1:7890"

这些设置只在当前窗口有效,关闭后会失效。

方法二:永久设置(系统环境变量)

打开「系统设置 > 环境变量」

在“用户变量”或“系统变量”中添加以下变量:

应用并重新打开终端

Git 专用代理设置(推荐独立配置)

1
2
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

取消代理:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

检查代理是否生效

1
curl https://google.com

或者用 git clone 测试 Git 访问 GitHub 是否变快。