Git Clone 如何指定自定义文件名的 SSH 私钥

目录

在使用 GitHub、Gitea 或自建 Git 服务时,通常推荐使用 SSH Key 进行身份认证。

SSH 私钥并不一定要使用默认的 ~/.ssh/id_rsa~/.ssh/id_ed25519 文件名。我们完全可以针对不同的 Git 服务生成不同的密钥,例如:

~/.ssh/gitea
~/.ssh/gitea.pub

其中:

  • gitea私钥,只保存在本机,不能泄露
  • gitea.pub公钥,可以添加到 Gitea、GitHub、GitLab 等服务器

需要注意:添加到 Git 服务器的是公钥,而 Git Clone 时本机实际用于认证的是对应的私钥。

一、生成 SSH 密钥对

目前推荐使用 Ed25519 算法生成 SSH Key。

首先确认 SSH 目录存在:

mkdir -p ~/.ssh
chmod 700 ~/.ssh

然后生成一套专门用于 Gitea 的密钥:

ssh-keygen -t ed25519 -C "gitea" -f ~/.ssh/gitea

参数含义:

-t ed25519       使用 Ed25519 算法
-C "gitea"       给公钥添加备注,方便识别
-f ~/.ssh/gitea  指定密钥文件名和保存位置

执行后会提示:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

可以根据需要设置私钥密码(passphrase)。如果设置了密码,每次使用私钥时需要解锁,也可以配合 ssh-agent 使用。

生成完成后会得到两个文件:

~/.ssh/gitea
~/.ssh/gitea.pub

其中:

~/.ssh/gitea

是​私钥​,必须妥善保存,不要上传到服务器、Git 仓库或者发送给其他人。

而:

~/.ssh/gitea.pub

是​公钥​,可以安全地添加到 Gitea。

查看公钥:

cat ~/.ssh/gitea.pub

输出类似:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIxxxxxxxxxxxxxxxxxxxxxxxx gitea

复制完整的一行,然后进入 Gitea 的 SSH Key 管理页面,将其添加到自己的账号即可。

如果同时使用多个 Git 服务,也可以分别生成:

ssh-keygen -t ed25519 -C "github" -f ~/.ssh/github
ssh-keygen -t ed25519 -C "gitea" -f ~/.ssh/gitea
ssh-keygen -t ed25519 -C "gitlab" -f ~/.ssh/gitlab

这样每个平台都有自己独立的 SSH Key,后续管理会更加清晰。

二、临时指定 SSH 私钥

如果只是偶尔使用某个私钥,可以通过 GIT_SSH_COMMAND 指定:

GIT_SSH_COMMAND="ssh -i ~/.ssh/gitea -o IdentitiesOnly=yes" \
git clone ssh://git@git.example.com:2222/user/project.git

其中:

-i ~/.ssh/gitea

表示指定 SSH 私钥。

而:

-o IdentitiesOnly=yes

表示只使用明确指定的 Identity,避免 SSH Agent 中存在多个 Key 时尝试错误的私钥。

如果服务器使用默认的 SSH 22 端口,也可以使用常见的 SCP 风格地址:

GIT_SSH_COMMAND="ssh -i ~/.ssh/gitea -o IdentitiesOnly=yes" \
git clone git@git.example.com:user/project.git

三、推荐方式:配置 ~/.ssh/config

如果经常访问这个 Git 服务,更推荐配置 SSH:

nano ~/.ssh/config

加入:

Host my-gitea
    HostName git.example.com
    Port 2222
    User git
    IdentityFile ~/.ssh/gitea
    IdentitiesOnly yes

这里使用了 my-gitea 作为 SSH 别名。

各配置项含义:

Host            SSH 主机别名
HostName        实际服务器域名或 IP
Port            SSH 端口
User            SSH 登录用户
IdentityFile    使用的私钥
IdentitiesOnly  只使用指定的 Identity

之后可以测试:

ssh -T my-gitea

如果服务器不支持 -T,也可以:

ssh my-gitea

四、使用配置后的地址 Clone

配置 ~/.ssh/config 后,不再需要每次指定私钥和端口。

例如原始地址:

ssh://git@git.example.com:2222/user/project.git

可以简化为:

git clone my-gitea:user/project.git

SSH 会自动将:

my-gitea

解析为:

HostName = git.example.com
Port = 2222
User = git
IdentityFile = ~/.ssh/gitea

这种方式尤其适合同时使用 GitHub、GitLab、Gitea 等多个 Git 服务的情况。

五、同时配置多个 Git 服务

例如 GitHub 和自己的 Gitea 分别使用不同的 SSH Key:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github
    IdentitiesOnly yes

Host my-gitea
    HostName git.example.com
    Port 2222
    User git
    IdentityFile ~/.ssh/gitea
    IdentitiesOnly yes

这样:

git clone git@github.com:user/project.git

会自动使用:

~/.ssh/github

而:

git clone my-gitea:user/project.git

会自动使用:

~/.ssh/gitea

多个 Git 服务之间不会互相影响。

六、检查 SSH 文件权限

SSH 对私钥文件权限要求比较严格,建议执行:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/gitea
chmod 644 ~/.ssh/gitea.pub

如果私钥权限过于宽松,SSH 可能直接拒绝使用该私钥。

七、不修改配置文件时如何测试

如果只是想确认私钥、服务器和端口是否正确,可以直接:

ssh \
    -i ~/.ssh/gitea \
    -o IdentitiesOnly=yes \
    -p 2222 \
    git@git.example.com

如果需要查看 SSH 到底选择了哪个 Key,可以增加 -v

ssh -v \
    -i ~/.ssh/gitea \
    -o IdentitiesOnly=yes \
    -p 2222 \
    git@git.example.com

重点观察类似:

Offering public key: ...
Server accepts key: ...
Authenticated to ...

的信息。

总结

整个配置流程实际上可以概括为:

生成 SSH 密钥对
       ↓
将 .pub 公钥添加到 Gitea
       ↓
私钥保留在本机 ~/.ssh/
       ↓
配置 ~/.ssh/config 指定私钥
       ↓
ssh 测试认证
       ↓
git clone

生成专用密钥:

ssh-keygen -t ed25519 -C "gitea" -f ~/.ssh/gitea

临时指定私钥:

GIT_SSH_COMMAND="ssh -i ~/.ssh/gitea -o IdentitiesOnly=yes" git clone ...

长期使用则推荐配置:

Host my-gitea
    HostName git.example.com
    Port 2222
    User git
    IdentityFile ~/.ssh/gitea
    IdentitiesOnly yes

之后直接:

git clone my-gitea:user/project.git

即可。

对于同时使用 GitHub、GitLab、Gitea 或多个不同账号的开发环境,建议​为不同服务生成独立的 SSH Key,并通过 ​~/.ssh/config​ 统一管理​。这样既避免了多个密钥之间的冲突,也方便后续迁移、吊销和维护。