Git keeps asking me for my ssh key passphrase

Go To StackoverFlow.com

420

I'm using Linux and I created keys as instructed in the github tutorial, registered them with github, and tried using ssh-agent explicitly — yet git continues to ask me for my passphrase every time I try to do a pull or a push.

What could be the cause?

2012-04-05 16:33
by Rogach
@uDaY - I've already tried to start ssh-agent - it didn't help - Rogach 2012-04-05 16:39
Recently there was breach in github so they wanted you to explicitly audit your ssh keys did you do it? with the one you primarily use - uday 2012-04-05 16:45
@uDaY - yes, I did. That same day. Now I try to do it from my another machine, with the newly generated key - Rogach 2012-04-05 16:47
@dm03514 - yes - Rogach 2012-04-05 16:48
Related: http://stackoverflow.com/questions/14762034/push-to-github-without-password-using-ssh-ke - Jeff 2015-06-16 05:05


838

Once you have started the SSH agent with:

eval $(ssh-agent)
  1. You have to add your private key to it:

    ssh-add
    

    This will ask you your passphrase just once, and then you should be allowed to push, provided that you uploaded the public key to Github.

  2. To save key permanently:

    ssh-add -K  
    

    This will persist it after you close and re-open it by storing it in user's keychain.

2012-04-05 16:47
by Roberto Bonvallet
log-out of the server, ssh back in, run git pull, prompt me for password again - kyo 2016-10-03 22:31
This didn't work for me. It is still asking for passphrase when I close Gitbash and open it again - Omar Tariq 2016-11-04 15:19
@OmarTariq If you close your git-bash session, you lose the ssh-agent instance. Some desktop environments launch a global agent so added keys are shared across terminal sessions, but that's specific to the OS you are using - Roberto Bonvallet 2016-11-04 15:29
Solution worked - avalon 2016-11-21 07:23
I'd just like to add another bit of info: If you have multiple ssh keys, you may want to specify the path to a particular id_rsa in your ssh-add command, e.g. ssh-add /Users/rubyx/.ssh/git/id_rsaRubix 2017-01-04 17:08
ssh-add -K will persist it after you close and re-open it by storing it in user's keychain - Kirk 2017-01-07 01:05
@Kirk ssh-add -K gives the following: unknown option -- K usage: ssh-add [options] [file ...] Options: -l List fingerprints of all identities. -L List public key parameters of all identities. -k Load only keys and not certificates. -c Require confirmation to sign using identities -t life Set lifetime (in seconds) when adding identities. -d Delete identity. -D Delete all identities. -x Lock agent. -X Unlock agent. -s pkcs11 Add keys from PKCS#11 provider.Sandeep C 2017-02-12 10:35
This just prevents you having to retype it repeatedly. If you literally don't want any security, ever, then don't put a password on the key - Kenny Ostrom 2017-03-01 17:09
I still get prompted for a passphrase regardless - IgorGanapolsky 2017-03-09 20:37
I have to do this every few months on one of my macs but not another. Why would this be - hawkeye126 2017-04-03 14:26
If you have Sierra, you have to add it to your ssh/config file. Look for that answer lower on the page - Joshua Dance 2017-09-21 06:25
I had to do this ssh-add -K ~/.ssh/id_rsa for it to persist on Mac - igneosaur 2018-02-15 17:11
This was not working for me on Windows. But the following command solved my problem: git config credential.helper store After that I was asked one more time for my credentials, but after that no more - eztam 2018-05-14 14:51
-K is apple specific. See https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#adding-your-ssh-key-to-the-ssh-agen - bkdir 2018-07-17 15:32
whats the linux idiomatic way to do the same - Vincent Buscarello 2018-10-24 16:50
@VincentBuscarello Yes, it is - Roberto Bonvallet 2018-10-24 18:13
The question was about Linux specifically, and here is an answer for MAC that doesn't work on linux?? wtf.. - OZZIE 2018-12-07 07:39
This works for me - Felix Jr 2018-12-10 12:45
why i'm getting this warning; Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts - Ahmed bhs 2018-12-29 12:37
The option in linux is -k in lower case - Paulo zOiO zuLLu Fabrício 2019-02-21 16:59


180

This has been happening to me after restarts since upgrading from OS X El Capitan (10.11) to macOS Sierra (10.12). The ssh-add solution worked temporarily but would not persist across another restart.

The permanent solution was to edit (or create) ~/.ssh/config and enable the UseKeychain option.

Host *
    UseKeychain yes

Related: macOS keeps asking my ssh passphrase since I updated to Sierra

2017-01-10 18:51
by Kyle Clegg
I have no idea why Sierra started asking me this one day, but this worked. Thanks! (especially since I can't remember the passphrase! - Nicolas Miari 2017-01-17 12:31
worked for me thanks... I had done... ssh-add -K /Users/***/.ssh/git/id_rsa but it was still not working after terminal restart... thank you - nawlbergs 2017-01-19 15:12
I'm using mac and your answer is the only one which worked for me. Thanks a lot - sunghun 2017-04-22 11:40
Want to why this happen? Read the technical note on OpenSSH changes https://developer.apple.com/library/content/technotes/tn2449/_index.htm - samwize 2017-04-25 15:38
This solution worked for me. I had the same problem after upgrading to Sierra - danmakenoise 2017-09-05 14:49
Works, 100%! Great answer - Hlawuleka MAS 2017-09-19 20:00
Worked for me on High Sierra. Thanks - Matthew Blewitt 2018-07-11 10:00


57

If you've tried ssh-add and you're still prompted to enter your passphrase then try using ssh-add -K. This adds your passphrase to your keychain.

Update: if you're using macOS Sierra then you likely need to do another step as the above might no longer work. Add the following to your ~/.ssh/config:

Host *
  UseKeychain yes
2016-12-19 10:20
by Darryl Young
Still prompts me for a passphrase - IgorGanapolsky 2017-03-09 20:37
@IgorGanapolsky Are you using macOS Sierra? If so, check out what I just added to the answer. I hope that helps - Darryl Young 2017-03-11 08:30
Yes, I am on Sierra. However, here is the line that worked worked for me too: AddKeysToAgent yesIgorGanapolsky 2017-03-13 13:21


41

I would try the following:

  1. Start GitBash
  2. Edit your ~/.bashrc file
  3. Add the following lines to the file

SSH_ENV=$HOME/.ssh/environment

# start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."
    # spawn ssh-agent
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
    echo succeeded
    chmod 600 ${SSH_ENV}
    . ${SSH_ENV} > /dev/null
    /usr/bin/ssh-add
}

