我正在尝试使用以下命令行将代码部署到heroku:

git push heroku master

但会出现以下错误:

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

我已经上传了我的公共SSH密钥,但仍然会出现此错误。


当前回答

检查heroku的.ssh配置。转到.ssh文件夹并打开配置文件

cd ~/.ssh
subl config

“subl”用于Sublime文本,但您可以使用任何您想要的编辑器。查找“IdentityFile”行,并确保它列出了非公钥:

IdentityFile "/Users/ircmullaney/.ssh/my_ssh"

not

IdentityFile "/Users/ircmullaney/.ssh/my_ssh.pub"

这对我来说很有用。我不知道为什么我的配置文件中有公共版本,但它确实有,并且抛出了错误:

Permissions 0644 for '/Users/ircmullaney/.ssh/my_ssh.pub' are too open.

其他回答

除了处理SSH密钥,您还可以尝试Heroku的新测试版HTTP Git支持。它只使用您的API令牌并在端口443上运行,因此无需使用SSH密钥或端口22。

要使用HTTP Git,首先确保Toolbelt已更新,并且您的凭据是最新的:

$ heroku update
$ heroku login

(这一点很重要,因为Heroku HTTP Git的认证方式与Toolbelt的其他方式略有不同)

在测试期间,通过将--httpgit标志传递给相关的heroku应用程序:create、heroku git:clone和heroku git:remote命令,可以获得HTTP。要创建新的应用程序并使用HTTP Git远程配置它,请运行以下命令:

$ heroku apps:create --http-git

要将现有应用程序从SSH更改为HTTP Git,只需从计算机上的应用程序目录运行以下命令:

$ heroku git:remote --http-git
Git remote heroku updated

有关如何为Heroku设置HTTP Git的详细信息,请查看开发中心文档。

我只想补充一点,该目录不一定是C:\Users\[用户名]\.ssh。它是您在其中创建公钥的目录。

例如,我在Windows中的主目录已更改为C:\[用户名]。ssh子文件夹中的主目录是创建密钥的最佳位置,也是最可能的位置。您可以使用以下命令检查Windows中的主目录:

    echo %HOMEPATH%

如果您是windows用户,这里的其他解决方案可能无法解决您的问题。

我使用的是Windows 7 64位+Git-1.7.7.1-review20111027,解决方案是将我的密钥从C:\users\user\.ssh复制到C:\Program Files(x86)\Git\.ssh。这是Git客户端在推送到heroku时查找密钥的地方。

我希望这有帮助。

分享我的经验:

Git(我自己安装的)正在查找名为“id_rsa”的密钥。

所以我尝试将我的密钥重命名为“id_rsa”和“id_rsa.pub”,结果成功了。

顺便说一句,我肯定还有其他方法可以做到这一点,但我还没有深入研究。

推送时使用

git push heroku production:master 

使用主目录~/.ssh/id_rsa下的公钥

要解决此问题

您应该以其他用户身份登录,可能是root用户

sudo su 

然后通过发出以下命令重新开始

heroku keys:clear //removes existing keys
ssh-keygen -t rsa //generates a new key in ~/.ssh folder (set a password)
heroku keys:add   //uploads the new key, ~/.ssh/id_rsa is uploaded                      
git push heroku production:master