Enable SSH Server in the Windows Subsystem for Linux (WSL)

In this guide, I will show you how to configure an SSH Server in your Windows Subsystem for Linux (WSL), so you can connect other devices in the same network directly to it.
Enable SSH Server in the Windows Subsystem for Linux (WSL)
What do you need
In this example, I am using Debian as Linux distro in the WSL, but should be compatible or adaptable with other Linux distro.
Process in the WSL
First, we need to make changes in the WSL, so, you can open the WSL profile in your Windows Terminal.
Install OpenSSH server
sudo apt update
sudo apt install openssh-server --no-install-recommends --no-install-suggests
Configure OpenSSH port
By default, OpenSSH uses port 22, but maybe you want to use that port to connect with your Windows host system. So, for the SSH Server in the WSL, we are using port 2222:
sudo sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config
Restart SSH Server
Now restart the SSH Server service, if you are using a Linux distro with systemd, you can use:
sudo systemctl restart ssh
Obtain the IP address of the WSL
By default the WSL uses a NAT-based network architecture, so, to identify their IP address use:
ip -o -f inet addr show eth0 | awk '{print $4}' | cut -d '/' -f1
A typical output might be 172.30.98.229
.
Process in the Windows host
Now we need to make some changes in the Windows host system, so, we need to open a PowerShell terminal with administrator rights.
Add a port forwarding rule
We need to add a rule to forward the traffic to port 2222 to the IP address of your WSL.
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=2222 connectaddress=172.30.98.229 connectport=2222
Remember to change the IP address 172.30.98.229
with the previously obtained WSL IP address.
With this port forward you can test your connection within your Windows host:
ssh -p 2222 tux@localhost
Remember to change the user tux
with the user of your WSL system.
Allow the access to the port 2222 in the Windows Firewall
netsh advfirewall firewall add rule name="SSH server in WSL" dir=in action=allow protocol=TCP localport=2222 profile=private
And that’s it, now other devices can connect to your WSL SSH server using ssh -p 2222 tux@192.168.1.2
.
Where 192.168.1.2
is the IP address of your Windows host system.
Final considerations
Remember that for other devices to connect with your WSL SSH server, they need to be connected to the same local network, and if it is a Wifi network that needs to be configured as type private
in your Windows host system.
Categories
Automation scripting Development tools Front end web development Infrastructure Kubernetes Programming guide Security Software architectureTags
Recent Posts