Connecting to WSL 2 from outside the Windows machine is possible with just a few commands in the
PowerShell (run as administrator).
In order for it to work, select non-default ports (2020, 29333, and 23333) that will be forwarded (inside the Windows box) to the
services of interest (SSHD, Bitcoin Core, and Stratum) respectively. It is possible for these external ports outside the Windows box
to be the known default ports, but the solutions are messy and beyond this simple post! One solution could be to change the default
ports of the services running in WSL and then forward the desired external port numbers to these new WSL port numbers.
So, why is this the recommended solution? WSL 2 runs with Windows' Hyper V where it has its own virtual network adapter and two
internal IP addresses (that are changed after each restart). One IP is for the virtual ethernet adapter and the other is for WSL directly.
On top of all this, there is a process (wslrelay.exe) actively running so all of WSL open ports can be accessible via "localhost" only.
Set Internal Port Forwarding Rules for WSL (Run as administrator: PowerShell)
- netsh interface portproxy set v4tov4 listenport=2022 listenaddress=0.0.0.0 connectport=22 connectaddress=localhost # SSH
- # netsh interface portproxy set v4tov4 listenport=29333 listenaddress=0.0.0.0 connectport=19333 connectaddress=localhost # Bitcoin Core (micro)
- netsh interface portproxy set v4tov4 listenport=23333 listenaddress=0.0.0.0 connectport=3333 connectaddress=localhost # Stratum
List All Internal Port Forwarding Rules for WSL
- netsh interface portproxy show all
Delete Internal Port Forwarding Rules for WSL (Run as administrator: PowerShell)
- netsh interface portproxy delete v4tov4 listenport=2022 listenaddress=0.0.0.0 # SSH
- netsh interface portproxy delete v4tov4 listenport=29333 listenaddress=0.0.0.0 # Bitcoin Core (micro)
- netsh interface portproxy delete v4tov4 listenport=23333 listenaddress=0.0.0.0 # Stratum
Configure Firewall Rules (Run as administrator: PowerShell)
- netsh advfirewall firewall add rule name="WSL SSH 2022" dir=in action=allow protocol=TCP localport=2022 # SSH
- # netsh advfirewall firewall add rule name="WSL Micro 29333" dir=in action=allow protocol=TCP localport=29333 remoteip=192.168.0.0/16
- netsh advfirewall firewall add rule name="WSL Stratum 23333" dir=in action=allow protocol=TCP localport=23333 remoteip=192.168.0.0/16
Show Firewall Rules
- netsh advfirewall firewall show rule name=all | select-string -pattern "WSL" # Show All
- netsh advfirewall firewall show rule name="WSL SSH 2022" # Show SSH
- netsh advfirewall firewall show rule name="WSL Micro 29333" # Show Bitcoin Core (micro)
- netsh advfirewall firewall show rule name="WSL Stratum 23333" # Show Stratum
Delete Firewall Rules (Run as administrator: PowerShell)
- netsh advfirewall firewall delete rule name="WSL SSH 2022" # Delete SSH
- netsh advfirewall firewall delete rule name="WSL Micro 29333" # Delete Bitcoin Core (micro)
- netsh advfirewall firewall delete rule name="WSL Stratum 23333" # Delete Stratum