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. This article explains how to set up a script 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:
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.
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:
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: