How To - Remotely SSH into Windows Subsystem for Linux (WSL)
By default, you can only talk to WSL locally on your machine. Accessing applications and services you have installed locally in WSL are not accessible outside the machine you are running WSL on.
This post will assume a few things…
WSL is installed, but an SSH server is NOT installed in WSL nor on the Windows HOST machine
Port 22 is not being used by another service both in WSL or on the Windows HOST machine.
The OS WSL is running is Ubuntu
The machine that is running WSL doesn’t have any firewall running locally
Let’s install the SSH Server
This is super easy to do with this one-line command run within WSL
sudo apt install openssh-server
Let’s get your WSL IP Address
Inside our WSL, we will need to run the “ip” command. Many other how-to articles online use the deprecated command “ifconfig”, which isn’t installed by default in Ubuntu. The commands used also produce a lot of extra “junk” that you just don’t need. This command will give you just the IP address needed.
ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
After running this command, we will need to make a note of this address. My IP address is:
172.23.153.123
Now that you have done these two tasks… Your work in WSL is now complete.
Setup a Port Proxy in the Windows host to the SSH service running within WSL
Open a command prompt as Administrator. Once done, type this command in:
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=22 connectaddress=172.23.153.123 connectport=22
Your “connectaddress” should be identical to the IP address you noted in the previous step.
You are done! You can now access the WSL environment outside the Windows Host by using an SSH client on the network.
In the SSH client, you will need to connect to the IP address you made a note of and use the default SSH port, which is TCP port 22.