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

相关推荐
数据大魔方39 分钟前
【期货量化实战】螺纹钢量化交易指南:品种特性与策略实战(TqSdk完整方案)
python·算法·github·程序员创富·期货程序化·期货量化·交易策略实战
Cx330❀2 小时前
Git 多人协作全攻略:从入门到高效协同
大数据·elasticsearch·搜索引擎·gitee·github·全文检索·gitcode
CoderJia程序员甲4 小时前
GitHub 热榜项目 - 日榜(2026-1-9)
开源·大模型·llm·github·ai教程
lpfasd1238 小时前
系统、详细地介绍 GitHub 官方 API 的能力边界
github
KevinShi_BJ8 小时前
Github Copilot 实践
github·copilot
秋雨雁南飞9 小时前
图床软件 PicGo + Github
github·picgo·图床
moment&forever9 小时前
GitHub 托管 API 地址配置文件:实现零成本云配置托管
github
小龙20 小时前
【Git 报错解决】本地无有效提交无法推送(`src refspec main does not match any`)
git·github·报错
行百里er1 天前
一个还没写代码的开源项目,我先来“画个饼”:Spring Insight
后端·开源·github
知行力1 天前
【GitHub每日速递 20260108】告别云服务弊端,Memos隐私至上自托管笔记服务来袭!
笔记·github