如何为我的新 gitlab 帐户生成新的 ssh-key?

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

我有两个 Gitlab 帐户。在我的 旧帐户 上,我添加了一个 ssh-key,它位于我计算机上的 ~/.ssh/id_rsa.pub 中。

现在我想为我的新 Gitlab 帐户添加另一个 ssh-key。如何在没有 ssh-keys 冲突的情况下执行此操作?

生成新的密钥对:

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

它会要求您输入密钥文件的名称:

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

选择不同的内容,例如 /Users/you/.ssh/gitlab_rsa

然后当您需要它时,将此密钥添加到您的 ssh-agent 中:

ssh-add ~/.ssh/gitlab_rsa

如果您想要永久访问权限,您可以编辑您的 ~/.ssh/config 文件:

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

有关详细信息,请参阅此 article

我会推荐第二个密钥,目前没有密码:

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

这将创建(没有任何提示)~/.ssh/gitlab_rsa(私钥)和~/.ssh/gitlab_rsa.pub(public 密钥)

你需要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'.

然后添加一个 ~/.ssh/config 文件:

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

最后,您可以克隆任何 GitLab 存储库作为您的第二个身份:

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

它将自动替换为 git@gitlab.com:<yourSecondACcount>/<yourRepo.git> 并将使用您的第二个密钥。

您需要创建文件 ~/.ssh/config 来定义每个域应该使用哪个密钥。

使用 nano 创建该文件并粘贴您的配置:

nano ~/.ssh/config

并添加:

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

请按照以下步骤生成 SSH。

在你的机器上打开 Git Bash

输入以下命令生成

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

正在生成 public/private rsa 密钥对。 输入保存密钥的文件(/c/Users/you/.ssh/id_rsa):按回车键 Enter passphrase (empty for no passphrase): 输入密码 再次输入相同的密码:再次输入密码

一旦输入确认密码,将收到确认消息。

转到 gitpair.pub 文件位置并右键单击用记事本打开。 复制代码并通过下面的文本框,您的电子邮件将在标题框中自动选择。 然后点击添加密钥。

只需应用两个命令:

ssh-keygen-t rsa -b 4096

cat ~/.ssh/id_rsa.pub

复制 ssh 密钥并粘贴到浏览器