在一个客户端上使用多个SSH私钥的最佳方法

Jus*_*tin 816 ssh openssh ssh-keys

我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理,Git的管理,以及同一服务器内的正常Git使用).我试过简单地将id_rsa文件中的密钥堆叠起来无济于事.

显然,一种直接的方法是使用该命令

ssh -i <key location> login@server.example.com 
Run Code Online (Sandbox Code Playgroud)

这非常麻烦.

关于如何更轻松地做这件事的任何建议?

Ran*_*rtz 1161

从我的.ssh/config:

Host myshortname realname.example.com
    HostName realname.example.com
    IdentityFile ~/.ssh/realname_rsa # private key for realname
    User remoteusername

Host myother realname2.example.org
    HostName realname2.example.org
    IdentityFile ~/.ssh/realname2_rsa  # different private key for realname2
    User remoteusername
Run Code Online (Sandbox Code Playgroud)

等等.

  • 请注意,您还可以为同一个`Host`指定多个`IdentityFile`条目,然后在连接时按顺序尝试. (45认同)
  • 谢谢Randal!我做了一些深入研究.ssh/config并找到了这个:http://github.com/guides/multiple-github-accounts向我指出正确的方向. (23认同)
  • 使用`IdentitiesOnly yes`来阻止〜/ .ssh/id_rsa或任何其他身份.(这最初是编辑) (11认同)
  • 这是一个很好的帮助(除了http://stackoverflow.com/a/3828682/169153).如果你想使用putty键,请点击此文档:http://blog.padraigkitterick.com/2007/09/16/using-putty-ssh-keys-with-openssh-on-ubuntu (6认同)
  • 我发现这篇文章非常有帮助。创建配置文件时发生的一个错误是我将.txt文件放在.ssh文件夹中,而不是运行“ touch”命令来创建配置文件。 (2认同)
  • 添加注释“# private key for realname”可能会导致错误“garbage at end of line; “#”`。 (2认同)
  • 这为我的 Windows 机器省去了很多麻烦。谢谢!ssh-agent 可以吸它 (2认同)
  • 如果您使用的是 Windows,并且您的 ssh 客户端似乎忽略了您的 .ssh/config 文件,请参阅此答案:http://stackoverflow.com/questions/9513712/git-ssh-client-for-windows-and-wrong -path-for-ssh-config-file (2认同)
  • 值得理解的是,这个 `~/ssh/config` [东西来自哪里](https://www.ssh.com/ssh/config/) (2认同)
  • 请注意,如果您使用 ssh-agent 来保存密钥,则配置中的 IdentityFile 可以指向 *public* 密钥文件。这样,ssh 可以看到配置中提到的文件已经存在于代理中,如果密钥是私有的(假设您的私钥上有密码短语),则它无法执行此操作。 (2认同)

小智 322

您可以指示ssh在连接时连续尝试多个键.这是如何做:

$ cat ~/.ssh/config
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_rsa_old
IdentityFile ~/.ssh/id_ed25519
# ... and so on

$ ssh server.example.com -v
....
debug1: Next authentication method: publickey
debug1: Trying private key: /home/example/.ssh/id_rsa
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/example/.ssh/id_rsa_old
debug1: read PEM private key done: type RSA
....
[server ~]$
Run Code Online (Sandbox Code Playgroud)

这样您就不必指定哪个键可以与哪个服务器一起使用.它只会使用第一个工作密钥.

如果给定服务器愿意接受密钥,您也只能输入密码.如上所示,.ssh/id_rsa即使有密码,ssh也没有尝试要求输入密码.

当然,它不像其他答案那样超出每服务器配置,但至少你不必为你连接的所有服务器添加配置!

  • 这对git不起作用,好像你有两个github部署密钥,列表中的第一个是有效的并且可以工作,但是然后github会抱怨存储库不匹配. (24认同)
  • 对于提出的问题,这是一个很棒的解决方案,但并不能完全满足提问者的意图.对我来说,它是完全正确的解决方案,它完全满足"在一个客户端上使用多个SSH私钥的最佳方式"的需求. (9认同)
  • 这应该是一个公认的答案. (3认同)
  • 如果 SFTP/目标服务器具有锁定帐户的安全策略(例如在 3 次多次失败的连接尝试之后),这是否最终会锁定帐户。尝试连接,但使用“错误密钥”文件 (3认同)
  • 请注意,在这些服务器上是否有类似fail2ban的信息。您可能会遇到这些监狱之一...由于其他键生成的尝试失败... (3认同)
  • 在配置文件中的主机声明下,这似乎不起作用 (2认同)

per*_*ron 249

兰德尔施瓦茨答案几乎帮助了我.我在服务器上有不同的用户名,所以我必须将User关键字添加到我的文件中:

Host           friendly-name
HostName       long.and.cumbersome.server.name
IdentityFile   ~/.ssh/private_ssh_file
User           username-on-remote-machine
Run Code Online (Sandbox Code Playgroud)

现在您可以使用友好名称进行连接:

ssh friendly-name
Run Code Online (Sandbox Code Playgroud)

可以在OpenSSH手册页上找到更多关键字.注意:列出的某些关键字可能已存在于/ etc/ssh/ssh_config文件中.

  • 我更喜欢使用'Port'关键字.另一个有趣的关键字是'StrictHostKeyChecking'. (3认同)

小智 99

foo:~$ssh-add ~/.ssh/xxx_id_rsa
Run Code Online (Sandbox Code Playgroud)

确保在添加之前测试它:

ssh -i ~/.ssh/xxx_id_rsa username@example.com
Run Code Online (Sandbox Code Playgroud)

如果您有任何错误问题,有时更改文件的安全性有助于:

chmod 0600 ~/.ssh/xxx_id_rsa
Run Code Online (Sandbox Code Playgroud)

  • +1 for chmod 0600 - 权限问题阻止我连接 (6认同)
  • 在我看来,这是最简洁和优雅的解决方案.像魅力一样工作! (4认同)
  • 这很好用,直到你在mac os X上重启你的机器. (4认同)

obl*_*ion 96

之前的答案已正确解释了创建配置文件以管理多个ssh密钥的方法.我认为,还需要解释的重要事情是在克隆存储库时用别名替换主机名.

假设,贵公司的GitHub帐户的用户名是abc1234.假设您的个人GitHub帐户的用户名是jack1234

并且,假设您已创建了两个RSA密钥,即id_rsa_companyid_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
Run Code Online (Sandbox Code Playgroud)

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

Repo URL: git@github.com:abc1234/demo.git
Run Code Online (Sandbox Code Playgroud)

现在,在执行操作时git clone,您应该将上述存储库URL修改为:

git@company:abc1234/demo.git
Run Code Online (Sandbox Code Playgroud)

请注意github.com现在如何替换为我们在配置文件中定义的别名"company".

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

  • 我希望我能不止一次地回答这个问题......这是解决问题的正确方法,它比其他方案更安全,更快捷.更具可扩展性(允许为*same*hostname定义不同的键) (7认同)
  • 还值得一提的是,如果您使用“ssh-agent”,那么您还应该在配置中添加“IdentitiesOnly yes”选项,否则“ssh-agent”将尝试使用不同别名的密钥连接到给定别名,如果他们的主机名匹配。 (3认同)
  • 不再浪费时间,这就是答案。非常感谢。 (2认同)
  • 我真的希望我能早点找到这个答案……但是总比没有好,谢谢! (2认同)
  • 很好的解释!对我来说很完美。而且,如果您忘记了使用别名克隆存储库,则通常可以在此后编辑远程原始URL。 (2认同)
  • 只需要注意,因为配置文件必须是(chmod 600) (2认同)
  • 虽然没有明确说明,但这解决了尝试在 GitHub 上使用多个密钥时的一个主要问题:由于您始终以同一用户“git”登录 GitHub,因此保存在任何 GitHub 帐户上的任何密钥都将允许 SSH 登录。但是在 SSH 登录之后,GitHub 将检查所使用的密钥是否被授权用于您正在尝试的特定操作(例如,检查存储库),如果密钥错误,则会失败。但 SSH 部分成功了,因此 SSH 不会尝试另一个密钥。该解决方案通过引入主机名别名来解决这个问题。杰出的! (2认同)
  • 如果您不想每次克隆存储库时都修改存储库 url,则可以将“Host company github.com”作为配置文件中的条目之一,该条目将充当“默认”。 (2认同)

Saj*_*han 33

  1. 生成SSH密钥:

    $ ssh-keygen -t rsa -C <email1@example.com>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 生成another SSH key:

    $ ssh-keygen -t rsa -f ~/.ssh/accountB -C <email2@example.com>
    
    Run Code Online (Sandbox Code Playgroud)

    现在,目录中应该存在两个公钥(id_rsa.pub,accountB.pub)~/.ssh/.

    $ ls -l ~/.ssh     # see the files of '~/.ssh/' directory 
    
    Run Code Online (Sandbox Code Playgroud)
  3. ~/.ssh/config使用以下内容创建配置文件:

    $ nano ~/.ssh/config
    
    Host bitbucket.org  
        User git  
        Hostname bitbucket.org
        PreferredAuthentications publickey  
        IdentityFile ~/.ssh/id_rsa  
    
    Host bitbucket-accountB  
        User git  
        Hostname bitbucket.org  
        PreferredAuthentications publickey  
        IdentitiesOnly yes  
        IdentityFile ~/.ssh/accountB  
    
    Run Code Online (Sandbox Code Playgroud)
  4. default帐户克隆.

    $ git clone git@bitbucket.org:username/project.git
    
    Run Code Online (Sandbox Code Playgroud)
  5. accountB帐户克隆.

    $ git clone git@bitbucket-accountB:username/project.git
    
    Run Code Online (Sandbox Code Playgroud)

在这里查看更多


Wah*_*Haq 22

我同意Tuomas关于使用ssh-agent的观点.我还想为工作添加第二个私钥,这个教程对我来说就像一个魅力.

步骤如下:

  1. $ ssh-agent bash
  2. $ ssh-add /path.to/private/key 例如 ssh-add ~/.ssh/id_rsa
  3. 验证 $ ssh-add -l
  4. $ssh -v <host url>例如测试它ssh -v git@assembla.com

  • 使用`ssh-agent`多年后,我最近在我的`i3` wm中转而使用Gnome的`gnome-keyring`.原因很简单:Gnome的Keyring管理器自动处理添加和删除ssh密钥,而不必记住`ssh-add`.另外为我提供一个密码来解锁它们(为了安全起见,在指定时间内超时).对每一个他自己.因为我在Arch上使用gnome设置,所以我的设置就是插件.如果你是反gnome,请忽略此评论. (4认同)

Ana*_*Pai 14

我曾经遇到过这个问题,当时我有两个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
Run Code Online (Sandbox Code Playgroud)

现在,当我不得不从我的工作帐户克隆存储库时 - 命令如下.

git clone git@bitbucket.org:teamname/project.git
Run Code Online (Sandbox Code Playgroud)

我不得不将此命令修改为:

git clone git@**work**.bitbucket.org:teamname/project.git
Run Code Online (Sandbox Code Playgroud)

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

git clone git @ personal .bitbucket.org:name/personalproject.git

有关更多信息,请参阅此链接.


Tuo*_*nen 10

使用ssh-agent作为密钥.

  • 你能详细说明一下吗? (2认同)

Nag*_*ran 8

现在,使用最新版本的git,我们可以在存储库特定的git配置文件中指定sshCommand

  [core]
      repositoryformatversion = 0
      filemode = true
      bare = false
      logallrefupdates = true
      sshCommand = ssh -i ~/.ssh/id_rsa_user   
   [remote "origin"]
      url = git@bitbucket.org:user/repo.git
      fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

  • 这是最正确的做法。如果你不想修改文件,只需在 git 存储库中运行 `git config --local core.sshCommand "ssh -i ~/.ssh/your_other_rsa_file"` (4认同)

Hug*_*ohm 7

这是我使用的解决方案,灵感来自sajib-khan 的答案。未设置默认配置;它是我在GitLab上的个人帐户,另一个指定的是我的公司帐户。这是我所做的:

生成 SSH 密钥

ssh-keygen -t rsa -f ~/.ssh/company -C "name.surname@company.com"
Run Code Online (Sandbox Code Playgroud)

编辑 SSH 配置

nano ~/.ssh/config
Run Code Online (Sandbox Code Playgroud)
    Host company.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/company
Run Code Online (Sandbox Code Playgroud)

删除缓存的 SSH 密钥

ssh-add -D
Run Code Online (Sandbox Code Playgroud)

测试一下!

ssh -T git@company.gitlab.com
Run Code Online (Sandbox Code Playgroud)

欢迎来到 GitLab,@hugo.sohm!

ssh -T git@gitlab.com
Run Code Online (Sandbox Code Playgroud)

欢迎来到 GitLab,@HugoSohm!

用它!

公司账户

git clone git@company.gitlab.com:group/project.git
Run Code Online (Sandbox Code Playgroud)

个人/默认帐户

git clone git@gitlab.com:username/project.git
Run Code Online (Sandbox Code Playgroud)

这是我使用的来源


amd*_*dev 6

对我来说,唯一可行的解​​决方案是简单地将其添加到文件中~/.ssh/config

Host *
  IdentityFile ~/.ssh/your_ssh_key
  IdentityFile ~/.ssh/your_ssh_key2
  IdentityFile ~/.ssh/your_ssh_key3
  AddKeysToAgent yes
Run Code Online (Sandbox Code Playgroud)

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