# ============================================================================ # ENABLE-WINDOWSSSH.PS1 # Installation et configuration OpenSSH Server sur Windows 11 # ============================================================================ # Date: 11 janvier 2026 # Version: 1.0 # # USAGE: # Exécuter en tant qu'Administrateur # .\Enable-WindowsSSH.ps1 # # Ce script va: # - Installer OpenSSH Server # - Configurer le service en démarrage automatique # - Ouvrir le port 22 dans le pare-feu # - Configurer l'authentification par clé publique # - Afficher les informations de connexion # ============================================================================ #Requires -RunAsAdministrator #Requires -Version 5.1 $ColorSuccess = "Green" $ColorError = "Red" $ColorInfo = "Cyan" function Write-StepHeader { param([string]$Title) Write-Host "`n╔════════════════════════════════════════════════════════════════╗" -ForegroundColor $ColorInfo Write-Host "║ $Title" -ForegroundColor $ColorInfo Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor $ColorInfo } Clear-Host Write-StepHeader "INSTALLATION OPENSSH SERVER" # Étape 1: Vérifier si déjà installé Write-Host "Vérification de l'état actuel..." -ForegroundColor $ColorInfo $SshServerFeature = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*' if ($SshServerFeature.State -eq "Installed") { Write-Host "✅ OpenSSH Server est déjà installé" -ForegroundColor $ColorSuccess } else { Write-Host "📦 Installation d'OpenSSH Server..." -ForegroundColor $ColorInfo Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 Write-Host "✅ OpenSSH Server installé" -ForegroundColor $ColorSuccess } # Étape 2: Démarrer et configurer le service Write-StepHeader "CONFIGURATION DU SERVICE" Write-Host "Démarrage du service sshd..." -ForegroundColor $ColorInfo Start-Service sshd Write-Host "Configuration en démarrage automatique..." -ForegroundColor $ColorInfo Set-Service -Name sshd -StartupType 'Automatic' Write-Host "✅ Service SSH configuré et démarré" -ForegroundColor $ColorSuccess # Étape 3: Configuration du pare-feu Write-StepHeader "CONFIGURATION PARE-FEU" $FirewallRule = Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue if ($FirewallRule) { Write-Host "✅ Règle de pare-feu déjà existante" -ForegroundColor $ColorSuccess } else { Write-Host "Création de la règle de pare-feu..." -ForegroundColor $ColorInfo New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 Write-Host "✅ Règle de pare-feu créée (port 22)" -ForegroundColor $ColorSuccess } # Étape 4: Configuration SSH Write-StepHeader "CONFIGURATION OPENSSH" $SshConfigPath = "$env:ProgramData\ssh\sshd_config" Write-Host "Fichier de config: $SshConfigPath" -ForegroundColor $ColorInfo # Backup de la config $BackupPath = "$SshConfigPath.backup.$(Get-Date -Format 'yyyyMMdd_HHmmss')" Copy-Item $SshConfigPath $BackupPath Write-Host "✅ Backup créé: $BackupPath" -ForegroundColor $ColorSuccess # Activer l'authentification par clé publique $SshConfig = Get-Content $SshConfigPath if ($SshConfig -notmatch "^PubkeyAuthentication yes") { Add-Content -Path $SshConfigPath -Value "`nPubkeyAuthentication yes" Write-Host "✅ Authentification par clé publique activée" -ForegroundColor $ColorSuccess } # Redémarrer le service pour appliquer les changements Restart-Service sshd Write-Host "✅ Service SSH redémarré" -ForegroundColor $ColorSuccess # Étape 5: Créer le répertoire .ssh pour l'utilisateur Write-StepHeader "PRÉPARATION AUTHENTIFICATION PAR CLÉ" $SshDir = "$env:USERPROFILE\.ssh" if (-not (Test-Path $SshDir)) { New-Item -ItemType Directory -Path $SshDir | Out-Null Write-Host "✅ Répertoire .ssh créé: $SshDir" -ForegroundColor $ColorSuccess } else { Write-Host "✅ Répertoire .ssh existe déjà" -ForegroundColor $ColorSuccess } $AuthorizedKeysPath = "$SshDir\authorized_keys" if (-not (Test-Path $AuthorizedKeysPath)) { New-Item -ItemType File -Path $AuthorizedKeysPath | Out-Null Write-Host "✅ Fichier authorized_keys créé" -ForegroundColor $ColorSuccess } # Afficher le chemin pour ajouter les clés Write-Host "`nPour autoriser une clé SSH:" -ForegroundColor $ColorInfo Write-Host " 1. Copiez votre clé publique (id_rsa.pub) depuis Linux" -ForegroundColor White Write-Host " 2. Ajoutez-la au fichier: $AuthorizedKeysPath" -ForegroundColor White Write-Host "" Write-Host "Depuis Linux (base.w-turn.lan), exécutez:" -ForegroundColor $ColorInfo $WindowsIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.IPAddress -like "192.168.100.*"}).IPAddress $WindowsUser = $env:USERNAME Write-Host " type ~/.ssh/id_rsa.pub | ssh $WindowsUser@$WindowsIP 'cat >> .ssh/authorized_keys'" -ForegroundColor Yellow # Étape 6: Informations de connexion Write-StepHeader "INFORMATIONS DE CONNEXION" Write-Host "Hostname : $env:COMPUTERNAME" -ForegroundColor White Write-Host "IP : $WindowsIP" -ForegroundColor White Write-Host "Utilisateur : $WindowsUser" -ForegroundColor White Write-Host "Port SSH : 22" -ForegroundColor White Write-Host "" Write-Host "Connexion depuis Linux:" -ForegroundColor $ColorInfo Write-Host " ssh $WindowsUser@$WindowsIP" -ForegroundColor Yellow Write-Host "" Write-Host "Ou créer un alias dans ~/.ssh/config:" -ForegroundColor $ColorInfo Write-Host @" Host windows11 HostName $WindowsIP User $WindowsUser Port 22 "@ -ForegroundColor Yellow Write-Host "" Write-Host "Ensuite: ssh windows11" -ForegroundColor $ColorInfo # Étape 7: Test du service Write-StepHeader "VÉRIFICATION FINALE" $ServiceStatus = Get-Service sshd Write-Host "Service sshd: $($ServiceStatus.Status)" -ForegroundColor $(if ($ServiceStatus.Status -eq "Running") { $ColorSuccess } else { $ColorError }) $ServiceStartType = (Get-Service sshd).StartType Write-Host "Démarrage : $ServiceStartType" -ForegroundColor $(if ($ServiceStartType -eq "Automatic") { $ColorSuccess } else { $ColorError }) $FirewallStatus = Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" | Select-Object -ExpandProperty Enabled Write-Host "Pare-feu : $(if ($FirewallStatus) { 'Autorisé' } else { 'Bloqué' })" -ForegroundColor $(if ($FirewallStatus) { $ColorSuccess } else { $ColorError }) Write-Host "" Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor $ColorSuccess Write-Host "║ ✅ INSTALLATION SSH TERMINÉE ║" -ForegroundColor $ColorSuccess Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor $ColorSuccess Write-Host "" Write-Host "Prochaines étapes:" -ForegroundColor $ColorInfo Write-Host " 1. Copier votre clé SSH publique depuis Linux" -ForegroundColor White Write-Host " 2. L'ajouter à: $AuthorizedKeysPath" -ForegroundColor White Write-Host " 3. Tester la connexion: ssh $WindowsUser@$WindowsIP" -ForegroundColor White Write-Host "" Read-Host "Appuyez sur Entrée pour quitter"