Using Windows Proxy Settings in WSL2
Setting up a Proxy for WSL2
WSL2 (Windows Subsystem for Linux 2) utilizes a virtual machine approach, separating its network from Windows. This means that network traffic no longer shares the same subnet as the Windows host, making it more challenging to use the host's proxy settings.
Obtaining the Host IP in WSL
In WSL, the IP address changes with each reboot, so you cannot set a
static proxy. WSL2 writes the IP address to
/etc/resolv.conf
, allowing you to retrieve the host's IP
using the following command:
1 |
|
To obtain WSL2's IP address, you can use:
1 |
|
Setting up the Proxy
With the host's IP address, you can set up the proxy by exporting
environment variables. Replace <Port>
with your
proxy's port number, and don't forget to allow connections from the
local network in your proxy software.
1 |
|
However, this method requires you to reset the proxy every time you restart the terminal, and you'll need to manually enter the IP address, which can be cumbersome. This is where a script comes in handy!
1 |
|
Remember to replace <PORT>
on line 4 with your
proxy's port number!
If you want Git to use the proxy as well, add the following commands
to the set_proxy
and unset_proxy
functions:
1 |
|
After that, run . ./proxy.sh set
to automatically set
the proxy. Use unset
to cancel the proxy and
test
to check the proxy status and verify if the
environment variables are correctly modified.
Remember to include the .
before running the script
(e.g., . ./proxy.sh set
or
source ./proxy.sh set
). Running the script directly (e.g.,
./proxy.sh set
or sh proxy.sh set
) will
execute it in a child shell, which won't affect the current shell's
environment variables.
Additionally, you can add the following lines to your
~/.bashrc
file, replacing the paths with the location of
your script:
1 |
|
The first line sets an alias for the script, allowing you to use the
proxy
command from any directory to access the script's
functionality. The second line runs the script during shell startup,
automatically setting the proxy without additional steps.
Firewall Settings
If you've completed the previous steps and can already use the proxy successfully, you can skip this section. However, if the proxy is correctly set up, especially if you've allowed connections from the local network, but you still can't access the internet, and the proxy software isn't receiving requests, it's possible that the Windows Firewall is blocking the connection.
Try pinging the host IP and using telnet
to connect to
the proxy port to check if the connection is successful. If the
connection fails, it's likely a firewall issue.
You can try allowing the proxy software through the firewall in the "Allow an app or feature through Windows Defender Firewall" section of the Control Panel's firewall settings.
Alternatively, you can create an inbound rule in the Advanced Settings. If you're unsure, you can allow any program using any protocol and permit inbound requests from the 172.16.0.0/12 and 192.168.0.0/16 remote IP ranges.