# 配置SSH密钥认证 # 用途:将SSH公钥复制到飞牛OS服务器,实现免密登录 # 使用:.\scripts\setup-ssh-key.ps1 param( [string]$ServerIP = "192.168.42.252", [string]$ServerUser = "admin", [string]$ServerPassword = "admin123" ) Write-Host "=== 配置SSH密钥认证 ===" -ForegroundColor Cyan Write-Host "服务器: $ServerIP" -ForegroundColor Yellow Write-Host "用户: $ServerUser`n" -ForegroundColor Yellow # 检查SSH密钥是否存在 $sshKeyPath = "$env:USERPROFILE\.ssh\id_rsa.pub" if (-not (Test-Path $sshKeyPath)) { Write-Host "❌ SSH公钥不存在,正在生成..." -ForegroundColor Yellow ssh-keygen -t rsa -b 4096 -f "$env:USERPROFILE\.ssh\id_rsa" -N '""' -q Write-Host "✅ SSH密钥生成完成" -ForegroundColor Green } else { Write-Host "✅ SSH公钥已存在: $sshKeyPath" -ForegroundColor Green } # 读取公钥内容 $publicKey = Get-Content $sshKeyPath -Raw Write-Host "`n公钥内容:" -ForegroundColor Yellow Write-Host $publicKey.Trim() -ForegroundColor Gray # 将公钥复制到服务器 Write-Host "`n正在将公钥复制到服务器..." -ForegroundColor Yellow # 使用PowerShell的SSH命令(需要手动输入密码一次) $sshCommand = @" mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$($publicKey.Trim())' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && echo 'SSH密钥配置成功' "@ Write-Host "`n请按照以下步骤手动配置:" -ForegroundColor Yellow Write-Host "1. SSH连接到服务器: ssh $ServerUser@$ServerIP" -ForegroundColor White Write-Host "2. 密码: $ServerPassword" -ForegroundColor White Write-Host "3. 执行以下命令(将公钥内容替换为下方显示的内容):" -ForegroundColor White Write-Host " mkdir -p ~/.ssh && chmod 700 ~/.ssh" -ForegroundColor Gray Write-Host " echo '$($publicKey.Trim())' >> ~/.ssh/authorized_keys" -ForegroundColor Gray Write-Host " chmod 600 ~/.ssh/authorized_keys" -ForegroundColor Gray Write-Host " exit" -ForegroundColor Gray Write-Host "`n或者运行脚本后,手动执行SSH命令(需要输入密码):" -ForegroundColor Yellow Write-Host "ssh $ServerUser@$ServerIP `"$sshCommand`"" -ForegroundColor Cyan Write-Host "`n按任意键继续(或Ctrl+C取消)..." -ForegroundColor Yellow $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") ssh -o StrictHostKeyChecking=no "$ServerUser@$ServerIP" $sshCommand if ($LASTEXITCODE -eq 0) { Write-Host "`n✅ SSH密钥配置成功!" -ForegroundColor Green Write-Host "`n测试免密登录..." -ForegroundColor Yellow # 测试连接 $testResult = ssh -o StrictHostKeyChecking=no -o BatchMode=yes "$ServerUser@$ServerIP" "echo '免密登录成功'" 2>&1 if ($LASTEXITCODE -eq 0) { Write-Host "✅ 免密登录测试成功!" -ForegroundColor Green } else { Write-Host "⚠️ 免密登录测试失败,可能需要手动配置" -ForegroundColor Yellow Write-Host "手动配置方法:" -ForegroundColor Cyan Write-Host "1. 复制以下公钥内容:" -ForegroundColor White Write-Host $publicKey.Trim() -ForegroundColor Gray Write-Host "`n2. SSH连接到服务器后执行:" -ForegroundColor White Write-Host " mkdir -p ~/.ssh" -ForegroundColor Gray Write-Host " chmod 700 ~/.ssh" -ForegroundColor Gray Write-Host " echo '公钥内容' >> ~/.ssh/authorized_keys" -ForegroundColor Gray Write-Host " chmod 600 ~/.ssh/authorized_keys" -ForegroundColor Gray } } else { Write-Host "❌ SSH密钥配置失败" -ForegroundColor Red Write-Host "请手动执行以下步骤:" -ForegroundColor Yellow Write-Host "1. 复制公钥内容(见上方)" -ForegroundColor White Write-Host "2. SSH连接到服务器: ssh $ServerUser@$ServerIP" -ForegroundColor White Write-Host "3. 执行: mkdir -p ~/.ssh && chmod 700 ~/.ssh" -ForegroundColor White Write-Host "4. 将公钥内容追加到: ~/.ssh/authorized_keys" -ForegroundColor White Write-Host "5. 执行: chmod 600 ~/.ssh/authorized_keys" -ForegroundColor White }