【CMD、PowerShell和Bash设置代理】

【CMD、PowerShell和Bash设置代理】

1. CMD(命令提示符)

在 Windows 的命令提示符(CMD)中设置代理,可以使用 set 命令临时设置,或者使用 setx 命令永久设置环境变量。

临时设置代理(只对当前会话有效):

cmd 复制代码
set http_proxy=http://127.0.0.1:10809
set https_proxy=http://127.0.0.1:10809

查看当前代理设置:

cmd 复制代码
echo %http_proxy%
echo %https_proxy%

清除临时代理设置:

cmd 复制代码
set http_proxy=
set https_proxy=

永久设置代理(对所有新的 CMD 会话有效):

使用 setx 命令可以将代理设置保存到系统或用户环境变量中,使其对所有新的 CMD 会话有效。

cmd 复制代码
setx http_proxy "http://127.0.0.1:10809"
setx https_proxy "http://127.0.0.1:10809"

如果需要将其设置为 系统级别 环境变量(适用于所有用户),可以加上 /M 参数(需要管理员权限):

cmd 复制代码
setx http_proxy "http://127.0.0.1:10809" /M
setx https_proxy "http://127.0.0.1:10809" /M

清除永久代理设置:

要清除永久代理设置,可以使用 setx 命令将变量值设为空:

cmd 复制代码
setx http_proxy ""
setx https_proxy ""

2. PowerShell

在 PowerShell 中,设置代理通过 $env 来设置临时变量,或者通过 [System.Environment]::SetEnvironmentVariable 来设置永久变量。

临时设置代理(只对当前 PowerShell 会话有效):

powershell 复制代码
$env:http_proxy="http://127.0.0.1:10809"
$env:https_proxy="http://127.0.0.1:10809"

查看当前代理设置:

powershell 复制代码
$env:http_proxy
$env:https_proxy

清除临时设置的代理:

powershell 复制代码
$env:http_proxy=$null
$env:https_proxy=$null

永久设置代理(对所有 PowerShell 会话有效):

powershell 复制代码
[System.Environment]::SetEnvironmentVariable("http_proxy", "http://127.0.0.1:10809", [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("https_proxy", "http://127.0.0.1:10809", [System.EnvironmentVariableTarget]::User)

清除永久代理设置:

powershell 复制代码
[System.Environment]::SetEnvironmentVariable("http_proxy", $null, [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("https_proxy", $null, [System.EnvironmentVariableTarget]::User)

3. Bash(Linux / macOS)

在 Linux 或 macOS 的 Bash 中,设置代理使用 export 命令来配置的。

临时设置代理(只对当前终端会话有效):

bash 复制代码
export http_proxy="http://127.0.0.1:10809"
export https_proxy="http://127.0.0.1:10809"

查看当前代理设置:

bash 复制代码
echo $http_proxy
echo $https_proxy

永久设置代理(对所有 Bash 会话有效):

将 export 命令添加到用户的 ~/.bashrc 或 ~/.bash_profile 文件中,这样每次打开终端时都会自动设置代理。

bash 复制代码
echo 'export http_proxy="http://127.0.0.1:10809"' >> ~/.bashrc
echo 'export https_proxy="http://127.0.0.1:10809"' >> ~/.bashrc
source ~/.bashrc

清除代理设置:

bash 复制代码
unset http_proxy
unset https_proxy

永久清除代理设置:

从 ~/.bashrc 或 ~/.bash_profile 文件中删除设置的代理行,或者将其注释掉。


4. 测试代理

CMD测试代理

bash 复制代码
curl -I www.baidu.com
curl -I www.google.com

PowerShell测试代理

bash 复制代码
curl www.baidu.com
curl www.google.com

Bash测试代理

bash 复制代码
curl -I www.baidu.com
curl -I www.google.com

5. 总结

  • CMD(命令提示符) :
    • set 命令:临时设置代理,仅对当前会话有效。
    • setx 命令:永久设置代理,适用于所有新的 CMD 会话。
  • PowerShell :
    • $env 变量:临时设置代理,仅对当前会话有效。
    • [System.Environment]::SetEnvironmentVariable:永久设置代理,适用于所有 PowerShell 会话。
  • Bash(Linux / macOS) :
    • export 命令:临时设置代理,仅对当前终端会话有效。
    • ~/.bashrc 或 ~/.bash_profile 文件:永久设置代理,适用于所有终端会话。
相关推荐
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
LaughingZhu3 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
高洁013 天前
未来三年,AI 落地的五个确定性判断一
人工智能·深度学习·机器学习·transformer·知识图谱
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
lpfasd1233 天前
2026年第38周GitHub趋势周报
python·科技·github
霍霍的袁3 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超3 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
u1301303 天前
GitHub 热榜项目:日榜(2026-09-21)
人工智能·github