Set Up a Script in WSL2 or Linux to Easily Enable Command Line Proxy

Set Up a Script in WSL2 or Linux to Easily Enable Command Line Proxy

Prerequisites

A proxy server that supports local network sharing.

Main Content

The command line in WSL2 cannot directly use the system proxy, or on some Linux servers, we may want to use a proxy in the current shell. In such cases, you can achieve proxy functionality through commands:

1
2
3
4
5
6
7
8
export http_proxy="${地址}"
export HTTP_PROXY="${地址}"

export https_proxy="${地址}"
export HTTPS_proxy="${地址}"

export ALL_PROXY="${地址}"
export all_proxy="${地址}"

Additionally, git can also be configured to use a proxy address:

1
2
git config --global http.https://github.com.proxy ${地址}
git config --global https.https://github.com.proxy ${地址}

Manually setting this can be cumbersome, so it is integrated into the script.

1
2
# touch a new file
vim yourPath/proxy.sh

For WSL2 Versions Before 2.0

Write the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
wslip=$(hostname -I | awk '{print $1}')
port=yourPort
testurl=yourUrl

PROXY_HTTP="http://${hostip}:${port}"
#PROXY_SOCKS5="socks5://${hostip}:${port}"
#PROXY_SOCKS5="socks5h://${hostip}:${port}"

set_proxy(){
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

export ALL_PROXY="${PROXY_SOCKS5}"
export all_proxy="${PROXY_SOCKS5}"

git config --global http.https://github.com.proxy ${PROXY_HTTP}
git config --global https.https://github.com.proxy ${PROXY_HTTP}

echo "Proxy has been opened."
}

unset_proxy(){
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ALL_PROXY
unset all_proxy
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

echo "Proxy has been closed."
}

test_setting(){
echo "Host IP:" ${hostip}
echo "WSL IP:" ${wslip}
echo "Try to connect to testUrl..."
resp=$(curl -I -s --connect-timeout 5 -m 5 -w "%{http_code}" -o /dev/null ${testurl})
if [ ${resp} = 200 ]; then
echo "Proxy setup succeeded!"
else
echo "Proxy setup failed!"
fi
}

if [ "$1" = "set" ]
then
set_proxy

elif [ "$1" = "unset" ]
then
unset_proxy

elif [ "$1" = "test" ]
then
test_setting
else
echo "Unsupported arguments."
fi

To explain, the first line is mainly used to obtain the host IP. Because for some reason, the proxy IP of the host’s WLAN sharing changes every time WSL starts, this command is used to get the current IP.

1
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')

Note!!! Here you need to set the port number for proxy sharing, i.e., the port variable in the script, and set the website URL to test the connection, i.e., the testurl variable in the script.

About SOCKS Comments

Here we explain the commented-out SOCKS5 proxy. For SOCKS proxies, you need to know which version of SOCKS your proxy server supports: whether it’s SOCKS5, SOCKS4, or SOCKS5h. The following are the corresponding relationships:

1
2
3
4
5
6
7
8
# socks4
socks://{ip}:{port}

# socks5
socks5://{ip}:{port}

# socks5h
socks5h://{ip}:{port}

In bash, if it is commented out, it means that the SOCKS proxy variable is not set. There is no need to specifically comment out the all_proxy variable afterward; it will be set as an empty string. By default, it is commented out because in most cases, an HTTP proxy is sufficient. If you need a SOCKS proxy, please uncomment the corresponding SOCKS version after determining which SOCKS version your proxy server supports.

For WSL2 Versions 2.0 and Later, and General Linux

For WSL

Microsoft has updated WSL to allow for easy sharing of the system network.

At this point, if you directly open WSL without any configuration, you should see:

1
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。

Open or create the WSL configuration file (located at C:/Users/%Your username%/.wslconfig) and add the following content:

1
2
3
4
5
6
[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

This resolves the issue, and because it is mirrored to WSL, the host’s IP will default to 127.0.0.1.

Therefore, the first line in the previous script can be changed to:

1
2
3
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
changed to
hostip=127.0.0.1

For General Linux

For general Linux, it is even simpler; just set hostip to the proxy server address.

Add to Bash or Zsh

In Bash, add the command as follows:

1
2
3
4
5
vim ~/.bashrc
# 添加命令
alias proxy="source yourPath/proxy.sh"
# 更新
source ~/.bashrc

You can then enable the proxy using the proxy command.

1
2
3
proxy set 开启
proxy unset 关闭
proxy test 测试

Add Authentication Information

Sometimes you need to include username and password authentication information. You can modify the PROXY variable as follows:

1
2
3
4
5
6
7
username=yourUsername
password=yourPassword


PROXY_HTTP="http://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5h://${username}:${password}@${hostip}:${port}"

Complete version is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
wslip=$(hostname -I | awk '{print $1}')
port=yourPort
testurl=yourUrl

username=yourUsername
password=yourPassword


PROXY_HTTP="http://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5://${username}:${password}@${hostip}:${port}"
#PROXY_SOCKS5="socks5h://${username}:${password}@${hostip}:${port}"

set_proxy(){
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"

export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"

export ALL_PROXY="${PROXY_SOCKS5}"
export all_proxy="${PROXY_SOCKS5}"

git config --global http.https://github.com.proxy ${PROXY_HTTP}
git config --global https.https://github.com.proxy ${PROXY_HTTP}

echo "Proxy has been opened."
}

unset_proxy(){
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
unset ALL_PROXY
unset all_proxy
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

echo "Proxy has been closed."
}

test_setting(){
echo "Host IP:" ${hostip}
echo "WSL IP:" ${wslip}
echo "Try to connect to testUrl..."
resp=$(curl -I -s --connect-timeout 5 -m 5 -w "%{http_code}" -o /dev/null ${testurl})
if [ ${resp} = 200 ]; then
echo "Proxy setup succeeded!"
else
echo "Proxy setup failed!"
fi
}

if [ "$1" = "set" ]
then
set_proxy

elif [ "$1" = "unset" ]
then
unset_proxy

elif [ "$1" = "test" ]
then
test_setting
else
echo "Unsupported arguments."
fi

Reference

使用WSL2时控制台输出“wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理-CSDN博客

wsl2配置代理 - 博客园 (cnblogs.com)