【问题标题】:How to use local ssh key to access a third computer?如何使用本地 ssh 密钥访问第三台计算机?
【发布时间】:2017-09-29 07:07:06
【问题描述】:

我想知道是否有办法使用我的本地 ssh 密钥从第三台远程计算机访问 git 服务器。 例如: 假设我们有三台计算机:PC1、PC2 和 GITSERVER。

我的 ssh 密钥存储在 PC1 上,这些密钥在 GITSERVER 上获得授权,从 PC1 我 ssh 到 PC2,我想从那里克隆存储在 GITSERVER 上的存储库。

有没有一种方法可以在不复制我的私钥或创建新私钥的情况下做到这一点?

【问题讨论】:

    标签: git ssh key roaming


    【解决方案1】:

    一种方法是转发 SSH 代理。首先,打开ssh_config文件并添加以下两行:

    $ sudo nano /etc/ssh/ssh_config 
    
    Host <PC2-server-ip.com>
    ForwardAgent yes
    

    将 ssh 密钥添加到代理列表中:

    $ eval "$(ssh-agent)"
    $ ssh-add ~/.ssh/id_rsa          # add ssh key to list of agent 
    identites
    $ ssh-add -l                     # you can see the agent just added
    

    现在,登录 PC2 并从 GITSERVER 克隆:

    $ ssh <user>@<pc2-server-ip>
    $ git clone ...
    

    【讨论】:

      【解决方案2】:

      正如 Sajib Khan 所说,ssh_config 文件可以帮助您。 但是,您总是随身携带您的私钥。可能存在您不想在远程计算机上使用您的私钥的用例。 为此,如果您想将密钥带到远程机器上,您可以定义每个主机:

      $ vim ~/.ssh/config
      Host remote-pc2 # local name of the host (You can use this on your local terminal to connect to the remote host)
      HostName 127.0.0.1 # enter correct IP here
      User francesco # your login name
      ForwardAgent yes
      

      另一种实现方式: 您可以启动ssh-agent 并为其添加密钥,这可以在您的.bashrc 中完成:

      $ vim ~/.bashrc
      eval `ssh-agent -s`
      ssh-add ~/.ssh/id_rsa
      # check if key is there
      $ ssh-add -l
      

      这有助于您在启动 ssh 会话时不必输入密码。 您可以使用以下命令将 ssh 密钥从本地计算机转移到另一台计算机以进行一次连接:

      $ ssh -A username@remote-host
      

      【讨论】:

      • 这样做安全吗?我的意思是,攻击者是否有可能获取有关我的私钥的任何信息?
      • 嗯,它就像 ssh 转发代理一样保存。我相信它。但是,很少有人认为 ssh-agent 不保存(连接到不同的公共计算机时),即heipei.github.io/2015/02/26/… 但是,由于您似乎只是连接到自己的计算机,所以我认为这是保存 - 只要已安装安全补丁:)
      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 2011-12-31
      • 2019-03-18
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 2014-01-23
      • 2013-09-03
      相关资源
      最近更新 更多