19

I set up my Ubuntu WSL instance and am running an SSH server on it. However, when I do ifconfig on the Ubuntu console, my Ipv4 is 172.26.66.223, which is different from my regular machine's ipv4 192.168.0.248

While I am able to SSH into my WSL instance using ssh localhost or ssh 172.26.66.223 from the same machine, using ssh 192.168.0.248 doesn't work. When I try to do ssh 172.26.66.223 from another computer on the same network it says that the connection timed out.

How can I SSH into an OpenSSL server running on my WSL instance from another device on the network?

Here is my network information:

enter image description here

enter image description here

Victor2748
  • 273
  • 2
  • 6
  • 18
  • 3
    [Here](https://docs.microsoft.com/en-us/windows/wsl/networking) are instructions to acomplish what you are attempting to do. – Ramhound Apr 23 '22 at 22:14
  • @Ramhound It doesn't work. This is basically what I did in the question and it worked only from local machine from windows, but did not work from another machine on the same network. The 172.*.*.* ip wasn't visible from outside the computer. However, when the SSH server was running on Windows and not WSL, it connected successfully, (using the parent computers ipv4), so its not the network issue – Victor2748 Apr 23 '22 at 22:32
  • 2
    You did read the article completely, right? The second to last section contains what you need. – Daniel B Apr 23 '22 at 22:34
  • Oh thank you for noticing @DanielB – Victor2748 Apr 23 '22 at 23:29
  • @Ramhound it worked! Thanks for the help, please post this as an answer – Victor2748 Apr 23 '22 at 23:29
  • 2
    I honestly had no idea that WSL2 wasn't even accessible from the network. I'm spoiled by WSL1 it seems. True kernel integration ftw. – Dev Apr 25 '22 at 13:28

3 Answers3

15

You need to run the following command:

netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.0.248

Source: Accessing a WSL 2 distribution from your local area network (LAN)

Ramhound
  • 41,734
  • 35
  • 103
  • 130
7

Since your WSL2 address changes on each reboot, the address that you'll need to forward to changes each time. If you use the "forwarding" method described in the Microsoft docs, you will need to:

  • Delete previous forwarding rules on each reboot (best practice, at least, to avoid leaving numerous old forwarding rules in place)
  • Forward to the new WSL2 address after each reboot (or wsl --shutdown)

Update Note: The following will no longer work with recent WSL releases installed from the Microsoft Store. It is left here for historical purposes, and in case the SSH feature is eventually fixed in a newer WSL release.

My preference is to instead run the SSH server in Windows (it's built in to Windows 10 and 11 now anyway -- See the installation instructions). Once you have that configured, you can easily SSH into a WSL session with:

ssh -t 192.168.0.248 "wsl ~"

That also gives you a lot more control, like the ability to log in remotely as root:

ssh -t 192.168.0.248 "wsl ~ -u root"

Or a different distribution with:

ssh -t 192.168.0.248 "wsl ~ -d Debian"

More options (which don't require forwarding) in this answer.

NotTheDr01ds
  • 17,574
  • 4
  • 44
  • 81
0

Honestly I had never heard of WSL before but it does from my experience (without reading anything on WSL, but I know a decent amount about Linux and Windows), what you are doing seems like the hard way. Why configure it on the Linux side. I am assuming that you can see the WSL Network driver in Windows, and if so, do two things. 1) bridge that network with your internet connection by holding control and clicking both of them and then right clicking on either and click "Bridge", then 2) try to change the assigned IP address in the driver to one on your subnet 192.168.1.200 for example. At this point, you should be able to ssh directly to 192.168.1.200. I appologize if I have the wrong procedure here it has been a minute since I did all that, but since I did not see the "use Windows to configure" suggestion I thought I would chime in. If anyone knows the procedure better than me please chime in (even if I am wrong I don't mind being told that - it may save me some time doing something in the future :)

Mark Rusty
  • 21
  • 2
  • 1
    That would be nice, but it just won't work. The WSL2 Hyper-V-based virtual machine is "managed" (a.k.a. not user accessible) and that includes the networking. So no, unfortunately you can't see the WSL interface in Windows to bridge it like you would other interfaces. However, there is a beta feature in newer WSL releases that allows you to specify a *different* Hyper-V switch for WSL that is supposed to be able to be bridged. [This blog post](https://randombytes.substack.com/p/bridged-networking-under-wsl?s=r) is the best write-up I've seen on the topic, but I haven't tried it myself. – NotTheDr01ds Oct 08 '22 at 01:21