我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理、Git管理和同一服务器中的正常Git使用)。我尝试简单地将密钥堆叠在id_rsa文件中,但没有用。

显然,要做到这一点,一个简单的方法是使用命令

ssh -i <key location> login@server.example.com 

这很麻烦。

有什么建议可以让你做这件事更容易一些吗?


当前回答

前面的回答已经正确地解释了如何创建一个配置文件来管理多个ssh密钥。我认为,还需要解释的重要事情是在克隆存储库时用别名替换主机名。

假设,您公司的GitHub帐户的用户名是abc1234。 假设你的个人GitHub账户的用户名是jack1234

并且,假设您已经创建了两个RSA密钥,即id_rsa_company和id_rsa_personal。因此,您的配置文件将如下所示:

# Company account
Host company
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_company

# Personal account
Host personal
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal

现在,当你从公司的GitHub帐户克隆存储库(命名为demo)时,存储库URL将类似于:

Repo URL: git@github.com:abc1234/demo.git

现在,在做git克隆时,你应该把上面的存储库URL修改为:

git@company:abc1234/demo.git

注意github.com现在是如何被我们在配置文件中定义的别名“company”所取代的。

同样,您必须根据配置文件中提供的别名修改个人帐户中存储库的克隆URL。

其他回答

我喜欢在~/.ssh/config文件中设置以下方法:

# Configuration for GitHub to support multiple GitHub  keys
Host  github.com
  HostName github.com
  User git

# UseKeychain adds each keys passphrase to the keychain so you
# don't have to enter the passphrase each time.
  UseKeychain yes

# AddKeysToAgent would add the key to the agent whenever it is
# used, which might lead to debugging confusion since then
# sometimes the one repository works and sometimes the
# other depending on which key is used first.
#  AddKeysToAgent yes

# I only use my private id file so all private
# repositories don't need the environment variable
# `GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa"` to be set.
  IdentityFile ~/.ssh/id_rsa

然后在你的存储库中创建一个.env文件,其中包含要使用的ssh命令:

GIT_SSH_COMMAND="ssh -i ~/.ssh/your_ssh_key"

如果你使用dotenv,环境变量会自动导出,你可以为每个项目/目录指定你想要的键。口令只被请求一次,因为它被添加到密钥链中。

这个解决方案与Git一起完美地工作,并且设计为在Mac上工作(由于UseKeychain)。

您可以尝试这个sshmulti npm包来维护多个SSH密钥。

这是我从sajib-khan的回答中得到的灵感。没有设置默认配置;这是我在GitLab上的个人账号,另一个是我的公司账号。以下是我所做的:

生成SSH密钥

ssh-keygen -t rsa -f ~/.ssh/company -C "name.surname@company.com"

编辑SSH配置

nano ~/.ssh/config
    Host company.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/company

删除缓存的SSH密钥

ssh-add -D

测试它!

ssh -T git@company.gitlab.com

欢迎来到GitLab, @hugo.sohm!

ssh -T git@gitlab.com

欢迎你的芳名…

使用它!

公司账户

git clone git@company.gitlab.com:group/project.git

个人/默认账户

git clone git@gitlab.com:username/project.git

这是我使用的源代码。

对我来说,在MacOs上,唯一可行的解决方案就是在文件~/.ssh/config中添加这个:

Host *
  IdentityFile ~/.ssh/your_ssh_key
  IdentityFile ~/.ssh/your_ssh_key2
  IdentityFile ~/.ssh/your_ssh_key3
  AddKeysToAgent yes

Your_ssh_key没有任何扩展名。不要使用.pub。

我曾经遇到过这个问题,当时我有两个Bitbucket帐户,想要为两个帐户存储单独的SSH密钥。这对我来说很管用。

我创建了两个单独的ssh配置,如下所示。

Host personal.bitbucket.org
    HostName bitbucket.org
    User git
    IdentityFile /Users/username/.ssh/personal
Host work.bitbucket.org
    HostName bitbucket.org
    User git
    IdentityFile /Users/username/.ssh/work

现在当我必须从我的工作帐户克隆一个存储库时,命令如下所示。

git clone git@bitbucket.org:teamname/project.git

我必须修改这个命令:

git clone git@**work**.bitbucket.org:teamname/project.git

同样,从我的个人帐户克隆命令必须修改为

Git克隆git@personal.bitbucket.org:name/personalproject.git

更多信息请参考这个链接。