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?

2
  • 1
    One problem I've seen is different clock time causing SSH login issues. This might be problematic if one machine is local time and the other UTC... or not. This is just a possible issue, not an answer. Commented Jan 29, 2021 at 18:18
  • hm interesting, I just checked mine and they are all on the same time so I don't think that's the issue here
    – jaib1
    Commented Jan 29, 2021 at 18:26

1 Answer 1

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).

5
  • Ah I see, I was hoping there would be a way to jump directly into WSL, but I guess there's no way around having to jump into Windows first. I guess this isn't a huge deal. Thanks for letting me know about the virtual NAT'd NIC and the other helpful info!
    – jaib1
    Commented Jan 29, 2021 at 23:49
  • Don't forget to check out the other answer I linked above. It shows how you can access the port directly. I just prefer to do it with an ssh into Windows first, but you don't have to. Commented Jan 30, 2021 at 0:46
  • you mean with port forwarding? I think of this as still jumping through Windows?
    – jaib1
    Commented Jan 30, 2021 at 0:53
  • Ah, I see. I guess you could consider it that way. Yes, as long as WSL is running something inside or on Windows, there's definitely going to be some Windows networking involvement. The WSL1 (bridged by default) or WSL2 (bridging through Hyper-V) would be the closest you could come to "direct access" to WSL, but they would still be "sharing" the Windows NIC, of course. Commented Jan 30, 2021 at 1:01
  • yup makes sense, guess I was just being naive thinking otherwise
    – jaib1
    Commented Jan 30, 2021 at 1:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .