GitHub 与 Gitee 的 SSH 配置完整指南

By Mige - 九月 23, 2026

GitHub 与 Gitee 的 SSH 配置完整指南
很多开发者需要同时使用 GitHub 和 Gitee,SSH 配置的关键在于多 SSH Key 管理。下面从零开始,覆盖单账号和多账号两种场景。
一、SSH 基本原理
SSH Key 是一对密钥:
私钥(  id_ed25519  ):保存在本地,相当于你的身份证明,绝不能泄露
公钥(  id_ed25519.pub  ):添加到 GitHub/Gitee 账号中
当你执行  git push  时,Git 用私钥签名请求,服务器用公钥验证身份。
二、生成 SSH Key2.1 检查是否已有 SSH Key
ls -la ~/.ssh/
如果看到  id_ed25519  和  id_ed25519.pub  ,说明已有密钥,可以跳过生成步骤。
2.2 生成新的 SSH Key
推荐使用 Ed25519 算法(比 RSA 更安全、更短):
ssh-keygen -t ed25519 -C "your_email@example.com"
如果系统不支持 Ed25519(极少见),用 RSA:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
交互过程:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/you/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
文件路径:直接回车使用默认路径
密码短语:建议设置(可选),每次使用 SSH 时需要输入,增加安全性
2.3 查看公钥内容
cat ~/.ssh/id_ed25519.pub
输出类似:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDdX... your_email@example.com
复制这段内容,准备添加到 GitHub 和 Gitee。
三、单 SSH Key 方案(最简单)
如果你只用一个邮箱,且 GitHub 和 Gitee 使用同一个 SSH Key,这是最简单的方案。
3.1 将公钥添加到 GitHub
登录 GitHub →右上角头像 → Settings2. 左侧菜单 → SSH and GPG keys
点击「New SSH key」
标题:填(如 "My Laptop")
密钥类型:Authentication Key
粘贴公钥内容
点击「Add SSH key」
3.2 将公钥添加到 Gitee
登录 Gitee → 右上角头像 → 设置
左侧菜单 → 安全设置 → SSH 公钥
点击「添加公钥」
标题:填(如 "我的电脑")
粘贴公钥内容
点击「确定」
3.3 测试连接
测试 GitHub:
ssh -T git@github.com
成功输出:
Hi 用户名! You've successfully authenticated, but GitHub does not provide shell access.
测试 Gitee:
ssh -T git@gitee.com
成功输出:
Hi 用户名! You've successfully authenticated, but GITEE.COM does not provide shell access.
3.4 配置 Git 用户信息
虽然 SSH 配置好了,但 Git 提交时还需要用户信息:
全局统一配置(如果 GitHub 和 Gitee 用同一个身份):
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
或者按仓库单独配置:
cd my-project
git config user.name "Your Name"
git config user.email "your_email@example.com"
四、多 SSH Key 方案(推荐)
如果你有以下任一情况,需要用多 Key 方案:
GitHub 和 Gitee 使用不同的邮箱
有个人账号和工作账号
想为不同平台使用不同的密钥
4.1 生成多个 SSH Key
为 GitHub 生成一个密钥:
ssh-keygen -t ed25519 -C "github_email@example.com" -f ~/.ssh/id_ed25519_github
为 Gitee 生成一个密钥:
ssh-keygen -t ed25519 -C "gitee_email@example.com" -f ~/.ssh/id_ed25519_gitee
参数说明:
 -C  :注释,通常填邮箱,方便识别
 -f  :指定文件名,不指定则用默认的  id_ed25519  
4.2 将私钥添加到 SSH Agent
启动 SSH Agent:
eval "$(ssh-agent -s)"
添加私钥:
ssh-add ~/.ssh/id_ed25519_github
ssh-add ~/.ssh/id_ed25519_gitee
查看已添加的密钥:
ssh-add -l
4.3 配置 SSH Config 文件
这是多 Key 方案的核心。创建或编辑  ~/.ssh/config  :
touch ~/.ssh/config
chmod 600 ~/.ssh/config
编辑内容:
GitHub 配置
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
Gitee 配置
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/id_ed25519_gitee
IdentitiesOnly yes
个人 GitHub 账号
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
工作 GitHub 账号
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
参数说明:
参数 作用
Host  连接别名,可以自定义
HostName  实际服务器地址
User  固定为 git
IdentityFile  指定使用的私钥文件
IdentitiesOnly yes 只使用指定的密钥,不尝试其他密钥


