我遇到了以下错误:

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

当前回答

这个解决方案很可能对你有用,再试一次

在我的例子中,它与DNS映射有关(bitbucket没有指向正确的IP)。我通过一个简单的技巧修复了它(在查找bitbucket.org时,我重写了IP地址)

在ubuntu中打开和编辑/etc/hosts(如果是任何其他操作系统,请查看如何做到这一点)

sudo vim /etc/hosts

并添加以下行(如果是GitHub或任何其他网站,请查找IP并相应地进行映射)

18.205.93.2 bitbucket.org

然后就能解决问题

其他回答

阅读用户456814的答案以获得解释。这里我只尝试自动修复。

如果您在Bash中使用Cygwin终端,请将以下内容添加到$HOME/.bashrc文件中。这只会在第一个Bash终端中启动一次ssh代理,并将密钥添加到ssh代理。(我不确定Linux上是否需要这样做。)

###########################
# start ssh-agent for
# ssh authentication with github.com
###########################
SSH_AUTH_SOCK_FILE=/tmp/SSH_AUTH_SOCK.sh
if [ ! -e $SSH_AUTH_SOCK_FILE ]; then
    # need to find SSH_AUTH_SOCK again.
    # restarting is an easy option
    pkill ssh-agent
fi
# check if already running
SSH_AGENT_PID=`pgrep ssh-agent`
if [ "x$SSH_AGENT_PID" == "x" ]; then
#   echo "not running. starting"
    eval $(ssh-agent -s) > /dev/null
    rm -f $SSH_AUTH_SOCK_FILE
    echo "export SSH_AUTH_SOCK=$SSH_AUTH_SOCK" > $SSH_AUTH_SOCK_FILE
    ssh-add $HOME/.ssh/github.com_id_rsa 2>&1 > /dev/null
#else
#   echo "already running"
fi
source $SSH_AUTH_SOCK_FILE

不要忘记在“ssh-add”命令中添加正确的密钥。

连接到服务器时使用参数-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文件吗?如果你在文本编辑器中打开它,它会说它是私钥吗?

让我提供另一种解决方案。如果您刚刚安装了Git 1.8.2.2或更高版本,并且希望启用SSH,请遵循写得好的说明。

一直到步骤5.6,您可能会遇到一个小障碍。如果SSH代理已经在运行,则在重新启动bash时可能会收到以下错误消息

Could not open a connection to your authentication agent

如果需要,请使用以下命令查看是否有多个ssh代理进程正在运行

ps aux | grep ssh

如果看到多个ssh代理服务,则需要终止所有这些进程。按如下方式使用kill命令(PID在您的计算机上是唯一的)

kill <PID>

例子:

kill 1074

删除所有ssh代理进程后,再次运行px aux | grep ssh命令以确保它们已删除,然后重新启动Bash。

瞧,你现在应该得到这样的东西:

Initializing new SSH agent...
succeeded
Enter passphrase for /c/Users/username/.ssh/id_rsa:

现在,您可以继续执行步骤5.7及更高的步骤。

我在尝试下载Git存储库时出错:

无法打开与身份验证代理的连接。ssh添加退出代码2

要消除此错误,请执行以下操作:

使用无密码私钥时,既不需要ssh代理,也不需要ssh add。请参阅将私钥添加到docker文件中的ssh代理。

如果处理得当(使用后删除),无密码私钥不需要使系统不安全。也许这也有助于解决Heroku问题?


这个“把戏”什么时候最重要?在Dockerfile中!

我必须使用无密码私钥来获取Dockerfile以克隆Git存储库,因为在Dockerfile运行期间,无法在shell中输入密码。请参阅Dockerfile:clone repo with passwordless private key。错误:例如,“身份验证代理”或“read_passphrase:无法打开/dev/tty”。最近,还可以考虑使用部署令牌访问Git存储库,而无需手动输入密码。