我遇到了以下错误:

$ git push heroku master
Warning: Permanently added the RSA host key for IP address '50.19.85.132' to the list of known hosts.
!  Your key with fingerprint b7:fd:15:25:02:8e:5f:06:4f:1c:af:f3:f0:c3:c2:65 is not authorized to access bitstarter.

我尝试添加密钥,但出现以下错误:

$ ssh-add ~/.ssh/id_rsa.pub
Could not open a connection to your authentication agent.

当前回答

我在Linux上遇到了同样的问题,下面是我所做的:

基本上,命令ssh-agent启动代理,但它并没有真正设置运行环境变量。它只是将这些变量输出到shell。

您需要:

eval `ssh-agent`

然后执行ssh-add。请参阅无法打开与身份验证代理的连接。

其他回答

以下是我在使用PowerShell时提出的解决方案。

将以下函数添加到Microsoft.PowerShell_profile.ps1

function RunSsh($userIdentity ) {
   $agent=ssh-agent
   $position=$agent[0].IndexOf("=")
   $ending=$agent[0].IndexOf(";")

   $variableStartPosition=$agent[0].IndexOf("export")
   $variableEndPosition=$agent[0].LastIndexOf(";")
   $variableName=$agent[0].Substring($variableStartPosition+7,$variableEndPosition-$variableStartPosition-7)
   [Environment]::SetEnvironmentVariable($variableName, $agent[0].Substring($position+1,$ending-$position-1))

   $position=$agent[1].IndexOf("=")
   $ending=$agent[1].IndexOf(";")

   $variableStartPosition=$agent[1].IndexOf("export")
   $variableEndPosition=$agent[1].LastIndexOf(";")
   $variableName=$agent[1].Substring($variableStartPosition+7,$variableEndPosition-$variableStartPosition-7)
   [Environment]::SetEnvironmentVariable($variableName, $agent[1].Substring($position+1,$ending-$position-1))

   if($userIdentity.Length -eq 0) {
      ssh-add
   } else {
      ssh-add $userIdentity
   }
}

现在,您可以从命令行运行RunSsh,它使用~\.ssh文件夹中的标识文件,或者使用RunSsh C:\ssh\id_rsa传递标识文件,其中C:\ssh\ id_rsa是您的标识文件。

要实现这一点,您需要在路径环境变量中使用ssh-add和ssh-agent。

连接到服务器时使用参数-A,例如:

ssh -A root@myhost

来自手册页:

-A Enables forwarding of the authentication agent connection.  
   This can also be specified on a per-host basis in a configuration file.

   Agent forwarding should be enabled with caution.  Users with the ability to bypass file permissions on the remote host (for the agent's
   UNIX-domain socket) can access the local agent through the forwarded 
   connection.  An attacker cannot obtain key material from the agent,
   however they can perform operations on the keys that enable them to
   authenticate using the identities loaded into the agent.

你启动ssh代理了吗?

在运行ssh-add命令之前,可能需要启动ssh代理:

eval `ssh-agent -s`
ssh-add

注意,这将在Windows上启动msysgit Bash的代理。如果您使用的是不同的shell或操作系统,则可能需要使用命令的变体,例如其他答案中列出的那些。

请参阅以下答案:

ssh-add抱怨:无法打开与身份验证代理的连接Git推送需要用户名和密码(包含如何使用ssh代理的详细说明)如何运行(git/ssh)身份验证代理?。无法打开与身份验证代理的连接

要自动启动ssh代理并允许单个实例在多个控制台窗口中工作,请参阅登录时启动ssh代理。

为什么我们需要使用eval而不仅仅是ssh代理?

SSH需要两件事才能使用SSH-agent:一个在后台运行的SSH-agent实例,以及一个环境变量集,该环境变量集告诉SSH应该使用哪个套接字连接到代理(SSH_AUTH_SOCK IIRC)。如果您只运行ssh代理,那么代理将启动,但ssh不知道在哪里可以找到它。

从这个评论中。

公钥与私钥

此外,每当我使用ssh-add时,我总是向其中添加私钥。文件~/.ssh/id_rsa.pub看起来像公钥,我不确定这是否有效。您有~/.ssh/id_rsa文件吗?如果你在文本编辑器中打开它,它会说它是私钥吗?

尝试执行以下步骤:

打开GitBash并运行:cd~/.ssh尝试运行agent:eval$(ssh代理)现在,您可以运行以下命令:ssh-add-l

我通过强制停止(终止)git进程(ssh代理),然后卸载git,然后再次安装git来解决这个错误。