42

I recently reinstalled my laptop and found that 'git clone' wasn't working. I was consistently receiving Permission denied (publickey). errors.

The fix is documented on GitHub's SSH issues page: use ssh-add to tell SSH about the key I want to use to authenticate on GitHub.

My question is: under the hood, what does ssh-add actually do?

I've been meaning to expand my understanding of SSH for a while, & this seems like a good excuse to start :-)

2 Answers 2

47

ssh-add adds private key identities (from your ~/.ssh directory) to the authentication agent (ssh-agent), so that the ssh agent can take care of the authentication for you, and you don’t have type in passwords at the terminal.

3
  • 6
    I'll be honest, this confused me for a while. I thought the ssh client always used the key in your ~/.ssh directory. Do some distros do the equivalent of ssh-add automatically? Maybe that's why I was confused?
    – blong
    Apr 11, 2015 at 1:21
  • 21
    ssh does use the key in the ~/.ssh directory, but if the key is protected by a passphrase, this passphrase must be entered at some point. If you use ssh-agent and ssh-add, you type this passphrase once. Otherwise you must type the passphrase each time you use ssh. Note that if the key is not password protected, you can use ssh without being asked for a passphrase. Jul 10, 2015 at 9:39
  • There can be multiple ssh keys in ~/.ssh and this might be the more useful thing to have, see on SO and bitbucket docs
    – Frank N
    Apr 11, 2018 at 11:01
3

Based on this article in github, sometime on some linux distributions even after you set up everything properly this error shows up:

Agent admitted failure to sign using the key.
debug1: No more authentication methods to try.
Permission denied (publickey).

And you should run ssh-add to solve the issue.

1
  • Worked for me! Would I have to run ssh-add everytime though? I restarted my MacOS and had to run this again.
    – Qasim
    Mar 2, 2021 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.