21

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

6
  • 3
    Here are instructions to acomplish what you are attempting to do.
    – Ramhound
    Apr 23, 2022 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, 2022 at 22:32
  • 2
    You did read the article completely, right? The second to last section contains what you need.
    – Daniel B
    Apr 23, 2022 at 22:34
  • Oh thank you for noticing @DanielB
    – Victor2748
    Apr 23, 2022 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, 2022 at 13:28

3 Answers 3

18

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)

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

0

If you are running WSL2 on Windows 11 22H2 or higher, you can now use mirrored mode networking.

  1. In your %USERPROFILE%\.wslconfig file in Windows (create it if it doesn't exist), add the line networkingMode=mirrored under [wsl2].

  2. Allow inbound connections through the Hyper-V firewall. This can be done by running one of the following commands in PowerShell with administrator privileges.

    • One port or range of ports: New-NetFirewallHyperVRule -DisplayName "WSL SSH" -Direction Inbound -VMCreatorId "{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}" -Protocol TCP -LocalPorts 22 - change -DisplayName to your desired value and -LocalPorts to your desired port or range of ports

    • All ports: Set-NetFirewallHyperVVMSetting -Name "{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}" -DefaultInboundAction Allow

  3. Run WSL with the new config, for example using wsl --shutdown and then wsl to restart it.

This should be enough to allow connections to sshd and other WSL-hosted servers over LAN.

Sources:

You must log in to answer this question.

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