Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 706
Benyamin Jafari Avatar asked Jan 14 '18 09:01

Benyamin Jafari


People also ask

How do I create a new SSH key?

Open a terminal and use the ssh-keygen command with the -C flag to create a new SSH key pair. Replace the following: KEY_FILENAME : the name for your SSH key file. For example, a filename of my-ssh-key generates a private key file named my-ssh-key and a public key file named my-ssh-key.

Can I have more than one SSH key for GitLab?

Gitlab won't allow reuse of a public ssh key for multiple accounts. To get around this you need to create a second ssh key for the second account.


1 Answers

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

ssh-keygen -t rsa -C "[email protected]" -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 [email protected]:<yourSecondACcount>/<yourRepo.git> and will use your second key.

like image 124
VonC Avatar answered Oct 15 '22 11:10

VonC