2

I'm trying to ssh into Ubuntu 20.04 running on a Windows 10 machine via Windows Subsystem for Linux 2 (WSL 2).

I've followed this guide on medium, and have additionally looked at this, this, and a couple other blog posts.

Basically, what I've done is:

  1. Reinstall openssh-server on the Ubuntu server.
  2. Changed the sshd_config file to listen on port 2222 and set PasswordAuthentication to yes on the Ubuntu server.
  3. Found the IP address by installing net-tools and running ifconfig on the Ubuntu server.
  4. Set a rule to open port 2222 in Windows Defender Firewall on Windows.

From the same computer, I can ssh from Windows into the Ubuntu server using the flag -p 2222 for the port. However, when I try to ssh into the Ubuntu server from another computer on the same internet network, I am getting time outs. (On this other computer, however, I can ssh into Windows on the first computer - I installed open-ssh server on both Windows and Ubuntu of the first computer).

Any suggestions for troubleshooting this?

jaib1
  • 123

1 Answers1

3

From the looks of it, all of those guides you linked to were posted long enough ago that they were using WSL1, since WSL2 just came out last summer'ish. If they had been using WSL2 before it launched, they would probably have mentioned that it required Windows Insider/Preview.

The big difference between WSL1 and WSL2 in this regard is that WSL1 ran in a pseudo-bridge mode network with the Windhows host, but WSL2 runs in a Hyper-V VM with its own virtual, NAT'd NIC. From the Windows computer itself, WSL2 seems to do some local port mapping magic to allow you to reach WSL2 services on localhost. But the external IP does not behave that way, so other computers on the network (and even other networks, such as Docker, on the same same machine), cannot directly access the WSL2 interface and ports.

I've provided some answers to this for other services in other questions (such as this), and I highly encourage you to read it for some insight.

But for SSH, I personally take a different approach. I simply run the Windows OpenSSH server on the Windows host itself (on port 22), and then also run an SSH server (as you have set up) in each of my WSL instances (both WSL1 and WSL2). I can then use the SSH ProxyCommand option to utilize the Windows host as a jump host into the WSL instances (I have multiple instances at most times for different scenario development and testing).

It's really even easier than that in most cases. Most of the time, I just SSH "into" my WSL instances with something like:

ssh -t windowsip wsl or ssh -t windowsip wsl -d Ubuntu20.04

The first one just launches me into the default WSL instance. The second allows me to select a specific instance/distrobution.

I really only use the ProxyCommand version with Ansible or other applications which require "real" SSH on the endpoint.

Also note that you can use this trick to even launch the SSH server in each instance, avoiding the hassle of setting up the "launch via task manager at boot". Just run something like:

ssh -o "RemoteCommand=wsl -d Ubuntu-20.04 sudo service ssh status ^|^| sudo service ssh start" windowshost_ip_or_name (the ^|^| is escaping the || for CMD).

NotTheDr01ds
  • 28,025