docs(git): 添加远程仓库 SSH 配置与常见问题排查

- 记录 Gitea 服务器信息(SSH 用户名 sc-gitea,端口 2223)
- 添加 ED25519 密钥生成与配置步骤
- 记录实际遇到的认证问题及解决方案(用户名/端口/密钥格式)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-05-31 02:07:34 +08:00
parent 710cd45a48
commit bcc146584b
+103
View File
@@ -231,3 +231,106 @@ git reset --soft HEAD~1
git fetch origin
git reset --hard origin/main
```
---
## 五、远程仓库配置
### 5.1 服务器信息
| 项目 | 值 |
|---|---|
| 平台 | Gitea(自建) |
| HTTP 地址 | `http://192.168.1.22:8418/starmen1265/ChildHood.git` |
| SSH 地址 | `ssh://sc-gitea@192.168.1.22:2223/starmen1265/ChildHood.git` |
| SSH 端口 | `2223`(非默认 22) |
| SSH 用户名 | `sc-gitea`(非默认 `git`) |
### 5.2 SSH 配置
#### 生成密钥(ED25519,推荐)
```bash
ssh-keygen -t ed25519 -C "starmen1265@163.com" -f ~/.ssh/id_ed25519 -N ""
```
> ⚠️ 优先使用 ED25519 而非 RSA。部分 Git 服务器对 RSA 密钥兼容性较差。
#### 添加公钥到 Gitea
1. 查看公钥:`cat ~/.ssh/id_ed25519.pub`
2. 复制完整输出
3. 登录 Gitea Web UI → 设置 → SSH/GPG 密钥 → 添加密钥
4. 粘贴公钥内容,保存
#### 设置远程地址
```bash
# 查看当前远程地址
git remote get-url origin
# 切换到 SSH(注意用户名是 sc-gitea,端口是 2223)
git remote set-url origin ssh://sc-gitea@192.168.1.22:2223/starmen1265/ChildHood.git
```
### 5.3 常见问题排查
#### ❌ `Host key verification failed`
首次连接服务器需要信任主机密钥:
```bash
ssh-keyscan -p 2223 192.168.1.22 >> ~/.ssh/known_hosts
```
#### ❌ `Permission denied (publickey)`
排查步骤:
```bash
# 1. 测试 SSH 连接,查看详细日志
ssh -vvv -p 2223 sc-gitea@192.168.1.22 "exit"
# 2. 检查密钥是否被 offer
# 在日志中找 "Offering public key" 行
# 确认使用了 id_ed25519 而不是 id_rsa
# 3. 对比本地和服务器的密钥指纹
ssh-keygen -lf ~/.ssh/id_ed25519.pub
# 然后到 Gitea Web UI → 设置 → SSH/GPG 密钥 查看指纹是否一致
```
常见原因:
| 原因 | 解决方法 |
|---|---|
| 远程地址用了 `git@` 而非 `sc-gitea@` | 改用正确的用户名(见 5.1) |
| SSH 端口用了默认 22 而非 2223 | URL 中指定端口 `ssh://sc-gitea@host:2223/...` |
| RSA 密钥被服务器拒绝 | 重新生成 ED25519 密钥 |
| 公钥未添加到 Gitea | Web UI → 设置 → SSH/GPG 密钥 → 添加 |
| 公钥格式不完整 | 确保从 `ssh-ed25519` 复制到字符串末尾,无截断 |
#### ❌ `Authentication failed`(HTTP 方式)
HTTP 方式需要用户名密码认证,建议切换到 SSH:
```bash
git remote set-url origin ssh://sc-gitea@192.168.1.22:2223/starmen1265/ChildHood.git
```
#### 🔧 SSH 使用了错误的密钥
如果服务器同时有 RSA 和 ED25519 密钥,SSH 可能优先尝试 RSA。可以指定密钥:
```bash
# 单次指定
git -c core.sshCommand="ssh -i ~/.ssh/id_ed25519 -p 2223" push origin main
# 或全局配置(推荐)
# 编辑 ~/.ssh/config,添加:
Host 192.168.1.22
Port 2223
User sc-gitea
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
```