Last Updated: February 25, 2016
· codemangler

Switching SSH keys between Heroku accounts

Do you work with multiple Heroku accounts? Have you run into errors like this:

Your key with fingerprint: ... is not authorized to access <application-name>.
fatal: the remote end hung up unexpectedly

You can solve this by configuring your SSH client, using SSH config files, to present the correct keys to the server, but I find this a bit tedious.

Instead, I just make sure that the SSH client has just one key to present to the server, and that it's the right key. Here's what I do:

  • Clear all active keys
$ ssh-add -D
  • ssh-add the correct key
$ ssh-add ~/.ssh/an_ssh_key
  • Start using the key (in this case, use it to push to Heroku)
$ git push heroku-remote master

If you noticed, this technique isn't really specific to Heroku. It's useful whenever you have to work with multiple SSH accounts on the same host, like switching between multiple GitHub accounts.

Note: On Linux, if you’re on GNOME, the gnome-keyring-daemon keeps adding keys back to the auth agent as you keep trying to remove them with ssh-add -D. So, it’ll appear as if the command is not working. The solution is to disable the damn thing (Google for it). I find the daemon annoying for the popups it keeps throwing at me, so personally, I’d be glad to see it gone.

Finally, if your usecase is specific to Heroku, i.e. if you only have to switch between Heroku accounts, there's a nice Heroku plugin for that. Checkout heroku-accounts on GitHub:

Read more

4 Responses
Add your response

a very helpful tips. i was looking for this. thanks man.

over 1 year ago ·

glad to know you found it useful :)

over 1 year ago ·

To switch between multiple Github accounts I have already built an ad-hoc solution : github-keygen. I'm using host aliases for the remotes to select the SSH key to use just once when I clone a repo.

over 1 year ago ·

Cool :)
I'll check it out.

over 1 year ago ·