Last active
February 5, 2026 22:38
-
-
Save oeo/854776f17f4d4237067b5a7243c230f2 to your computer and use it in GitHub Desktop.
WSL2 SSH Setup Script - Fully Automated
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
| # WSL2 SSH Setup Script - Fully Automated | |
| # Run this in PowerShell as Administrator on your Windows machine | |
| # Check if running as administrator | |
| $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
| if (-not $isAdmin) { | |
| Write-Host "ERROR: This script must be run as Administrator" -ForegroundColor Red | |
| Write-Host "Right-click PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow | |
| exit 1 | |
| } | |
| Write-Host "╔══════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan | |
| Write-Host "║ WSL2 SSH Setup - Fully Automated ║" -ForegroundColor Cyan | |
| Write-Host "╚══════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan | |
| Write-Host "" | |
| # Step 1: Install and configure SSH in WSL2 | |
| Write-Host "[1/6] Installing OpenSSH Server in WSL2..." -ForegroundColor Green | |
| wsl bash -c @' | |
| #!/bin/bash | |
| set -e | |
| echo " → Installing openssh-server..." | |
| sudo apt update -qq 2>&1 | grep -v "Hit:" || true | |
| sudo apt install -y openssh-server >/dev/null 2>&1 | |
| echo " → Configuring SSH..." | |
| sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config | |
| sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config | |
| sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config | |
| sudo sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config | |
| sudo sed -i 's/^Port 22/Port 2222/' /etc/ssh/sshd_config | |
| echo " → Creating .ssh directory..." | |
| mkdir -p ~/.ssh | |
| chmod 700 ~/.ssh | |
| echo " → Starting SSH service..." | |
| sudo service ssh stop 2>/dev/null || true | |
| sudo service ssh start | |
| echo " ✓ SSH server configured and running" | |
| '@ | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "ERROR: Failed to install SSH in WSL2" -ForegroundColor Red | |
| exit 1 | |
| } | |
| # Step 2: Get WSL IP address | |
| Write-Host "[2/6] Getting WSL IP address..." -ForegroundColor Green | |
| $wslIP = wsl bash -c "ip addr show eth0 | grep 'inet\b' | awk '{print `$2}' | cut -d/ -f1" | |
| Write-Host " → WSL IP: $wslIP" -ForegroundColor White | |
| # Step 3: Set up port forwarding | |
| Write-Host "[3/6] Setting up Windows → WSL port forwarding..." -ForegroundColor Green | |
| netsh interface portproxy delete v4tov4 listenport=2222 listenaddress=0.0.0.0 2>&1 | Out-Null | |
| netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=$wslIP | |
| Write-Host " ✓ Port 2222 forwarded to WSL" -ForegroundColor White | |
| # Step 4: Configure Windows Firewall | |
| Write-Host "[4/6] Configuring Windows Firewall..." -ForegroundColor Green | |
| Remove-NetFirewallRule -DisplayName "WSL2 SSH" -ErrorAction SilentlyContinue | Out-Null | |
| New-NetFirewallRule -DisplayName "WSL2 SSH" -Direction Inbound -Protocol TCP -LocalPort 2222 -Action Allow | Out-Null | |
| Write-Host " ✓ Firewall rule created" -ForegroundColor White | |
| # Step 5: Set WSL password (non-interactive) | |
| Write-Host "[5/6] Setting WSL user password..." -ForegroundColor Green | |
| $wslUser = "taky" | |
| $wslPassword = "oijoij" | |
| Write-Host " → WSL Username: $wslUser" -ForegroundColor White | |
| Write-Host " → Setting password: $wslPassword" -ForegroundColor White | |
| wsl bash -c "echo '$wslUser:$wslPassword' | sudo chpasswd" | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "ERROR: Failed to set password" -ForegroundColor Red | |
| exit 1 | |
| } | |
| Write-Host " ✓ Password set successfully" -ForegroundColor White | |
| # Step 6: Display connection details | |
| Write-Host "[6/6] Getting connection details..." -ForegroundColor Green | |
| Write-Host "" | |
| # Get Windows IP (try multiple methods) | |
| $WindowsIP = $null | |
| try { | |
| $WindowsIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { | |
| $_.InterfaceAlias -notlike "*Loopback*" -and | |
| $_.InterfaceAlias -notlike "*WSL*" -and | |
| $_.IPAddress -notmatch "^169\." -and | |
| $_.IPAddress -ne "127.0.0.1" | |
| } | Select-Object -First 1).IPAddress | |
| } catch { | |
| $WindowsIP = "Unable to detect" | |
| } | |
| Write-Host "╔══════════════════════════════════════════════════════════════╗" -ForegroundColor Green | |
| Write-Host "║ Setup Complete! ║" -ForegroundColor Green | |
| Write-Host "╚══════════════════════════════════════════════════════════════╝" -ForegroundColor Green | |
| Write-Host "" | |
| Write-Host "SSH Connection Details:" -ForegroundColor Cyan | |
| Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan | |
| Write-Host " Host: $WindowsIP" -ForegroundColor White | |
| Write-Host " Port: 2222" -ForegroundColor White | |
| Write-Host " Username: $wslUser" -ForegroundColor White | |
| Write-Host " Password: $wslPassword" -ForegroundColor White | |
| Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan | |
| Write-Host "" | |
| Write-Host "To connect:" -ForegroundColor Yellow | |
| Write-Host " ssh -p 2222 $wslUser@$WindowsIP" -ForegroundColor White | |
| Write-Host "" | |
| Write-Host "To test locally:" -ForegroundColor Yellow | |
| Write-Host " ssh -p 2222 $wslUser@localhost" -ForegroundColor White | |
| Write-Host "" | |
| # Check if we can get public IP | |
| Write-Host "Checking for public IP..." -ForegroundColor Yellow | |
| try { | |
| $PublicIP = (Invoke-WebRequest -Uri "https://icanhazip.com" -UseBasicParsing -TimeoutSec 5).Content.Trim() | |
| Write-Host " → Public IP: $PublicIP" -ForegroundColor White | |
| Write-Host "" | |
| Write-Host "If connecting from outside your network:" -ForegroundColor Yellow | |
| Write-Host " 1. Forward port 2222 on your router to this computer" -ForegroundColor White | |
| Write-Host " 2. Connect using: ssh -p 2222 $wslUser@$PublicIP" -ForegroundColor White | |
| } catch { | |
| Write-Host " → Could not determine public IP (this is okay)" -ForegroundColor Gray | |
| } | |
| Write-Host "" | |
| Write-Host "Notes:" -ForegroundColor Cyan | |
| Write-Host " • SSH service will stop when WSL shuts down" -ForegroundColor Gray | |
| Write-Host " • To restart: wsl sudo service ssh start" -ForegroundColor Gray | |
| Write-Host " • To stop: wsl sudo service ssh stop" -ForegroundColor Gray | |
| Write-Host "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment