个人头像ztm0929

如何为 Windows 终端启用代理

本文尚处于草稿状态,内容可能不完整或存在错误

很多小白打开了系统代理,却发现终端里的下载、安装、更新命令还是慢。原因通常不是网络本身,而是终端会话里没有持续生效的代理环境变量。本文会带你用最少步骤,把 Windows 下的终端代理配置成“开终端就可用”,让后续常见命令长期稳定加速。

我们的目标

运行少量命令,为终端成功注入代理,使 gitpip 等命令运行速度提升

开始之前

  • 拥有良好的网络环境(已运行 小猫咪
  • 已安装 Windows Terminal(终端)

动手操作

环境预检

Windows Terminal(终端)
$env:HTTP_PROXY; $env:HTTPS_PROXY;

注入环境变量

Windows Terminal(终端)
"http_proxy","https_proxy","HTTP_PROXY","HTTPS_PROXY" | % { [System.Environment]::SetEnvironmentVariable($_,"http://127.0.0.1:7897","User") }

确认注入成功

Windows Terminal(终端)
$env:HTTP_PROXY; $env:HTTPS_PROXY;

测试代理效果(可选)

当终端返回 http://127.0.0.1:7897 的结果时,说明已成功注入,我们可以测试一下代理效果,这一步骤不是必需的。

Windows Terminal(终端)
curl.exe ipinfo.io
{
  "ip": "xxx.xxx.xxx.xxx",
  "hostname": "xxx",
  "city": "Los Angeles",
  "region": "California",
  "country": "US",
  "loc": "xxx,xxx",
  "org": "AS25820 IT7 Networks Inc",
  "postal": "90012",
  "timezone": "America/Los_Angeles",
  "readme": "https://ipinfo.io/missingauth"
}
{
  "ip": "xxx.xxx.xxx.xxx",
  "city": "Shenzhen",
  "region": "Guangdong",
  "country": "CN",
  "loc": "xxx,xxx",
  "org": "AS4134 CHINANET-BACKBONE",
  "postal": "518000",
  "timezone": "Asia/Shanghai",
  "readme": "https://ipinfo.io/missingauth"
}

git clone https://github.com/ztm0929/ztm0929.cn.git

pip install requests

scoop install python

pnpm install next

常见问题

继续探索

完成目标之后,休息一下。我们可以尝试这些:

  • 为注入设置“启停”
  • 为 git、pip 等程序单独配置代理
  • 通过 Get-ChildItem Env: 命令,探索除了 HTTP(S)_PROXY 之外的 更多 Windows PowerShell 环境变量

最后更新于

目录