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

相关推荐
阿里嘎多学长1 小时前
2026-06-13 GitHub 热点项目精选
开发语言·程序员·github·代码托管
洛星核2 小时前
CrewAI 安装、使用方法详细全解
人工智能·github·人机交互·ai编程·agi·智能体
火山上的企鹅3 小时前
Codex实战:APP远程升级服务搭建(四)Node 服务端自动识别 APK 信息
android·服务器·git·github·qgc
Dontla6 小时前
gh CLI(GitHub CLI)安装教程(Github Command Line)
github
Dontla6 小时前
CI/CD前世今生(持续集成、持续交付、持续部署、Jenkins、Github Actions)
ci/cd·github·jenkins
委婉待续6 小时前
登录github出现ERR_CONNECTION_TIMED_OUT问题
github
洛星核7 小时前
Aider 安装、使用方法详细全解
人工智能·github·人机交互·ai编程·agi
法欧特斯卡雷特7 小时前
从 Kotlin 编译器 API 的变化开始: 2.4.0
android·开源·github
Hical617 小时前
百万 TCP 长连接内存实测:50 万点回归,R²=1.0000,每连接 7.58 KB
后端·github
uhakadotcom7 小时前
get_event_loop(),和 get_running_loop() + ThreadPoolExecutor 有啥区别
后端·面试·github