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

你说的曾经没有我的故事 提交于 2019-12-29 04:55:20

问题


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?


回答1:


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.




回答2:


You need to create the file ~/.ssh/config to define which key should use for every domain.

Create that file with nano and paste your configuration:

nano ~/.ssh/config

And add:

Host your-gitlab.com
    HostName your-gitlab.com
    IdentityFile ~/.ssh/your-gitlab-privkey



回答3:


Generate a new key pair with:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

It will ask you to enter a name for the key file:

 Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]

Choose something different such as /Users/you/.ssh/gitlab_rsa

Then when you need it add this key to your ssh-agent with:

ssh-add ~/.ssh/gitlab_rsa

If you want a permanent access you can edit your ~/.ssh/config file with:

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

Refer to this article for further details.




回答4:


Generate SSH please follow below steps.

Open Git Bash on you machine

Enter the below command to genarate

ssh-keygen -t rsa -b 4096 -C "yourmail@example.com"

Generating public/private rsa key pair. Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):Press enter Enter passphrase (empty for no passphrase): Type a passphrase Enter same passphrase again: Type passphrase again

Once enter the confirm passphrase, will get confirmation message.

go to the gitpair.pub file location and right click open with notepad. copy the code and past the in the below text box, your email will pick automatically in the title box. then click add key.



来源:https://stackoverflow.com/questions/48248144/how-do-i-generate-new-ssh-key-for-my-new-gitlab-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!