# 一键修复 Windows SSH 认证 # 1. 获取服务器公钥 $serverKey = ssh root@82.29.54.80 "cat ~/.ssh/id_rsa.pub" # 2. 添加到管理员 authorized_keys(管理员组用户) $adminKeysFile = "C:\ProgramData\ssh\administrators_authorized_keys" if (!(Test-Path $adminKeysFile)) { New-Item -ItemType File -Path $adminKeysFile -Force } Add-Content -Path $adminKeysFile -Value $serverKey # 3. 修复权限 icacls $adminKeysFile /inheritance:r icacls $adminKeysFile /grant "SYSTEM:(F)" icacls $adminKeysFile /grant "BUILTIN\Administrators:(F)" # 4. 也添加到用户 authorized_keys(备用) $userKeysFile = "$env:USERPROFILE\.ssh\authorized_keys" if (!(Test-Path $userKeysFile)) { New-Item -ItemType File -Path $userKeysFile -Force } if (!(Get-Content $userKeysFile | Select-String "root@vps21249823.hosteons.com")) { Add-Content -Path $userKeysFile -Value $serverKey } icacls $userKeysFile /inheritance:r icacls $userKeysFile /grant:r "${env:USERNAME}:(F)" icacls $userKeysFile /grant "SYSTEM:(R)" # 5. 重启 SSH Server Restart-Service sshd Write-Host "修复完成!" -ForegroundColor Green