Skip to content

Instantly share code, notes, and snippets.

@oeo
Created February 5, 2026 22:41
Show Gist options
  • Select an option

  • Save oeo/459715905697b8143729aee8967732be to your computer and use it in GitHub Desktop.

Select an option

Save oeo/459715905697b8143729aee8967732be to your computer and use it in GitHub Desktop.
WSL2 SSH Diagnostic & Fix Script
# WSL2 SSH Fix Script - Diagnose and repair SSH connection
# Run this in PowerShell as Administrator
Write-Host "╔══════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ WSL2 SSH Diagnostic & Fix ║" -ForegroundColor Cyan
Write-Host "╚══════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
Write-Host "[1/7] Checking WSL status..." -ForegroundColor Green
$wslUser = wsl whoami
Write-Host " → WSL User: $wslUser" -ForegroundColor White
Write-Host "[2/7] Checking if SSH is installed..." -ForegroundColor Green
wsl bash -c "dpkg -l | grep openssh-server | head -1"
Write-Host "[3/7] Installing/reinstalling SSH server..." -ForegroundColor Green
wsl bash -c @'
sudo apt update -qq 2>&1 | grep -v "Hit:" || true
sudo apt install -y openssh-server
'@
Write-Host "[4/7] Configuring SSH (port 2222, password auth)..." -ForegroundColor Green
wsl bash -c @'
sudo sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config
sudo sed -i 's/^Port 22/Port 2222/' /etc/ssh/sshd_config
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
# Show current SSH config
echo ""
echo "SSH Config (relevant lines):"
grep -E "^Port|^PasswordAuthentication|^PermitRootLogin" /etc/ssh/sshd_config
'@
Write-Host "[5/7] Setting password for user..." -ForegroundColor Green
wsl bash -c "echo '$wslUser:oijoij' | sudo chpasswd"
Write-Host " → Password set to: oijoij" -ForegroundColor White
Write-Host "[6/7] Starting SSH service..." -ForegroundColor Green
wsl bash -c @'
sudo service ssh stop 2>/dev/null || true
sleep 1
sudo service ssh start
sleep 2
# Check if SSH is actually running
if sudo service ssh status | grep -q "running"; then
echo " ✓ SSH service is running"
# Check if listening on port 2222
if sudo netstat -tlnp 2>/dev/null | grep -q ":2222"; then
echo " ✓ SSH listening on port 2222"
else
echo " ✗ SSH NOT listening on port 2222"
echo ""
echo "Ports currently listening:"
sudo netstat -tlnp 2>/dev/null | grep sshd || echo "No SSH ports found"
fi
else
echo " ✗ SSH service failed to start"
echo ""
echo "SSH service status:"
sudo service ssh status
fi
'@
Write-Host "[7/7] Testing SSH connection..." -ForegroundColor Green
Write-Host " → Attempting connection to localhost:2222..." -ForegroundColor White
# Test SSH with password (requires sshpass or manual password entry)
Write-Host ""
Write-Host "Testing SSH connection (you may need to enter password: oijoij):" -ForegroundColor Yellow
ssh -p 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=NUL $wslUser@localhost "echo '✓ SSH connection successful!' && uname -a"
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════════╗" -ForegroundColor Green
Write-Host "║ SSH is Working! ║" -ForegroundColor Green
Write-Host "╚══════════════════════════════════════════════════════════════╝" -ForegroundColor Green
Write-Host ""
Write-Host "Connection Details:" -ForegroundColor Cyan
Write-Host " Host: localhost (or your public IP)" -ForegroundColor White
Write-Host " Port: 2222" -ForegroundColor White
Write-Host " Username: $wslUser" -ForegroundColor White
Write-Host " Password: oijoij" -ForegroundColor White
} else {
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════════╗" -ForegroundColor Red
Write-Host "║ SSH Connection Failed ║" -ForegroundColor Red
Write-Host "╚══════════════════════════════════════════════════════════════╝" -ForegroundColor Red
Write-Host ""
Write-Host "Diagnostic information:" -ForegroundColor Yellow
Write-Host ""
Write-Host "WSL IP address:" -ForegroundColor Cyan
wsl ip addr show eth0 | Select-String "inet\b"
Write-Host ""
Write-Host "SSH service status:" -ForegroundColor Cyan
wsl sudo service ssh status
Write-Host ""
Write-Host "Listening ports:" -ForegroundColor Cyan
wsl sudo netstat -tlnp | Select-String "2222"
Write-Host ""
Write-Host "Try manually starting SSH:" -ForegroundColor Yellow
Write-Host " wsl sudo service ssh restart" -ForegroundColor White
}
Write-Host ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment