196 lines
4.1 KiB
Markdown
196 lines
4.1 KiB
Markdown
|
|
# Gerrit SSH 密钥配置指南
|
|||
|
|
|
|||
|
|
## 在 Windows 上配置 SSH 密钥
|
|||
|
|
|
|||
|
|
### 步骤 1:生成 SSH 密钥(如果还没有)
|
|||
|
|
|
|||
|
|
在 Git Bash 中执行:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 检查是否已有 SSH 密钥
|
|||
|
|
ls -la ~/.ssh
|
|||
|
|
|
|||
|
|
# 如果没有 id_rsa 或 id_ed25519,生成新密钥
|
|||
|
|
ssh-keygen -t ed25519 -C "your_email@example.com"
|
|||
|
|
|
|||
|
|
# 或者使用 RSA(如果 ed25519 不支持)
|
|||
|
|
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**提示**:
|
|||
|
|
- 按回车使用默认路径:`/c/Users/Administrator/.ssh/id_ed25519`
|
|||
|
|
- 可以设置密码(推荐)或直接回车留空
|
|||
|
|
|
|||
|
|
### 步骤 2:查看公钥内容
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 查看公钥(ed25519)
|
|||
|
|
cat ~/.ssh/id_ed25519.pub
|
|||
|
|
|
|||
|
|
# 或(RSA)
|
|||
|
|
cat ~/.ssh/id_rsa.pub
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**复制整个公钥内容**(从 `ssh-ed25519` 或 `ssh-rsa` 开始到邮箱结束)
|
|||
|
|
|
|||
|
|
### 步骤 3:将公钥添加到 Gerrit
|
|||
|
|
|
|||
|
|
1. **登录 Gerrit**
|
|||
|
|
- 访问:`http://101.43.95.130:8080`
|
|||
|
|
- 使用您的账号登录
|
|||
|
|
|
|||
|
|
2. **进入 SSH 公钥设置**
|
|||
|
|
- 点击右上角用户名 → "Settings"
|
|||
|
|
- 或直接访问:`http://101.43.95.130:8080/#/settings/ssh-keys`
|
|||
|
|
|
|||
|
|
3. **添加 SSH 公钥**
|
|||
|
|
- 点击 "Add Key" 或 "+" 按钮
|
|||
|
|
- 粘贴刚才复制的公钥内容
|
|||
|
|
- 点击 "Add" 保存
|
|||
|
|
|
|||
|
|
### 步骤 4:修复 known_hosts(删除旧密钥)
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 删除旧的 Gerrit 主机密钥
|
|||
|
|
ssh-keygen -R [101.43.95.130]:29418
|
|||
|
|
|
|||
|
|
# 或者手动编辑 known_hosts 文件
|
|||
|
|
# 删除包含 101.43.95.130:29418 的行
|
|||
|
|
notepad ~/.ssh/known_hosts
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 5:测试 SSH 连接
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 测试 SSH 连接(会提示接受新密钥)
|
|||
|
|
ssh -p 29418 admin@101.43.95.130
|
|||
|
|
|
|||
|
|
# 输入 yes 接受新的主机密钥
|
|||
|
|
# 如果配置正确,应该能看到 Gerrit 命令提示符
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 6:配置 Git 使用 SSH
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
cd /d/zhini/zhini_im
|
|||
|
|
|
|||
|
|
# 删除旧的 HTTP 远程仓库
|
|||
|
|
git remote remove gerrit
|
|||
|
|
|
|||
|
|
# 添加 SSH 远程仓库
|
|||
|
|
git remote add gerrit ssh://admin@101.43.95.130:29418/zhini_im
|
|||
|
|
|
|||
|
|
# 验证
|
|||
|
|
git remote -v
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 7:推送代码
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 推送代码
|
|||
|
|
git push gerrit HEAD:refs/heads/master
|
|||
|
|
|
|||
|
|
# 或推送到评审队列
|
|||
|
|
git push gerrit HEAD:refs/for/master
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 完整操作流程
|
|||
|
|
|
|||
|
|
### 在 Git Bash 中执行:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 1. 生成 SSH 密钥(如果还没有)
|
|||
|
|
ssh-keygen -t ed25519 -C "admin@example.com"
|
|||
|
|
# 按回车使用默认路径和密码
|
|||
|
|
|
|||
|
|
# 2. 查看公钥
|
|||
|
|
cat ~/.ssh/id_ed25519.pub
|
|||
|
|
|
|||
|
|
# 3. 复制公钥内容,然后:
|
|||
|
|
# - 访问 http://101.43.95.130:8080/#/settings/ssh-keys
|
|||
|
|
# - 点击 "Add Key"
|
|||
|
|
# - 粘贴公钥
|
|||
|
|
# - 保存
|
|||
|
|
|
|||
|
|
# 4. 删除旧的 known_hosts 条目
|
|||
|
|
ssh-keygen -R [101.43.95.130]:29418
|
|||
|
|
|
|||
|
|
# 5. 测试 SSH 连接
|
|||
|
|
ssh -p 29418 admin@101.43.95.130
|
|||
|
|
# 输入 yes 接受新密钥
|
|||
|
|
# 如果成功,输入 exit 退出
|
|||
|
|
|
|||
|
|
# 6. 配置 Git 远程仓库
|
|||
|
|
cd /d/zhini/zhini_im
|
|||
|
|
git remote remove gerrit
|
|||
|
|
git remote add gerrit ssh://admin@101.43.95.130:29418/zhini_im
|
|||
|
|
|
|||
|
|
# 7. 推送代码
|
|||
|
|
git push gerrit HEAD:refs/heads/master
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 常见问题
|
|||
|
|
|
|||
|
|
### Q: 提示 "Permission denied (publickey)"?
|
|||
|
|
|
|||
|
|
**A:** 可能的原因:
|
|||
|
|
1. 公钥未添加到 Gerrit → 检查 Settings → SSH Keys
|
|||
|
|
2. 用户名错误 → 使用注册时的用户名
|
|||
|
|
3. 密钥路径不对 → 检查 `~/.ssh/` 目录
|
|||
|
|
|
|||
|
|
### Q: 提示 "Host key verification failed"?
|
|||
|
|
|
|||
|
|
**A:** 执行:
|
|||
|
|
```bash
|
|||
|
|
ssh-keygen -R [101.43.95.130]:29418
|
|||
|
|
ssh -p 29418 admin@101.43.95.130 # 接受新密钥
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Q: 如何查看已添加的 SSH 密钥?
|
|||
|
|
|
|||
|
|
**A:** 访问:`http://101.43.95.130:8080/#/settings/ssh-keys`
|
|||
|
|
|
|||
|
|
### Q: 如何测试 SSH 连接?
|
|||
|
|
|
|||
|
|
**A:**
|
|||
|
|
```bash
|
|||
|
|
ssh -p 29418 admin@101.43.95.130 gerrit version
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
如果配置正确,应该显示 Gerrit 版本信息。
|
|||
|
|
|
|||
|
|
## SSH vs HTTP 对比
|
|||
|
|
|
|||
|
|
### SSH 方式
|
|||
|
|
- ✅ 更安全
|
|||
|
|
- ✅ 不需要每次输入密码
|
|||
|
|
- ✅ 支持更多 Gerrit 命令
|
|||
|
|
- ❌ 需要配置密钥
|
|||
|
|
|
|||
|
|
### HTTP 方式
|
|||
|
|
- ✅ 配置简单
|
|||
|
|
- ✅ 不需要密钥
|
|||
|
|
- ❌ 每次推送可能需要认证(开发模式下不需要)
|
|||
|
|
|
|||
|
|
## 验证配置
|
|||
|
|
|
|||
|
|
### 测试 SSH 连接
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 测试连接
|
|||
|
|
ssh -p 29418 admin@101.43.95.130 gerrit version
|
|||
|
|
|
|||
|
|
# 应该显示:
|
|||
|
|
# gerrit version 3.9.0
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 测试 Git 推送
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
cd /d/zhini/zhini_im
|
|||
|
|
git push gerrit HEAD:refs/heads/master
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
如果成功,应该能看到推送进度和成功消息。
|
|||
|
|
|