文档首页> 代码托管 CodeArts Repo> 常见问题> 仓库管理> 在一台电脑上,如何配置多个SSH Key?
更新时间:2023-05-11 GMT+08:00
分享

在一台电脑上,如何配置多个SSH Key?

场景描述

开发人员通常只会生成一个SSH Key,名字叫id_rsa,然后提交到多个不同的网站(如:GitHub、CodeArts或Gitee)。

但是也存在另一种需要,在同一个网站上,注册了两个用户名,通常网站不会允许为这两个用户名,配置同一个SSH Key,这时候就会有些麻烦。

操作步骤

  1. 在本地Git仓库生成两个不同的SSH Key。

    1
    2
    3
    4
    ssh-keygen -t rsa -C "email"
    Generating public/private rsa key pair.
    Enter file in which to save the key (~/.ssh/id_rsa):<不要直接回车填写自己定义的名字>
    Enter passphrase(empty for no passphrase):<不要直接回车填写自己定义的密码>
    

    这是第一个关键,如果要生成2个Key,这里写成:github_1github_2,如果是访问CodeArts,可以写成dc_1dc_2。这样,就生成了2个证书。

  1. 用不同的帐号,上传两个不同的证书。

    读取*.pub的内容,粘贴到服务网站上。记住对应的用户名。

  1. 编辑~/.ssh/config文件。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    Host dc_1
    HostName *******.com
    IdentityFile ~/.ssh/dc_1
    PreferredAuthentications publickey
    User username1
    Host dc_2
    HostName *******.com
    IdentityFile ~/.ssh/dc_2
    PreferredAuthentications publickey
    User username2
    
    要点在于Host与HostName的区别:
    • HostName:是填写真实的服务地址。
    • Host:是填写别名,后面会用上。
    • IdentityFile:填写的是证书的所在位置,你也可以把证书保存在任何地方。

  1. 读写代码。

    原本在Web页面上复制的SSH URL,可以直接使用,例如:

    1
    git@*****.com:name/repo.git
    

    但是,现在根据你的帐号不同,需要自行替换:

    1
    git@dc_1:name/repo.git  git@dc_2:name/repo.git
    

分享:

仓库管理 所有常见问题

more