为什么执行 cap deploy 时出现 Permission denied (publickey) 错误?

47

我有点困惑... 我可以正常向我的github仓库提交,但是当我尝试从我的本地文件夹到我的staging服务器执行cap deploy时,我会得到Permission denied (publickey).

如果我运行ssh git@github.com,我实际上会得到一个错误PTY allocation request failed on channel 0

所以这里出了些问题。

如果我运行 ssh -vT git@github.com,我会得到:

OpenSSH_5.6p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data /Users/myuser/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: Applying options for *
debug1: Connecting to github.com [207.97.227.239] port 22.
debug1: Connection established.
debug1: identity file /Users/myuser/.ssh/id_rsa type 1
debug1: identity file /Users/myuser/.ssh/id_rsa-cert type -1
debug1: identity file /Users/myuser/.ssh/id_dsa type -1
debug1: identity file /Users/myuser/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1p1 Debian-5github2
debug1: match: OpenSSH_5.1p1 Debian-5github2 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.6
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/myuser/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/myuser/.ssh/github_rsa
debug1: Remote: Forced command: gerve technomad
debug1: Remote: Port forwarding disabled.
debug1: Remote: X11 forwarding disabled.
debug1: Remote: Agent forwarding disabled.
debug1: Remote: Pty allocation disabled.
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Remote: Forced command: gerve technomad
debug1: Remote: Port forwarding disabled.
debug1: Remote: X11 forwarding disabled.
debug1: Remote: Agent forwarding disabled.
debug1: Remote: Pty allocation disabled.
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([207.97.227.239]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
Hi technomad! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2384, received 2888 bytes, in 0.1 seconds
Bytes per second: sent 42630.8, received 51643.3
debug1: Exit status 1

我的密钥在~/.ssh文件夹中,那么问题出在哪里?如果有密钥问题,为什么我还能提交到存储库呢?

更新: 当我进入我的.ssh文件夹时,我注意到了一些情况。在安装Github for Mac时创建了一个新的密钥对……为什么它不能使用我的现有密钥对呢,我不知道。

6个回答

130

我必须做以下事情:

   $ ssh-add -D   #remove existing identities
   $ ssh-agent    #copy the lines & run them
   $ ssh-add      #uses the output from above

2
这是问题的确切答案。您无需在阶段机器上添加ssh密钥。只需运行上述命令即可完成...感谢@olore - Rahul Chaudhari
15
但是,这背后的原因是什么?这个命令又是如何解决这个问题的? - Rahul Chaudhari
8
以上命令将RSA/DSA身份验证信息添加到认证代理中,在部署时检查权限。如果在运行部署命令之前未添加身份验证信息,则会出现权限被拒绝的错误。对于我来说,每当我重新启动电脑时,都需要键入以上命令才能使其正常工作。我发现我的ssh公钥未能在每次重启后自动添加。因此,我使用了上述命令后面的ssh-add -K命令以将输出添加到钥匙串中。之后我的问题得到了永久解决。 - Brian Russel Davis
我在Debian中遇到了一个巨大的问题,因为我的密钥名称与id_rsa不同。我只需执行ssh-add $HOME/.ssh/[key_name]即可解决问题。 - user4383363
这对我有用。在将文件系统转移到新计算机后,我遇到了错误。 - Daniel Ristic
显示剩余2条评论

42

有时我会遇到这个错误,然后我只需输入$ ssh-add -k来添加我的身份标识,然后它就可以正常工作了。不确定为什么这样做可以解决问题,也不知道为什么错误消息没有建议这么做,但它总是能够拯救我!


19

我会确保你的测试服务器具有访问GitHub的SSH权限。在测试服务器的终端上运行相同的命令 "ssh -vT git@github.com",以帮助确定是否存在远程机器上的SSH问题。


那个有效,谢谢 :),我忽略了那一部分。但我仍然不知道为什么“ssh git@github.com”会返回“在通道0上的PTY分配请求失败”? - GiH
你能否在不出现问题的情况下ssh到其他服务器? - Dave Bettin
1
我会查看这个帖子:http://comments.gmane.org/gmane.os.cygwin/125243。希望有所帮助。 - Dave Bettin
1
谢谢,你的链接最终让我找到了这个答案https://dev59.com/H2865IYBdhLWcg3wYNjj。引用答案中的话:“关于GitHub身份验证,‘PTY分配请求失败’是一个误导(它是尝试交互式登录到GitHub时的结果,当他们唯一提供的SSH服务是非交互式的Git-over-SSH时;身份验证正在工作,他们只是不提供交互式的“shell”服务)。” - GiH
1
请注意,如果您在Capistrano中使用了 set :ssh_options, forward_agent: true(这是处理密钥的好方法),那么这可能是一个误导。如果您正在使用代理转发,即使其他所有内容都正常工作,您也可能无法从远程服务器ssh到Github,这意味着原始问题可能与本地ssh配置有关。 - Dane
显示剩余2条评论

3
如果您使用的是MAC系统,可能您的ssh密钥尚未添加到认证代理中。执行以下命令即可完成添加。
ssh-add path_to_private_key

例如:
ssh-add ~/.ssh/id_rsa

3
在安装 GitHub for Mac OS X 后,我遇到了同样的问题。应用程序在 ~/.ssh/github_rsa 中创建了一个新的 ssh 私钥,并将其添加到 ssh 认证代理中。
检查 ssh 认证代理缓存的密钥:
$ ssh-add -l
2048 63:0c:a6:51:63:c1:35:76:5d:02:77:97:39:48:0e:4a /Users/jiangxin/.ssh/github_rsa (RSA)

无论何时连接到github.com或其他ssh服务,此密钥首先被使用。

使用以下命令清除ssh-agent中的缓存密钥:

$ ssh-add -D

现在ssh客户端应该正常工作,使用在~/.ssh/config或~/.ssh/id_rsa中定义的密钥。

0
错误是因为ssh-add不知道如何与认证代理通信。 可以通过设置SSH_AUTH_SOCK环境变量来解决问题。
如果您运行ssh-agent,您应该会得到类似以下输出的一些内容:
SSH_AUTH_SOCK=/tmp/ssh-agVZL13989/agent.13989; export SSH_AUTH_SOCK;
SSH_AGENT_PID=13990; export SSH_AGENT_PID;
echo Agent pid 13990;SSH_AUTH_SOCK=/tmp/ssh-agVZL13989/agent.13989; export SSH_AUTH_SOCK;
SSH_AGENT_PID=13990; export SSH_AGENT_PID;
echo Agent pid 13990;

执行这个代码:
eval $(ssh-agent)

然后:

ssh-add -D

亲爱的Sankalp,你的回答太简短了,应该提供一些额外的信息以防止它被删除(已经被另一个用户标记为删除)。 - furins
1
谢谢@furins。我会写一些信息。 - Sankalp Singha
为什么要使用 ssh-add -D 命令?这只会删除您的身份验证信息,不会帮助解决问题。 - Jay Killeen
然后使用 ssh-add 将其添加回去。 - Kiry Meas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接