27

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

3 Answers3

22

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
  • 44,080
8

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
  • 28,025
7

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:

Nnnes
  • 256