Search code examples
gitgitlabssh-keys

How do I generate a new ssh-key for my new gitlab account?


I have two Gitlab accounts. On my old account I added an ssh-key that is located in ~/.ssh/id_rsa.pub on my computer.

Now I want to add another ssh-key for my new Gitlab account. How do I do this without having the ssh-keys conflict?


Solution

  • I would recommend a second key, for now without passphrase:

    ssh-keygen -t rsa -C "your_email@example.com" -P "" -q -f ~/.ssh/gitlab_rsa
    

    That will create (without any prompt) ~/.ssh/gitlab_rsa (private key) and ~/.ssh/gitlab_rsa.pub (public key)

    You need to register that second gitlab_rsa.pub public key to your second GitLab account.

    Navigate to the 'SSH Keys' tab in your 'Profile Settings'. Paste your key in the 'Key' section and give it a relevant 'Title'.

    Then add a ~/.ssh/config file with:

    Host gitlab_rsa
        HostName gitlab.com
        User git
        PreferredAuthentications publickey
        IdentityFile /home/<you>/.ssh/gitlab_rsa
    

    Finally, you can clone any GitLab repo as your second identity with:

    git clone gitlab_rsa:<yourSecondAccount>/<yourRepo.git>
    

    That will be replaced automatically with git@gitlab.com:<yourSecondACcount>/<yourRepo.git> and will use your second key.