欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 【CMD、PowerShell和Bash设置代理】

【CMD、PowerShell和Bash设置代理】

2025/2/24 16:14:43 来源:https://blog.csdn.net/AIFRZ/article/details/144327547  浏览:    关键词:【CMD、PowerShell和Bash设置代理】

【CMD、PowerShell和Bash设置代理】

    • 1. CMD(命令提示符)
      • 临时设置代理(只对当前会话有效):
      • 查看当前代理设置:
      • 清除临时代理设置:
      • 永久设置代理(对所有新的 CMD 会话有效):
      • 清除永久代理设置:
    • 2. PowerShell
      • 临时设置代理(只对当前 PowerShell 会话有效):
      • 查看当前代理设置:
      • 清除临时设置的代理:
      • 永久设置代理(对所有 PowerShell 会话有效):
      • 清除永久代理设置:
    • 3. Bash(Linux / macOS)
      • 临时设置代理(只对当前终端会话有效):
      • 查看当前代理设置:
      • 永久设置代理(对所有 Bash 会话有效):
      • 清除代理设置:
      • 永久清除代理设置:
    • 4. 测试代理
      • CMD测试代理
      • PowerShell测试代理
      • Bash测试代理
    • 5. 总结

1. CMD(命令提示符)

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

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

set http_proxy=http://127.0.0.1:10809
set https_proxy=http://127.0.0.1:10809

查看当前代理设置:

echo %http_proxy%
echo %https_proxy%

清除临时代理设置:

set http_proxy=
set https_proxy=

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

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

setx http_proxy "http://127.0.0.1:10809"
setx https_proxy "http://127.0.0.1:10809"

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

setx http_proxy "http://127.0.0.1:10809" /M
setx https_proxy "http://127.0.0.1:10809" /M

清除永久代理设置:

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

setx http_proxy ""
setx https_proxy ""

2. PowerShell

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

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

$env:http_proxy="http://127.0.0.1:10809"
$env:https_proxy="http://127.0.0.1:10809"

查看当前代理设置:

$env:http_proxy
$env:https_proxy

清除临时设置的代理:

$env:http_proxy=$null
$env:https_proxy=$null

永久设置代理(对所有 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)

清除永久代理设置:

[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 命令来配置的。

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

export http_proxy="http://127.0.0.1:10809"
export https_proxy="http://127.0.0.1:10809"

查看当前代理设置:

echo $http_proxy
echo $https_proxy

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

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

echo 'export http_proxy="http://127.0.0.1:10809"' >> ~/.bashrc
echo 'export https_proxy="http://127.0.0.1:10809"' >> ~/.bashrc
source ~/.bashrc

清除代理设置:

unset http_proxy
unset https_proxy

永久清除代理设置:

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


4. 测试代理

CMD测试代理

curl -I www.baidu.com
curl -I www.google.com

PowerShell测试代理

curl www.baidu.com
curl www.google.com

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 文件:永久设置代理,适用于所有终端会话。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词