SSH Into a WSL2 Instance (With Mirrored Networking)

homelab

SSH Into a WSL2 Instance (With Mirrored Networking)

I run most of my homelab tooling (Ansible, kubectl, Git) inside a Debian WSL2 instance on my Windows workstation. I wanted to SSH directly into that WSL instance from other machines on my LAN / Tailscale network without hopping through Windows first. Here's the setup that worked.

TL;DR

  • Enable networkingMode=mirrored in .wslconfig so WSL shares the Windows host's IP
  • Windows may already run sshd on port 22, so there are two sshds competing for one port - I moved Windows to 2222 and gave WSL the standard port 22
  • Each side needs its own Windows Firewall inbound rule (required because firewall=true routes WSL-bound traffic through Windows Firewall)
  • ssh user@<windows-ip> now lands you directly in WSL; ssh -p 2222 reaches Windows

General layout

flowchart LR
    subgraph LAN
        A[Laptop / NAS / K3s nodes]
    end
    subgraph PC["Windows PC - 192.168.0.117"]
        subgraph WIN["Windows host"]
            WSSHD["Windows sshd<br/>port 2222"]
            FW["Windows Firewall<br/>(Hyper-V filter)"]
        end
        subgraph WSL["WSL2 - Debian"]
            LSSHD["WSL sshd<br/>port 22"]
        end
    end
    A -->|"ssh [email protected]<br/>(port 22)"| FW
    FW -->|"allowed by WSL2 SSH rule"| LSSHD
    A -.->|"ssh -p 2222<br/>(Tailscale / Private profile)"| WSSHD

With mirrored networking, WSL doesn't hide behind a NAT - it shares the host's IP addresses, so anything on the LAN that can reach the Windows PC can reach listeners inside WSL. The catch is that sharing goes both ways: WSL and Windows compete for the same ports, and Windows will capture port 22 first if it's in use.

Port conflicts

My first attempt failed due to the port 22 conflict:

× ssh.service - OpenBSD Secure Shell server
     Active: failed (Result: exit-code)

Windows 11 ships an optional OpenSSH Server feature, and mine was installed, running, and holding 0.0.0.0:22. There are two reasonable ways to address this: run WSL on an alternate port (2222 works fine), or evict Windows from 22. I did the first, then swapped - I wanted the standard port for WSL since it's my primary environment, and Windows sshd is only an occasional management path.

Setup

1. .wslconfig (Windows side, C:\Users\<you>\.wslconfig)

[wsl2]
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

mirrored is the key setting; firewall=true means Windows Firewall rules apply to traffic headed for WSL, which is what makes step 4 necessary. Restart WSL after changing this (wsl --shutdown).

2. sshd inside WSL

sudo apt update && sudo apt install -y openssh-server

Don't edit the main sshd_config - use a drop-in at /etc/ssh/sshd_config.d/wsl-ssh.conf:

# Windows sshd was moved to 2222; WSL takes the standard port.
Port 22
PasswordAuthentication yes
sudo systemctl enable --now ssh

3. Move Windows sshd to 2222

Windows keeps its config at C:\ProgramData\ssh\sshd_config

  • Multiple Port lines are cumulative - sshd will listen on all of them. Comment out any existing Port 22
  • Order matters: if you restart Windows sshd onto 2222 while WSL is still holding 2222 (or vice versa), the bind fails and the service stops. Move the listener off the target port first.
# after removing comments this is the whole windows sshd_config @ C:\ProgramData\ssh\sshd_config
Port 2222
AuthorizedKeysFile	.ssh/authorized_keys
Subsystem	sftp	sftp-server.exe
Match Group administrators
       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
"$env:WINDIR\System32\OpenSSH\sshd.exe" -t   # test config to make sure syntax is ok
Restart-Service sshd # restart the windows sshd service

4. Windows Firewall rules (elevated PowerShell)

The built-in OpenSSH rule is program-scoped to sshd.exe and pinned to port 22, so retarget it. WSL-bound traffic gets its own rule. This can also be done through the windows firewall GUI.

Get-NetFirewallRule -DisplayName 'OpenSSH SSH Server (sshd)' |
    Get-NetFirewallPortFilter | Set-NetFirewallPortFilter -LocalPort 2222
New-NetFirewallRule -DisplayName "WSL2 SSH (22)" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow -Profile Any

-Profile Any on the WSL rule matters if your Wi-Fi network is set to Public (mine was) - otherwise the rule silently doesn't apply. I deliberately left the built-in Windows rule at its original Private-only scope, so Windows sshd stays reachable over Tailscale but not from the public-profile Wi-Fi LAN, same as before.

Final changes - disable password authentication

ssh-copy-id user@<windows-pc-IP>

Optionally flip PasswordAuthentication to no in the drop-in once key auth works.

Fallback

Even before any of this, hopping through Windows worked with no setup at all (now on the new Windows port):

ssh -t -p 2222 user@<windows-pc-IP> wsl

It SSHes into Windows and immediately drops you into a WSL shell. Perfectly fine for occasional use - I wanted a direct path for tooling.

Tailscale

If Tailscale runs on the Windows host, mirrored mode extends it into WSL too, so ssh user@<tailscale-ip> reaches WSL from anywhere on the tailnet once Tailscale is logged in. One sshd, one port, reachable from LAN and tailnet alike.

Comments

No account required. Open the Sign In menu and choose Anonymous to comment with a name.

Comments loading...