不同Shell下的代理设置

Why

鉴于日益严峻的网络形势,先写个文章记录一下各个 Shell 的代理设置,方便后续使用

(感觉不如…搞个 dot file repo)

Shell

*sh

不太清楚是不是所有发行版和 Shell 都支持all_proxy,一起写上总没错:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Replace with your own server config
export host_ip=<host ip>
export port=<port>

function proxyOn() {
export all_proxy=http://$host_ip:$port
export http_proxy=$all_proxy
export https_proxy=$all_proxy
echo -e "Proxies turned on"
}

function proxyOff(){
unset all_proxy http_proxy https_proxy
echo -e "Proxies turned off"
}

Powershell

Windows11 21H2 实测是跟随系统代理的

一些特殊情况

WSL2

WSL2 由于使用了虚拟机,所以会使用一个虚拟网卡与主机通信,并且 IP 会发生动态变化

可以通过查询 WSL2 主机 DNS 服务器的方法间接获得宿主机地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Replace with your own server config
export host_ip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
export port=1080

function proxyOn() {
export all_proxy=http://$host_ip:$port
export http_proxy=$all_proxy
export https_proxy=$all_proxy
echo -e "Proxies turned on"
}

function proxyOff(){
unset all_proxy http_proxy https_proxy
echo -e "Proxies turned off"
}

Git

你可以通过如下命令为 Git 设置代理

1
2
3
4
5
6
# This is managed by git, not shell.
git config --global http.proxy http://$host_ip:$port
git config --global https.proxy http://$host_ip:$port

git config --global --unset http.proxy
git config --global --unset https.proxy

这等效于编辑~/.gitconfig

1
2
3
4
[http]
proxy = http://$host_ip:$port
[https]
proxy = http://$host_ip:$port

快速验证代理

*sh

1
2
3
4
5
6
7
8
9
root@BakaFT-PC:~# curl cip.cc
IP : ****
地址 : 美国 美国

数据二 : 美国 | Microsoft数据中心

数据三 : 美国 | 微软

URL : http://www.cip.cc/****

Powershell

1
2
3
4
5
6
PS C:\Users\Administrator>curl api.ipify.org

StatusCode : 200
StatusDescription : OK
Content : **** // Your IP Address will be shown here
// ....

参考

WSL2 中访问宿主机 Windows 的代理 - ZingLix Blog

终端使用代理加速的正确方式(Shadowsocks) - SegmentFault 思否

Author

BakaFT

Posted on

2022-05-21

Updated on

2023-12-28

Licensed under

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×