我遇到了以下错误:

$ 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.

当前回答

连接到服务器时使用参数-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.

其他回答

尝试执行以下步骤:

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

以下命令对我有效。我使用的是CentOS。

exec ssh-agent bash

你启动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文件吗?如果你在文本编辑器中打开它,它会说它是私钥吗?

ssh-add和ssh(假设您使用的是openssh实现)需要一个环境变量来知道如何与ssh代理通信。如果在与当前使用的命令提示符窗口不同的命令提示符下启动代理,或者如果启动错误,ssh-add和ssh都不会看到该环境变量集(因为环境变量是在本地设置到其所在的命令提示符中的)。

你不知道你使用的是哪个版本的ssh,但如果你使用的cygwin,你可以在cygwin上使用ssh代理的这个配方:

# Add to your Bash config file
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
    eval `$SSHAGENT $SSHAGENTARGS`
    trap "kill $SSH_AGENT_PID" 0
fi

这将为您打开的每个新命令提示符窗口自动启动一个代理(如果您在一个会话中打开多个命令提示符,这是不太理想的,但至少它应该可以工作)。

我尝试了其他解决方案,但都没有效果。我做了更多的研究,发现以下命令有效。我正在使用Windows 7和Git Bash。

eval $(ssh-agent)

更多信息:https://coderwall.com/p/rdi_wq(web存档版本)