我遇到了以下错误:

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

当前回答

你启动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进程(ssh代理),然后卸载git,然后再次安装git来解决这个错误。

这对我有用。

在CMD窗口中,键入以下命令:

cd path-to-Git/bin # (for example,cd C:\Program Files\Git\bin)
bash
exec ssh-agent bash
ssh-add path/to/.ssh/id_rsa

在~/.ssh中创建配置文件并设置PERMANENTLY,主机可以是ip/domain它没有在windows10中测试

Host 20.16.4.5
IdentityFile ~/.ssh/id_rsa_ec2

如果你想过去,就去酒吧。在aws中使用1将PEM中的私钥复制到.ssh文件夹

$ cp /path/to/my-aws-ec2-instance.pem ~/.ssh/id_rsa_ec2

2生成并保存公钥

 ssh-keygen -y -f /path/to/my-aws-ec2-instance.pem > ~/.ssh/id_rsa_ec2.pub

AND SET(手动将密钥永久添加到代理),然后使用ec2而不使用pemssh软件ec2-user@ec2-ip.compute-x.amazonaws.com

对于Windows中的PowerShell

我在使用PowerShell和Start-SshAgent/Add-SshKey命令时遇到了问题,所以我快速编写了一个脚本,可能会帮助一些人。这将添加到PowerShell配置文件中,您可以通过执行记事本$profile来编辑该配置文件:

if ($(Get-Process ssh-agent) -eq $null)
{
     $ExecutionContext.InvokeCommand.ExpandString($(ssh-agent -c).Replace("setenv", "set"));
}

它将检测ssh代理是否正在运行,并且仅在没有代理运行时执行。请注意,$ExecutionContext.InvokeCommand.ExpandString是一个非常危险的命令,因此如果您使用的是不受信任的ssh代理副本,则可能不希望使用此解决方案。

这将只在您第一次需要时运行SSH代理并进行身份验证,而不是每次打开Bash终端时。它可以用于一般使用SSH的任何程序,包括SSH本身和scp。只需将其添加到/etc/profile.d/ssh-helper.sh:

ssh-auth() {
    # Start the SSH agent only if not running
    [[ -z $(ps | grep ssh-agent) ]] && echo $(ssh-agent) > /tmp/ssh-agent-data.sh

    # Identify the running SSH agent
    [[ -z $SSH_AGENT_PID ]] && source /tmp/ssh-agent-data.sh > /dev/null

    # Authenticate (change key path or make a symlink if needed)
    [[ -z $(ssh-add -l | grep "/home/$(whoami)/.ssh/id_rsa") ]] && ssh-add
}

# You can repeat this for other commands using SSH
git() { ssh-auth; command git "$@"; }

注:这是对这个问题的回答,它已经与这个问题合并。这个问题是针对Windows 7的,这意味着我的答案是针对Cygwin/MSYS/MSYS2的。这一点似乎适用于某些Unix,我不希望SSH代理需要这样管理。