Permission denied (publickey) when deploying herok

2019-01-02 19:20发布

I'm attempting to deploy my code to heroku with the following command line:

git push heroku master

but get the following error:

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

I have already uploaded my public SSH key, but it still comes up with this error.

30条回答
永恒的永恒
2楼-- · 2019-01-02 19:44

You have to upload your public key to Heroku:

heroku keys:add ~/.ssh/id_rsa.pub

If you don't have a public key, Heroku will prompt you to add one automatically which works seamlessly. Just use:

heroku keys:add

To clear all your previous keys do :

heroku keys:clear

To display all your existing keys do :

heroku keys

EDIT:

The above did not seem to work for me. I had messed around with the HOME environment variable and so SSH was searching for keys in the wrong directory.

To ensure that SSH checks for the key in the correct directory do :

ssh -vT git@heroku.com

Which will display the following ( Sample ) lines

OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
debug1: Connecting to heroku.com [50.19.85.156] port 22.
debug1: Connection established.
debug1: identity file /c/Wrong/Directory/.ssh/identity type -1
debug1: identity file /c/Wrong/Directory/.ssh/id_rsa type -1
debug1: identity file /c/Wrong/Directory/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version Twisted
debug1: no match: Twisted
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.6
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: sending SSH2_MSG_KEXDH_INIT
debug1: expecting SSH2_MSG_KEXDH_REPLY
debug1: Host 'heroku.com' is known and matches the RSA host key.
debug1: Found key in /c/Wrong/Directory/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /c/Wrong/Directory/.ssh/identity
debug1: Trying private key: /c/Wrong/Directory/.ssh/id_rsa
debug1: Trying private key: /c/Wrong/Directory/.ssh/id_dsa
debug1: No more authentication methods to try.

Permission denied (publickey).

From the above you could observe that ssh looks for the keys in the /c/Wrong/Directory/.ssh directory which is not where we have the public keys that we just added to heroku ( using heroku keys:add ~/.ssh/id_rsa.pub ) ( Please note that in windows OS ~ refers to the HOME path which in win 7 / 8 is C:\Users\UserName )

To view your current home directory do : echo $HOME or echo %HOME% ( Windows )

To set your HOME directory correctly ( by correctly I mean the parent directory of .ssh directory, so that ssh could look for keys in the correct directory ) refer these links :

  1. SO Answer on how to set Unix environment variable permanently

  2. SO Question regarding ssh looking for keys in the wrong directory and a solution for the same.

查看更多
萌妹纸的霸气范
3楼-- · 2019-01-02 19:44

If you are a windows user the other solutions here probably won't solve your problem.

I use Windows 7 64-Bit + Git-1.7.7.1-preview20111027 and the solution was to copy my keys from C:\users\user\.ssh to C:\Program Files (x86)\Git\.ssh. That's where this git client looks for the keys when pushing to heroku.

I hope this helps.

查看更多
一个人的天荒地老
4楼-- · 2019-01-02 19:46

Here is the link that explains how to manage your ssh keys : http://devcenter.heroku.com/articles/keys#adding_keys_to_heroku

查看更多
弹指情弦暗扣
5楼-- · 2019-01-02 19:46

Pushing was working for me and then stopped suddenly.

If the heroku api is experiencing downtime, you will get this error when you try to push.

Check:

https://status.heroku.com/

before freaking out too hard.

查看更多
弹指情弦暗扣
6楼-- · 2019-01-02 19:46

I was experiencing the same problem; following these steps should help:

  1. First, log in: heroku login
  2. Clear all keys: heroku keys:clear
  3. Delete all files in local folder ( all .pub files and know_host) in .ssh/ folder
  4. Log in again : heroku login - u will prompt with no key, so follow the onscreen instructions.
查看更多
浮光初槿花落
7楼-- · 2019-01-02 19:47

At first make sure hidden files are visible in your Mac. If not do:

  • Open terminal and type in defaults write com.apple.Finder AppleShowAllFiles TRUE
  • killall Finder

Next steps:

  • Going to Users/user_name/.ssh/ removed all the files.
  • Opening terminal type in ssh-keygen -t dsa
  • Then heroku keys:add ~/.ssh/id_dsa.pub

N.B. I did it in Mac OSX 10.7.2 Lion. Though the procedure should be same in others too.

查看更多
登录 后发表回答