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


甲基蓝
2025-02-06 11:17:06 (24天前)


在同一服务器内)。我试过简单地堆叠id_rsa文件中的密钥无济于事。

显然,直接的方法是使用

命令
</跨度>

ssh -i&lt;密钥位置&gt; login@server.example.com

这非常麻烦。

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


9 条回复
  1. 0# 银角 | 2019-08-31 10-32



    您可以创建一个名为的配置文件

    config

    在你的

    ~/.ssh

    夹。它可以包含:




    1. Host aws
      HostName yourip
      User youruser
      IdentityFile idFile

    2. </code>


    这将允许您连接到这样的机器




    1. ssh aws

    2. </code>

  2. 1# 谦逊的毛巾 | 2019-08-31 10-32



    现在,使用最新版本的git,我们可以指定

    sshCommand
    </强>
    在特定于存储库的git配置文件中。




    1. [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/

    2. </code>

  3. 2# 天线宝宝 | 2019-08-31 10-32



    在运行OpenSSH_5.3p1,OpenSSL 1.0.1e-fips的Centos 6.5上,我通过重命名我的密钥文件解决了这个问题,因此没有一个具有默认名称。我的.ssh目录包含id_rsa_foo和id_rsa_bar,但没有id_rsa等。


  4. 3# 遇见你 | 2019-08-31 10-32





    1. 生成SSH密钥:




      1. $ ssh-keygen -t rsa -C email1@example.com

      2.     </code>
      3.   </pre>
      4. </LI>
      5. <LI>
      6.   <P>
      7.     生成
      8.      <code>
      9.       another SSH key
      10.     </code>
      11.     
      12.   </p>
      13.    <pre>
      14.     <code>
      15.       $ ssh-keygen -t rsa -f ~/.ssh/accountB -C <email2@example.com>
      16.     </code>
      17.   </pre>
      18.   <P>
      19.     现在,两个公钥(
      20.     的<strong>
      21.       id_rsa.pub
      22.     </强>
      23.     
      24.     的<strong>
      25.       accountB.pub
      26.     </强>
      27.     )应该存在于
      28.      <code>
      29.       ~/.ssh/
      30.     </code>
      31.      目录。
      32.   </p>
      33.    <pre>
      34.     <code>
      35.       $ ls -l ~/.ssh     # see the files of '~/.ssh/' directory 
      36.     </code>
      37.   </pre>
      38. </LI>
      39. <LI>
      40.   <P>
      41.     创建配置文件
      42.      <code>
      43.       ~/.ssh/config
      44.     </code>
      45.      具有以下内容:
      46.   </p>
      47.    <pre>
      48.     <code>
      49.       $ nano ~/.ssh/config
      50. Host bitbucket.org
        User git
        Hostname bitbucket.org
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa

      51. Host bitbucket-accountB
        User git
        Hostname bitbucket.org
        PreferredAuthentications publickey
        IdentitiesOnly yes
        IdentityFile ~/.ssh/accountB

      52.     </code>
      53.   </pre>
      54. </LI>
      55. <LI>
      56.   <P>
      57.     克隆自
      58.      <code>
      59.       default
      60.     </code>
      61.      帐户。
      62.   </p>
      63.    <pre>
      64.     <code>
      65.       $ git clone git@bitbucket.org:username/project.git
      66.     </code>
      67.   </pre>
      68. </LI>
      69. <LI>
      70.   <P>
      71.     克隆自
      72.      <code>
      73.       accountB
      74.     </code>
      75.      帐户。
      76.   </p>
      77.    <pre>
      78.     <code>
      79.       $ git clone git@bitbucket-accountB:username/project.git
      80.     </code>
      81.   </pre>
      82. </LI>
      83. </醇>




        在这里查看更多
        </强>


  5. 4# SHOU宅大可爱 | 2019-08-31 10-32



    从我的

    .ssh/config





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

    2. Host myother realname2.example.org
      HostName realname2.example.org
      IdentityFile ~/.ssh/realname2_rsa # different private key for realname2
      User remoteusername

    3. </code>


    等等。


  6. 5# 求赞有赞必回 | 2019-08-31 10-32





    Randal Schwartz的回答

    几乎帮助了我。
    我在服务器上有一个不同的用户名,所以我不得不添加

    用户
    </强>
    关键字到我的文件:




    1. Host friendly-name
      HostName long.and.cumbersome.server.name
      IdentityFile ~/.ssh/private_ssh_file
      User username-on-remote-machine

    2. </code>


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




    1. ssh friendly-name

    2. </code>


    可以在上找到更多关键字

    OpenSSH手册页



    注意:
    </强>
    列出的某些关键字可能已存在于您的

    的/ etc / SSH / ssh_config中

    文件。


  7. 6# 疯子哥哥 | 2019-08-31 10-32




    重要提示:您必须启动ssh-agent
    </强>



    在使用ssh-add之前,必须启动ssh-agent(如果它尚未运行),如下所示:




    1. eval ssh-agent -s # start the agent

    2. ssh-add id_rsa_2 # where id_rsa_2 is your new private key file

    3. </code>


    请注意,eval命令在Windows上的GIT bash上启动代理。其他环境可能使用变体来启动SSH代理。


  8. 7# 浮华丶 | 2019-08-31 10-32



    使用ssh-agent作为密钥。


登录 后才能参与评论