首页 文章

无法打开与身份验证代理的连接

提问于
浏览
1253

我遇到了这个错误:

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

30 回答

  • 27

    我遇到的一件事是 eval 对我来说不适合使用Cygwin,对我有用的是 ssh-agent ssh-add id_rsa .

    之后我遇到了一个问题,我的私钥太开放了,我设法找到的解决方案(来自here):

    chgrp Users id_rsa
    

    以及

    chmod 600 id_rsa
    

    最后我能够使用:

    ssh-agent ssh-add id_rsa
    
  • 1938

    你启动ssh-agent了吗?

    您可能需要在运行 ssh-add 命令之前启动 ssh-agent

    eval `ssh-agent -s`
    ssh-add
    

    请注意,这将启动 msysgit Bash on Windows 的代理 . 如果您使用的是其他shell或操作系统,则可能需要使用该命令的变体,例如other answers中列出的变体 .

    请参阅以下答案:

    要自动启动ssh-agent并允许单个实例在多个控制台窗口中工作,请参阅Start ssh-agent on login .

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

    要找出原因,请参阅Robin Green's answer .

    公钥与私钥

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

  • 3

    我尝试了其他解决方案无济于事 . 我做了更多研究,发现以下命令有效 . 我正在使用Windows 7和Git Bash .

    eval $(ssh-agent)
    

    更多信息:https://coderwall.com/p/rdi_wq

  • 180

    以下命令对我有用 . 我正在使用CentOS .

    exec ssh-agent bash
    
  • 6

    MsysGit或Cygwin

    如果您正在使用Msysgit或Cygwin,您可以在SSH-Agent in msysgit and cygwin and bash找到一个很好的教程:

    • 将名为 .bashrc 的文件添加到您的主文件夹 .

    • 打开文件并粘贴到:

    #!/bin/bash
    eval `ssh-agent -s`
    ssh-add
    
    • 这假设您的密钥位于传统的 ~/.ssh/id_rsa 位置 . 如果不是,请在 ssh-add 命令后包含完整路径 .

    • 添加或创建包含内容的文件 ~/.ssh/config

    ForwardAgent yes
    

    在原始教程中, ForwardAgent param是 Yes ,但是's a typo. Use all lowercase or you' ll会出错 .

    • 重启Msysgit . 它会要求您输入一次密码,就是这样(直到您结束会话,或者您的ssh-agent被杀死 . )

    Mac / OS X.

    如果您不想在每次打开终端时启动新的ssh-agent,请查看Keychain . 我现在在Mac上,所以我使用了教程ssh-agent with zsh & keychain on Mac OS X进行设置,但我确信Google搜索会有大量的Windows信息 .

    Update :Mac上更好的解决方案是将密钥添加到Mac OS Keychain:

    ssh-add -K ~/.ssh/id_rsa
    

    就那么简单 .

  • 5

    无法打开与身份验证代理的连接

    要解决此错误:

    庆典:

    $ eval `ssh-agent -s`
    

    tcsh中:

    $ eval `ssh-agent -c`
    

    然后像往常一样使用 ssh-add .


    Hot Tip:

    我总是忘记为上面的ssh-agent命令输入什么,所以我在我的 .bashrc 文件中创建了一个别名,如下所示:

    alias ssh-agent-cyg='eval `ssh-agent -s`'
    

    现在不用 ssh-agent ,我可以使用 ssh-agent-cyg

    例如 .

    $ ssh-agent-cyg
    SSH_AUTH_SOCK=/tmp/ssh-n16KsxjuTMiM/agent.32394; export SSH_AUTH_SOCK;
    SSH_AGENT_PID=32395; export SSH_AGENT_PID;
    echo Agent pid 32395;
    $ ssh-add ~/.ssh/my_pk
    

    原始修复源:

    http://cygwin.com/ml/cygwin/2011-10/msg00313.html

  • 47

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

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

    你需要:

    eval `ssh-agent`
    

    然后执行ssh-add . 见Could not open a connection to your authentication agent .

  • 2

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

    你不能使用,但如果你're using cygwin',你可以使用SSH Agent on Cygwin中的这个食谱:

    # 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
    

    这将为您打开的每个新命令提示符窗口自动启动代理程序(如果您在一个会话中打开多个命令提示符,这是次优的,但至少它应该工作) .

  • 0

    尝试以下步骤:

    1)打开Git Bash并运行: cd ~/.ssh

    2)尝试运行代理: eval $(ssh-agent)

    3)现在,您可以运行以下命令: ssh-add -l

  • 9

    我没有使用 $ ssh-agent -s ,而是使用了 $ evalssh-agent -s`` 来解决这个问题 .

    这是我一步一步执行的(GitBash上的第2步):

    • C:\user\<username>\.ssh\ 清理了我的.ssh文件夹

    • 生成新的SSH密钥
      $ ssh-keygen -t rsa -b 4096 -C "xyz@abc.com"

    • 检查是否有任何进程ID(ssh代理)已在运行 .
      $ ps aux | grep ssh

    • (可选)如果在步骤3中找到任何内容,则执行kill那些
      $ kill <pids>

    • 启动了ssh代理
      $ evalssh-agent -s``

    • 在步骤2中为ssh代理添加了ssh密钥
      $ ssh-add ~/.ssh/id_rsa

  • 229

    在Windows 10中,我尝试了此处列出的所有答案,但它们似乎都没有效果 . 事实上,他们给出了一个线索 . 要解决问题,只需要3个命令 . The idea of this problem is that ssh-add needs SSH_AUTH_SOCK and SSH_AGENT_PID environment variables to be set with current ssh-agent sock file path and pid number.

    ssh-agent -s > temp.txt
    

    这将在文件中保存ssh-agent的输出 . 文本文件内容将是这样的:

    SSH_AUTH_SOCK=/tmp/ssh-kjmxRb2764/agent.2764; export SSH_AUTH_SOCK;
    SSH_AGENT_PID=3044; export SSH_AGENT_PID;
    echo Agent pid 3044;
    

    从文本文件中复制类似“/tmp/ssh-kjmxRb2764/agent.2764”的内容,并直接在控制台中运行以下命令:

    set SSH_AUTH_SOCK=/tmp/ssh-kjmxRb2764/agent.2764
    

    从文本文件中复制类似“3044”的内容,并直接在控制台中运行以下命令:

    set SSH_AGENT_PID=3044
    

    现在,当为当前控制台会话设置环境变量(SSH_AUTH_SOCK和SSH_AGENT_PID)时,运行ssh-add命令,它不会再次失败以连接ssh代理 .

  • 25

    放大n3o对Windows 7的回答......

    我的问题确实是没有设置一些必需的环境变量,并且n3o是正确的,ssh-agent告诉你如何设置这些环境变量,但实际上并没有设置它们 .

    由于Windows不允许您执行“eval”,因此以下是做什么:

    使用ssh-agent的输出重定向到批处理文件

    ssh-agent > temp.bat
    

    现在使用记事本等文本编辑器编辑temp.bat . 对于前两行中的每一行: - 在行的开头插入单词“set”和空格 . - 删除第一个分号及其后的所有内容 .

    现在删除第三行 . 你的temp.bat应该是这样的:

    set SSH_AUTH_SOCK=/tmp/ssh-EorQv10636/agent.10636
    set SSH_AGENT_PID=8608
    

    运行temp.bat . 这将设置ssh-add工作所需的环境变量 .

  • 14

    我刚刚开始工作了 . 打开 ~/.ssh/config 文件 .

    附上以下内容 -

    Host github.com
     IdentityFile ~/.ssh/github_rsa
    

    给我提示的页面Set up SSH for Git表示单个空格缩进很重要......虽然我在这里有一个来自Heroku的配置没有那个空间并且工作正常 .

  • 49

    如果您按照这些说明操作,您的问题就会得到解决 .

    如果您使用的是Mac或Linux计算机,请键入:

    eval "$(ssh-agent -s)"
    

    如果您使用的是Windows计算机,请键入:

    ssh-agent -s
    
  • 2

    让我提供另一种解决方案 . 如果你刚刚安装了Git 1.8.2.2或者你想要启用SSH,请按照写得好的directions进行操作 .

    一直到步骤5.6,你可能会遇到轻微的障碍 . 如果SSH代理已在运行,则在重新启动bash时可能会收到以下错误消息

    Could not open a connection to your authentication agent
    

    如果这样做,请使用以下命令查看是否正在运行多个ssh-agent进程

    ps aux | grep ssh
    

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

    kill <PID>
    

    例:

    kill 1074
    

    删除所有ssh-agent进程后,运行px aux | grep ssh命令再次确定它们已经消失,然后重新启动Bash .

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

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

    现在,您可以继续执行步骤5.7及更高版本 .

  • 1

    注意:这是这个问题的答案,已与此问题合并 . 这个问题适用于Windows 7,这意味着我的答案是针对Cygwin / MSYS / MSYS2 . 对于某些unix来说,这个似乎是一个unix,我不希望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 "$@"; }
    
  • 1

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

    许多答案都回答了运行 ssh-agent 的基本解决方案 . 但是,多次运行 ssh-agent (每个打开的终端或每个远程登录)将在内存中创建许多副本ot ssh-agent . 建议避免该问题的脚本很长,需要编写和/或复制分离的文件,或者需要在 ~/.profile~/.schrc 中写入太多字符串 . 我建议 simple two string solution:

    对于 shbash 等:

    # ~/.profile
    if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -s > ~/.ssh-agent.sh; fi
    . ~/.ssh-agent.sh
    

    对于 cshtcsh 等:

    # ~/.schrc
    sh -c 'if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -c > ~/.ssh-agent.tcsh; fi'
    eval `cat ~/.ssh-agent.tcsh`
    

    这是什么:

    • 按名称搜索进程 ssh-agent 当前用户

    • 通过调用 ssh-agent 创建适当的shell脚本文件,如果没有找到当前用户 ssh-agent 进程,则运行 ssh-agent 本身

    • 评估创建的shell脚本,它配置适当的环境

    没有必要保护创建的shell脚本 ~/.ssh-agent.tcsh~/.ssh-agent.sh 来自其他用户的访问权限,因为:通过受保护的套接字处理与 ssh-agent 的第一次通信,而其他用户无法访问,而且其他用户可以通过枚举找到 ssh-agent 套接字简单 /tmp/ 目录中的文件 . 至于访问 ssh-agent 进程它是一样的东西 .

  • 98

    请尝试以下方法:

    ssh-agent sh -c 'ssh-add && git push heroku master'
    
  • 3

    我有这个问题,当我启动ssh-agent时,它已经运行了 . 迷茫 . 要查看是否是这种情况,请使用

    eval $(ssh-agent)
    

    看看这是否和你想象的一样 . 就我而言,它与我刚刚开始时的不同 .

    要进一步验证您是否运行了多个ssh-agent,可以查看:

    ps -ef | grep ssh
    
  • 195

    当我试图在Windows上通过ssh连接到stash时,我遇到了类似的问题

    这是适合我的解决方案 .

    • 结果我在我的Windows机器上运行了Pageant ssh代理 - 我会检查你在运行什么 . 我怀疑它是Pageant,因为它是默认的Putty和winScp

    • ssh-add在命令行中不适用于此类代理

    • 您需要通过选美UI窗口添加私钥,您可以通过双击任务栏中的Pageant图标(启动后)来获取私钥 .

    • 在将关键字添加到Pageant之前,您需要将其转换为PPK格式 . 完整的说明在这里How to convert SSH key to ppk format

    • 就是这样 . 一旦我将密钥上传到stash,我就可以使用SourceTree创建本地仓库并克隆远程 .

    希望这可以帮助...

  • 4

    阅读@cupcake's answer以获得解释 . 在这里,我只尝试自动修复 .

    如果将Cygwin终端与BASH一起使用,请将以下内容添加到$ HOME / .bashrc文件中 . 这只在第一个Bash终端启动ssh-agent一次,并将密钥添加到ssh-agent . (不确定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”命令中添加正确的密钥 .

  • 2

    我通过强制停止(杀死)git进程(ssh代理),然后卸载Git,然后再次安装Git来解决错误 .

  • 3

    如果您使用Putty,可能需要将“Connection / SSH / Auth / Allow agent forwarding”选项设置为“true” .

    enter image description here

  • 462

    这个worked对我来说 .

    在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
    
  • 108

    您可以使用此命令: ssh-agent bash 然后运行ssh-add . 更多细节,你可以搜索 ssh-agent 或命令 man ssh-agent

  • 0

    在Win8.1E上使用Git Bash,我的解决方案如下:

    eval $(ssh-agent) > /dev/null
    ssh-add ~/.ssh/id_rsa
    
  • 88

    对于Windows 10中内置的bash,我将其添加到.bash_profile:

    if [ -z $SSH_AUTH_SOCK ]; then
        if [ -r ~/.ssh/env ]; then
                source ~/.ssh/env
                if [ `ps -p $SSH_AGENT_PID | wc -l` = 1 ]; then
                        rm ~/.ssh/env
                        unset SSH_AUTH_SOCK
                fi
        fi
    fi
    
    if [ -z $SSH_AUTH_SOCK ]; then
        ssh-agent -s | sed 's/^echo/#echo/'> ~/.ssh/env
        chmod 600 ~/.ssh/env
        source ~/.ssh/env > /dev/null 2>&1
    fi
    
  • 9

    在我的情况下,我的Comodo firewall已经沙箱化了ssh代理 . 一旦我禁用了沙盒,我就能够克隆存储库 .

    仅供参考,我在Windows 7上使用Comodo防火墙 .

  • 2

    甚至在生成和添加SSH密钥时运行命令时"Could not open a connection to your authentication agent.": ssh-add ~/.ssh/id_rsa . 我通过停止在我的机器上运行的多个 ssh-agent 实例解决了这个问题,然后从我的Windows机器上的控制面板上卸载了Git,然后再次安装了Git,现在一切正常 .

相关问题