终端代理设置

注意
只对当前终端有效, 新建终端需要重新设置。

格式:

1
set http_proxy=protocol://[proxy_userid]:[proxy_password]@proxy_ip:proxy_port

输入设置代理的命令:

HTTP代理:

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

或者

SOCKS5代理:

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

使用命令查看是否设置成功:

1
2
echo %http_proxy%
echo %https_proxy%

取消当前终端的代理设置:

1
2
set http_proxy=
set https_proxy=
  1. 新建一个cmd_init.cmd, 将上面相应的代理命令写入。
  2. 打开注册表, 在HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\下新建名为AutoRun字符串值, 数据的值是一个绝对路径, 路径指向刚才新建的cmd_init.cmd, 比如C:\Users\Username\cmd_init.cmd
  3. 此脚本会在每次打开cmd时被预加载。
  4. 修改文件权限, 禁用继承 (转化为显式权限) , 除SYSTEMAdministrators设为完全控制外, 其余用户均设为读取和执行
/2020-1/cmd.png
cmd_init.cmd

上面的方法一: 默认开启代理, 终端一打开就默认启用了代理, 如要关闭还需输入一长串命令。有一种更好的办法: 在cmd_init.cmd中不默认启用代理, 而是设置灵活的别名作为代理的开关:

1
2
DOSKEY proxy-on=set http_proxy=http://127.0.0.1:7890$tset https_proxy=http://127.0.0.1:7890
DOSKEY proxy-off=set http_proxy=$tset https_proxy=
注意
没错, $t的前后不需要有空格, 可以直接连接两条命令: command1$tcommand2。

日常使用时, 输入proxy-on即可设置代理, 输入proxy-off即可取消设置代理。

注意
只对当前终端有效, 新建终端需要重新设置。

格式:

1
$env:http_proxy="protocol://[proxy_userid]:[proxy_password]@proxy_ip:proxy_port"

输入设置代理的命令:

HTTP代理:

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

或者

SOCKS5代理:

1
2
$env:http_proxy="socks5://127.0.0.1:7890"
$env:https_proxy="socks5://127.0.0.1:7890"

使用命令查看是否设置成功:

1
2
$env:http_proxy
$env:https_proxy

取消当前终端的代理设置:

1
2
$env:http_proxy=""
$env:https_proxy=""

参考微软官方文档, 为所有用户、所有主机生成profile文件:

  1. 在PowerShell中运行以下命令:

    1
    
    if (!(Test-Path -Path $PROFILE.AllUsersAllHosts)) { New-Item -ItemType File -Path $PROFILE.AllUsersAllHosts -Force };
    
    其他级别
    • Current User, Current Host - $PROFILE
    • Current User, Current Host - $PROFILE.CurrentUserCurrentHost
    • Current User, All Hosts - $PROFILE.CurrentUserAllHosts
    • All Users, Current Host - $PROFILE.AllUsersCurrentHost
    • All Users, All Hosts - $PROFILE.AllUsersAllHosts
  2. 使用notepad $PROFILE.AllUsersAllHosts命令调用记事本打开profile文件, 在文件中写入上面相应的代理命令, 保存关闭即可, 该文件会在每次打开PowerShell时被预加载。

  3. 如果是Current User, Current Host级别, 则profile文件为此电脑/文档/WindowsPowerShell/Microsoft.PowerShell_profile.ps1

  4. 如果是All Users, All Hosts级别, 则profile文件为C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

  5. 如果在PowerShell 7生成profile文件, 则一律为C:\Program Files\PowerShell\7\profile.ps1

  6. 修改文件权限, 禁用继承 (转化为显式权限) , 除SYSTEMAdministrators设为完全控制外, 其余用户均设为读取和执行

/2020-1/powershell.png
Microsoft.PowerShell_profile.ps1

参考微软官方文档Set-ExecutionPolicy, 以管理员身份运行PowerShell, 运行以下命令:

1
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

上面的方法一: 默认开启代理, 终端一打开就默认启用了代理, 如要关闭还需输入一长串命令。有一种更好的办法: 在profile文件中不默认启用代理, 而是设置灵活的function作为代理的开关:

1
2
3
4
5
6
7
8
9
function proxy-on {
    $env:http_proxy="http://127.0.0.1:7890"
    $env:https_proxy="http://127.0.0.1:7890"
}

function proxy-off {
    $env:http_proxy=""
    $env:https_proxy=""
}

日常使用时, 输入proxy-on即可设置代理, 输入proxy-off即可取消设置代理。

注意
只对当前终端有效, 新建终端需要重新设置。

输入设置代理的命令:

格式:

1
export http_proxy=protocol://[proxy_userid]:[proxy_password]@proxy_ip:proxy_port

HTTP代理:

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

SOCKS5代理:

1
2
export http_proxy=socks5://127.0.0.1:7890
export https_proxy=socks5://127.0.0.1:7890

设置终端中的wgetcurl等软件都走SOCKS5代理:

1
export ALL_PROXY=socks5://127.0.0.1:7890

取消当前终端的代理设置:

1
2
unset http_proxy https_proxy
unset ALL_RPOXY
环境变量配置文件

/etc/envirnomentshell无关, 所以无法使用脚本或通配符展开。此文件仅接受variable=value键值对格式。

其他文件的加载顺序: /etc/profile>/etc/bash.bashrc>~/.profile=~/.bash_profile>~/.bashrc

  1. 将上面的临时代理命令写入环境变量配置文件中。
  2. 运行source /etc/bash.bashrcsource ~/.bashrc以应用新的环境变量。
  3. 这样当前终端和新建的终端就会使用代理。
  1. 通过设置alias来简化操作。将以下命令写入环境变量配置文件中。

    1
    2
    
    alias proxy-on="export http_proxy=http://127.0.0.1:7890;export https_proxy=http://127.0.0.1:7890"
    alias proxy-off="unset http_proxy https_proxy"
    
  2. 运行source /etc/bash.bashrcsource ~/.bashrc以应用新的环境变量。

  3. 每次要使用代理就输入proxy-on, 不用了就输入proxy-off

  1. 通过设置function来简化操作。将以下命令写入环境变量配置文件中。

    1
    2
    3
    4
    5
    6
    7
    8
    
    function proxy-on() {
        export http_proxy=http://127.0.0.1:7890
        export https_proxy=http://127.0.0.1:7890
    }
    
    function proxy-off() {
        unset http_proxy https_proxy
    }
    
  2. 运行source /etc/bash.bashrcsource ~/.bashrc以应用新的环境变量。

  3. 每次要使用代理就输入proxy-on, 不用了就输入proxy-off

注意
使用--global参数即全局永久生效, 不使用则对当前仓库永久生效, 相当于--local, 关于配置级别, 参考Git Config

格式:

1
git config --global http.proxy protocol://[proxy_userid]:[proxy_password]@proxy_ip:proxy_port

HTTP代理:

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

SOCKS5代理:

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

使用git config --global --list命令查看是否设置成功。

使用上文中作用于cmd, PowerShell, bash, zsh等shell的方案, 在shell中使用git命令, git也会走代理, 这样更灵活。