【CMD、PowerShell和Bash设置代理】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ztiddler

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值