if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi
  1. Save and close the file
  2. Close GitBash
  3. Reopen GitBash
  4. Enter your passphrase
2013-10-01 07:03
by Roland
For future reference, this also works with zsh. Just add this to .zshrcArda 2015-07-21 11:11
This worked for me, each time I was opening up bash in the repo it would ask for the pass phrase, all fixed now thanks - David 2016-03-11 18:45
I believe this assumes your keyfile is called id_rsa. If you have a custom name, you should use eg. /usr/bin/ssh-add ~/.ssh/custom_filenameJuha Untinen 2016-08-04 09:37
It worked well with Git Bash on Windows 7! Thanks - Kota Mori 2016-09-23 01:39
This worked for me too - Christoph Pader 2017-07-17 06:59
Can you explain what happen to this script - Lee 2018-10-31 04:54
@Lee it starts the ssh-agent if not already running, which is what makes sure not to ask you the ssh passphrase on every push (more or less) - Roland 2018-10-31 08:12
thank you finally an answer that works in Linux which the question was about.. wtf is wrong with people.. If you want to know about Mac make a new f*ing question : - OZZIE 2018-12-13 10:47


15

Try adding this to your ~/.ssh/config:

Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa

... assuming your private key is named id_rsa

2017-03-09 20:40
by IgorGanapolsky
UseKeychain yes is only valid for ma - Oz123 2018-04-13 07:26
@Oz123 what's the equivalent for window - IgorGanapolsky 2018-04-13 13:32
No clue, I use GNU\Linux .. - Oz123 2018-04-13 17:18
This did the trick for me on MacOS - Shobhit Chittora 2018-07-25 08:28


11

What worked for me on Windows was (I had cloned code from a repo 1st):

eval $(ssh-agent)
ssh-add 
git pull 

at which time it asked me one last time for my passphrase

Credits: the solution was taken from https://unix.stackexchange.com/questions/12195/how-to-avoid-being-asked-passphrase-each-time-i-push-to-bitbucket

2016-04-20 19:38
by Arthur
Not much love for Windows users sometimes. Thanks for this - Christopher Ronning 2016-10-11 00:55
Question was about Linux! - OZZIE 2018-12-13 10:48


5

I had a similar issue, but the other answers didn't fix my problem. I thought I'd go ahead and post this just in case someone else has a screwy setup like me.

It turns out I had multiple keys and Git was using the wrong one first. It would prompt me for my passphrase, and I would enter it, then Git would use a different key that would work (that I didn't need to enter the passphrase on).

I just deleted the key that it was using to prompt me for a passphrase and now it works!

2013-03-05 19:40
by 11101101b


4

It sounds like you may be having trouble with SSH-Agent itself. I would try troubleshooting that.

1) Did you do ssh-add to add your key to SSH?

2) Are you closing the terminal window between uses, because if you close the window you will have to enter the password again when you reopen it.

2012-04-05 16:40
by Jeff Welling
How should I do it - Rogach 2012-04-05 16:43
Would be nice to add information on how to solve the first problem Jeff Welling - ranu 2016-08-19 15:13


2

If the above solutions are not working for me, one thing to check is that you actually have the public key too (typically id_rsa.pub). It is unusual not to, but that was the cause for me.

To create your public key from your private key:

ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
2017-04-27 09:51
by Stephen Harris


1

I try different solutions but nothing help. But this steps (My GitBash SSH environment always asks for my passphrase, what can I do?) from Bitbucket.com seams works well :

The idea is:

  1. you create ~/.bashrc file

  2. add follow script:

    SSH_ENV=$HOME/.ssh/environment
    
    # start the ssh-agent
        function start_agent {
        echo "Initializing new SSH agent..."
        # spawn ssh-agent
        /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
        echo succeeded
        chmod 600 "${SSH_ENV}"
        . "${SSH_ENV}" > /dev/null
        /usr/bin/ssh-add
    }
    
    if [ -f "${SSH_ENV}" ]; then
         . "${SSH_ENV}" > /dev/null
         ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
            start_agent;
        }
    else
        start_agent;
    fi
    
  3. re-run Bash

2017-08-15 12:25
by Alx Lark


-1

To avoid git being asking for password once you have generated and configured ssh keys, don't use https, instead use:

git@bitbucket.org:<repo_owner>/<reponame>.git

or

ssh://git@bitbucket.org/<repo_owner>/<reponame>.git
2018-10-05 07:42
by Dalton
Question has already mentioned that the repo is closed using SSH, and not HTTPS - Jabbar_Jigariyo 2018-11-26 15:35
Ads