4.4 测试多 Key 配置
测试 GitHub:
ssh -T git@github.com
测试 Gitee:
ssh -T git@gitee.com
测试自定义 Host:
ssh -T git@github-personal
ssh -T git@github-work
4.5 使用自定义 Host 克隆仓库
如果配置了自定义 Host,克隆时要用别名替换 github.com:
git clone git@github-personal:用户名/仓库名.git
git clone git@github-work:用户名/仓库名.git
五、将公钥添加到平台
5.1 查看各公钥内容
cat ~/.ssh/id_ed25519_github.pub
cat ~/.ssh/id_ed25519_gitee.pub
5.2 添加到 GitHub
登录 GitHub → Settings → SSH and GPG keys
点击「New SSH key」
标题:填 "GitHub Key"
粘贴  id_ed25519_github.pub  的内容
点击「Add SSH key」
5.3 添加到 Gitee
登录 Gitee → 设置 → 安全设置 → SSH 公钥
点击「添加公钥」
标题:填 "Gitee Key"
粘贴  id_ed25519_gitee.pub  的内容
点击「确定」
六、完整的多账号配置示例
场景:同时使用 GitHub(个人)、GitHub(工作)、Gitee
6.1 生成三个密钥
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_personal
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "gitee@example.com" -f ~/.ssh/id_ed25519_gitee
6.2 SSH Config 配置
~/.ssh/config:
个人 GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
工作 GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
Gitee
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/id_ed25519_gitee
IdentitiesOnly yes
6.3 按仓库配置 Git 用户信息
个人项目:
cd ~/projects/personal-project
git init
git config user.name "个人名字"
git config user.email "personal@example.com"
git remote add origin git@github.com:个人用户名/项目.git
工作项目:
cd ~/projects/work-project
git init
git config user.name "工作名字"
git config user.email "work@company.com"
git remote add origin git@github-work:公司名/项目.git
Gitee 项目:
cd ~/projects/gitee-project
git init
git config user.name "Gitee名字"
git config user.email "gitee@example.com"
git remote add origin git@gitee.com:用户名/项目.git
七、常见问题排查
7.1 测试连接失败
ssh -T git@github.com
错误:Permission denied (publickey)
排查步骤:
检查密钥是否存在:
ls -la ~/.ssh/
检查 SSH Agent 是否运行并添加了密钥:
eval "$(ssh-agent -s)"
ssh-add -l
检查 config 文件权限:
chmod 600 ~/.ssh/config
详细调试模式:
ssh -vT git@github.com
7.2 连接超时
ssh -T git@github.com
错误:Connection timed out
原因:可能是网络问题,GitHub 需要代理。
解决方案:
配置 SSH 走代理(~/.ssh/config):
GitHub 走 HTTP 代理
Host github.com
HostName github.com
User git
ProxyCommand connect -H 127.0.0.1:7890 %h %p
IdentityFile ~/.ssh/id_ed25519_github
GitHub 走 SOCKS5 代理
Host github.com
HostName github.com
User git
ProxyCommand nc -x 127.0.0.1:1080 %h %p
IdentityFile ~/.ssh/id_ed25519_github
7.3 多个密钥时用了错误的密钥
ssh -T git@github.com
错误:ERROR: Permission to username/repo denied to other-username
原因:SSH 用了错误的密钥。
解决方案:
在 config 中加上 IdentitiesOnly yes:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes  # 只使用指定的密钥
7.4 每次都要输入密码
如果生成密钥时设置了 passphrase,每次使用 SSH 都要输入。
解决方案一:添加到 SSH Agent(一次输入,会话内有效):
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_github
解决方案二:配置 macOS Keychain(macOS 永久保存):
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github
解决方案三:重新生成不带 passphrase 的密钥(不推荐):
ssh-keygen -t ed25519 -C "email@example.com" -N "" -f ~/.ssh/id_ed25519_github
八、安全最佳实践
私钥权限必须为 600
chmod 600 ~/.ssh/id_ed25519*
公钥可以公开,私钥绝不能泄露
定期更换密钥(建议每 6-12 个月)
不同平台使用不同密钥
设置 passphrase 增加安全性
不再使用的密钥及时从平台删除7. 开启 GitHub/Gitee 的两步验证(2FA)
九、一键配置脚本
创建一个  setup-ssh.sh  脚本,一键完成配置:
!/bin/bash
SSH 配置脚本echo "🚀 开始配置 SSH..."
创建 .ssh 目录
mkdir -p ~/.ssh
chmod 700 ~/.ssh
生成 GitHub密钥
echo "生成 GitHub SSH Key..."
ssh-keygen -t ed25519 -C "$1" -f ~/.ssh/ided25519github -N "" 2>/dev/null
生成 Gitee 密钥
echo "生成 Gitee SSH Key..."
ssh-keygen -t ed25519 -C "$1" -f ~/.ssh/ided25519gitee -N "" 2>/dev/null
配置 SSH Config
cat > ~/.ssh/config << EOF
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/ided25519github
IdentitiesOnly yes
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/ided25519gitee
IdentitiesOnly yes
EOF
chmod 600 ~/.ssh/config
启动 SSH Agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/ided25519github
ssh-add ~/.ssh/ided25519gitee
显示公钥
echo ""
echo "=== GitHub 公钥(添加到 GitHub)==="
cat ~/.ssh/ided25519github.pub
echo ""
echo "=== Gitee 公钥(添加到 Gitee)==="
cat ~/.ssh/ided25519gitee.pub
echo ""
echo "✅ 配置完成!请将上面的公钥添加到对应平台。"
echo "运行测试: ssh -T git@github.com"
echo "运行测试: ssh -T git@gitee.com"
使用方法:
chmod +x setup-ssh.sh
./setup-ssh.sh your_email@example.com
十、总结

场景 方案 关键配置
单平台单账号 默认密钥 无需配置
双平台各一个账号 两个密钥 + config ~/.ssh/config区分 Host
多平台多账号 多个密钥 + config 自定义 Host 别名
个人+工作 GitHub 两个密钥 + config 不同 Host 指向 github.com


核心要点:
生成密钥:  ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_平台名  
添加代理:  ssh-add ~/.ssh/id_ed25519_平台名  
配置映射:  ~/.ssh/config  中指定 Host 对应的密钥
测试连接:  ssh -T git@github.com  和  ssh -T git@gitee.com  
配置好之后,Git 会自动根据你连接的域名选择正确的密钥,完全无感切换。

关注订阅我的公众号: 铭哥9369的散文与随笔
资源搜索: @lovouchat333  TG频道:@MFDYTG 免费、公益、订阅
欢迎投稿(技术分享、原创教程)至本站机器人: blog#mige.eu.org

AD友链:①枫铃网 ②网站导航 ③O520 ④情兽文学 ⑤学习Hexo ⑥在线网盘 ⑦在线图床 ⑧免费空间 ⑨枫铃社区 ⑩X-admin系统



  • Share:

You Might Also Like

0 comments