Help Center> CodeArts Repo> FAQs> Managing Repositories> How Can I Set Multiple SSH Keys on My Computer?
Updated on 2023-06-06 GMT+08:00

How Can I Set Multiple SSH Keys on My Computer?

Scenario

Developers generate only one public SSH key named id_rsa and submit it to multiple websites.

However, one public SSH key cannot be shared by two accounts in a website. In this case, you need to generate two SSH keys.

Procedure

  1. Generate two different SSH keys in the local Git repository.

    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):< Type two file names before pressing Enter. >
    Enter passphrase(empty for no passphrase):<Enter your custom password before pressing Enter.
    

    To generate two SSH keys, name two different file names github_1 and github_2.

  1. Upload the two keys to the two accounts respectively.

    Print the content of the *.pub file and paste it to the service website. Take note of the two usernames and their mappings to the keys.

  1. Edit the ~/.ssh/config file.

     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
    
    Note that Host and HostName should be set to different values.
    • HostName: Enter the service address.
    • Host: Enter an alias for each key. The aliases will be used when you access the service.
    • IdentityFile: Enter the location of each SSH key file. SSH key files can be stored in any directory you want.

  1. Read and write code.

    In cases where only one SSH key is set, the SSH URL obtained from the service web page can be directly used to communicate with the service.

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

    However, with two SSH keys on your computer, you need to replace the service address in the URL with a corresponding alias configured previously based on which account you are using.

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

Managing Repositories FAQs

more