GitLab/GitHub 多环境配置SSH密钥

需求:

  1. 代码有多个仓库,需要配置不同的密钥。
  1. 生成密钥(有的可以跳过)
shell 复制代码
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_custom_name
  • -t rsa 指定使用 RSA 算法。
  • -b 4096 指定密钥长度为 4096 位(推荐)。
  • -f ~/.ssh/id_rsa_custom_name: 指定生成的密钥文件的路径和名称。 强烈推荐自定义名称

除了RSA算法,ED25519ECDSA 也是常见的算法,可以根据平台自行选择。

  1. 多环境配置匹配密钥

创建 SSH 配置文件:touch ~/.ssh/config

根据环境配置

复制代码
Host your_host_name
    HostName your_host_url
    User git
    IdentityFile ~/.ssh/id_rsa_custom_name
  • Host: 设置匹配的主机名。
  • HostName: 实际连接的主机地址。
  • IdentityFile: 指定使用的 RSA 私钥路径。

备注:一个文件可以配置多个,用 Host分隔。

  1. 添加公钥到平台

公钥文件 以 pub 为结尾,例如 id_rsa_custom_name_pub,复制公钥到 GitLab/GitHub,保存。

  1. 验证连接

ssh -T user@host

相关推荐
逛逛GitHub1 天前
1 天狂揽 4000 多 Star 的 AI 舆情分析开源神器。
github
卡洛斯(编程版1 天前
大仓库推到GitHub大踩坑-Git LFS从安装到使用
git·github
玥轩_5211 天前
Git命令速查手册
大数据·git·elasticsearch·gitee·github·命令速查
吃饺子不吃馅1 天前
⚡️ Zustand 撤销重做利器:Zundo 实现原理深度解析
前端·javascript·github
whysqwhw1 天前
KMP类GSON转字符串工具
github
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2025-11-06)
ai·开源·大模型·github·ai教程
诸葛思颖1 天前
把本地 Python 项目用 Git 进行版本控制并推送到 GitHub
git·python·github
chhanz2 天前
git/github入门基操(终端版)
git·github
Bacon2 天前
Electron 应用商店:开箱即用工具集成方案
前端·github
掘金安东尼2 天前
GPT-6 会带来科学革命?奥特曼最新设想:AI CEO、便宜医疗与全新计算机
前端·vue.js·github