
# 在一台电脑上，如何配置多个SSH Key？
#### 场景描述
开发人员通常只会生成一个SSH Key，名字叫**id_rsa**，然后提交到多个不同的网站（如：GitHub、CodeArts或Gitee）。
但是也存在另一种需要，在同一个网站上，注册了两个用户名，通常网站不会允许为这两个用户名，配置同一个SSH Key，这时候就会有些麻烦。
#### 操作步骤
1. 在本地Git仓库生成两个不同的SSH Key。 
   ```
   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):<不要直接回车，填写自己定义的密码>
   ```
   ![](https://support.huaweicloud.com/codeartsrepo_faq/public_sys-resources/note_3.0-zh-cn.png)
   这是第一个关键，如果要生成2个Key，这里写成：**github_1** 和**github_2** ，如果是访问CodeArts，可以写成**dc_1** 和**dc_2**。这样，就生成了2个证书。
   
   

2. 用不同的账号，上传两个不同的证书。 
   读取\*.pub的内容，粘贴到服务网站上。记住对应的用户名。
   
   

3. 编辑**\~/.ssh/config** 文件。
   
   ```
   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：填写的是证书的所在位置，您也可以把证书保存在任何地方。
    
   
   

4. 读写代码。 
   在Web页面上复制的SSH URL，可以直接使用，例如：
   ```
   git@example.com:name/repo.git
   ```
   现在根据您的账号不同，需要自行替换：
   ```
   git@dc_1:name/repo.git 或 git@dc_2:name/repo.git
   ```
   
   
 
