Last active
January 16, 2026 21:25
-
-
Save Kazanir/55bdd2136aaeab7eb57d721564ec0309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. Enable systemd in WSL (needed for proper sshd): | |
| # /etc/wsl.conf | |
| [boot] | |
| systemd=true | |
| 2. Install and enable sshd: | |
| sudo apt install openssh-server | |
| sudo systemctl enable ssh | |
| sudo systemctl start ssh | |
| 3. Windows firewall rule (from PowerShell admin): | |
| New-NetFirewallRule -Name "WSL SSH" -DisplayName "WSL SSH" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow | |
| 4. Port forwarding from Windows to WSL (WSL2 has its own network): | |
| # Get WSL IP | |
| wsl hostname -I | |
| # Forward port 22 from Windows to WSL | |
| netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=<WSL_IP> | |
| echo "[wsl2]`nvmIdleTimeout=-1" > $env:USERPROFILE\.wslconfig | |
| wsl --shutdown | |
| wsl -d Ubuntu -e echo "WSL restarted" | |
| On LAMP Windows side, create C:\Scripts\wsl-startup.ps1: | |
| # Wait for WSL to be ready | |
| Start-Sleep -Seconds 5 | |
| # Start WSL and get its IP | |
| $wslIp = (wsl -d "Ubuntu 24.04" hostname -I).Trim().Split()[0] | |
| # Update port forwarding | |
| netsh interface portproxy delete v4tov4 listenport=22 listenaddress=0.0.0.0 2>$null | |
| netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=$wslIp | |
| # Start sshd and keep WSL alive | |
| wsl -d "Ubuntu 24.04" -e sudo /usr/sbin/sshd | |
| wsl -d "Ubuntu 24.04" -e sleep infinity | |
| Then replace the scheduled task: | |
| # Remove old task | |
| Unregister-ScheduledTask -TaskName "WSL-KeepAlive" -Confirm:$false | |
| # Create new one | |
| $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -WindowStyle Hidden -File C:\Scripts\wsl-startup.ps1" | |
| $trigger = New-ScheduledTaskTrigger -AtStartup | |
| $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries | |
| Register-ScheduledTask -TaskName "WSL-SSH-Ready" -Action $action -Trigger $trigger -Settings $settings -RunLevel Highest -User "SYSTEM" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment