126

I've an automatic building service which download from a git private repository. The problem is that when it tries to clone repository it need to provide the password, because it is not remembered; so because there is no human interaction, it waits forever the password. How can I force it to remember from id_rsa.pub?

030
  • 8,013
  • 8
  • 63
  • 100
D.Giunchi
  • 1,794
  • 2
  • 17
  • 22

7 Answers7

257

For Windows users, just a note that this is how I set up the Git Bash environment to log me in once when I start it up. I edit my ~/.bashrc file:

eval `ssh-agent`
ssh-add

So when I start Git Bash, it looks like:

Welcome to Git (version 1.7.8-preview20111206)
(etc)
Agent pid 3376
Enter passphrase for /c/Users/starmonkey/.ssh/id_dsa:
Identity added: /c/Users/starmonkey/.ssh/id_dsa (/c/Users/starmonkey/.ssh/id_dsa)

And now I can ssh to other servers without logging in every time.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
starmonkey
  • 3,087
  • 2
  • 18
  • 15
  • It is :) Thank you. Didn't know about `.bashrc`. – Ry- Jan 26 '12 at 02:00
  • 9
    If you don't have a ~/.bashrc file then just create a new text file (notepad or some other editor) and add the two lines starmonkey mentioned. – pbz Apr 27 '12 at 03:03
  • 8
    '~' refers to your "home directory". In Windows, you can find this by opening a command shell (cmd) and typing "echo %USERPROFILE%". – Hawkeye Parker Aug 28 '12 at 23:56
  • 14
    For me this syntax is not working. I had to write `eval $(ssh-agent)` instead. – clash Jan 27 '14 at 11:21
  • Everything's ok but now I have to type my passphrase + every git command I have to type password for server's shell connection. Generally - not working. – versedi Apr 28 '15 at 12:52
  • 1
    My issue was that I needed to specify the file for ssh-add to use. Probably because I have more than one and wasn't using the default name. Example `ssh-add ~/.ssh/myfile_rsa` – Syntax Error Jun 30 '15 at 15:32
  • Works great. Using @versedi's comment, I was able to add multiple keys, by adding multiple lines of `ssh-add //path/to/mykey_rsa`. – gligoran Oct 13 '15 at 08:58
  • nice ! I was looking for a while to cache my passphrase on windows, now its done, THX :) – altore Oct 14 '15 at 07:47
  • This didn't work for me. I took the route similar to *clash* and used `eval $('ssh-agent')` then `ssh-add` then gave it my password and now I'm not asked for my password anymore. – Rockin4Life33 May 14 '16 at 18:44
  • Didn't work for me. Stephen Tun Aung's answer to use credential.helper wincred does work. – Michael Freidgeim Nov 19 '16 at 05:08
  • 1
    `copy > ~/.bashrc` in git bash to create the bashrc file in windows, ignore the error – Ruben May 30 '17 at 12:27
  • The instructions works pretty fine for me in Windows 7 and git bash. The only detail is that I needed to change the `bash.bashrc` file in the installation directory - in my case `C:\Program Files\Git\etc\bash.bashrc` – Dimitar Ivanov Aug 23 '18 at 07:31
  • Not sure if there are anyway to save the pass phrases after exit/re-open the git bash (Windows 10). There are a tons of guide about SSH Key, but no one resolve the pass phrases still ask everytime open git bash, it's quite annoyance. – Loc_rabbirt Jan 05 '21 at 14:37
61

This answer explains how to get the GitHub username and password to be stored permanently, not the SSH key passphrase.

In Windows, just run

$ git config --global credential.helper wincred

This means that the next time you push, you'll enter your username and password as usual, but they'll be saved in Windows credentials. You won't have to enter them again, after that.

As in, Push to GitHub without entering username and password every time (Git Bash on Windows).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Stephen Tun Aung
  • 924
  • 8
  • 18
9

I prefer not to have to type my SSH passphrase when opening new terminals; unfortunately starmonkey's solution requires the password to be typed in for every session. Instead, I have this in my .bash_profile file:

# Note: ~/.ssh/environment should not be used, as it
#       already has a different purpose in SSH.

env=~/.ssh/agent.env

# Note: Don't bother checking SSH_AGENT_PID. It's not used
#       by SSH itself, and it might even be incorrect
#       (for example, when using agent-forwarding over SSH).

agent_is_running() {
    if [ "$SSH_AUTH_SOCK" ]; then
        # ssh-add returns:
        #   0 = agent running, has keys
        #   1 = agent running, no keys
        #   2 = agent not running
        ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
    else
        false
    fi
}

agent_has_keys() {
    ssh-add -l >/dev/null 2>&1
}

agent_load_env() {
    . "$env" >/dev/null
}

agent_start() {
    (umask 077; ssh-agent >"$env")
    . "$env" >/dev/null
}

if ! agent_is_running; then
    agent_load_env
fi

# If your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
    agent_start
    ssh-add
elif ! agent_has_keys; then
    ssh-add
fi

unset env

This will remember my passphrase for new terminal sessions as well; I only have to type it in once when I open my first terminal after booting.

I'd like to credit where I got this; it's a modification of somebody else's work, but I can't remember where it came from. Thanks anonymous author!

Update 2019-07-01: I don't think all this is necessary. I now consistently have this working by ensuring my .bash_profile file runs the ssh-agent:

eval $(ssh-agent)

Then I set up an ssh configuration file like this:

touch ~/.ssh/config
chmod 600 ~/.ssh/config
echo 'AddKeysToAgent yes' >> ~/.ssh/config
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Conan
  • 1,971
  • 22
  • 38
  • I still get asked for my passphrase every time. – Ryan Jan 20 '17 at 14:15
  • 1
    @Ryan hopefully the update I've just added will address your problem. I keep the info up to date in a blog post at http://conan.is/blogging/clojure-on-windows.html, and I asked the same question myself at https://stackoverflow.com/questions/52423626/remember-git-passphrase-in-wsl – Conan Jan 07 '19 at 10:32
  • @Conan The updated solution work per bash session. After I close my running bash session and opened a new one, I was again prompted for entering password. – Tushar Raj Feb 07 '19 at 06:52
5

If I understand the question correctly, you're already using an authorized SSH key in the build service, but you want to avoid having to type the passphrase for every clone?

I can think of two ways of doing this:

  1. If your build service is being started interactively: Before you start the build service, start ssh-agent with a sufficiently long timeout (-t option). Then use ssh-add (msysGit should have those) to add all the private keys you need before you start your build service. You'd still have to type out all the passphrases, but only once per service launch.

  2. If you want to avoid having to type the passphrases out at all, you can always remove the passphrases from the SSH keys, as described in https://serverfault.com/questions/50775/how-do-i-change-my-private-key-passphrase, by setting an empty new passphrase. This should do away with the password prompt entirely, but it is even less secure than the previous option.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
millimoose
  • 36,982
  • 8
  • 75
  • 128
1

When I tried to push my code, I got the below error:

$ git push origin dev

remote: Too many invalid password attempts. Try logging in through the website with your password.
fatal: unable to access 'https://naushadqamar-1@bitbucket.org/xxxx/xxxx-api.git/': The requested URL returned error: 403

After a few hours of research, I found I need to use the below command:

$ git config --global credential.helper cache

After executing the above command, I got the prompt for entering my GitHub username and password. After providing the correct credentials, I am able to push my code.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Naushad Qamar
  • 123
  • 3
  • 12
  • This just generated "git: 'credential-cache' is not a git command." However "git config --global credential.helper store" worked - this may not be the best, but I'm forced to use HTTPS rather than my preferred SSH and just want it to work. – Terry Brown May 30 '18 at 20:05
0

The right solution is:

  1. Run the Windows default terminal - cmd and get the directory of your master profile

    echo %USERPROFILE%
    
  2. Run Git Bash in the directory above and create the .bashrc file with the command

    echo "" > .bashrc
    
  3. Open the .bashrc file with your favourite text editor and paste code from GitHub Help into that file:

    env=~/.ssh/agent.env
    ...
    COPY WHOLE CODE FROM URL - I can't add it to Stack Overflow because it breaks layout... OMG!
    
  4. Restart Git Bash and it asks you for your password (only first time) and done. No password bothering again.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Manic Depression
  • 922
  • 1
  • 14
  • 27
-1

You need to create the authorized_keys file under the .ssh folder of the user under which you are going to connect to the repository server. For example, assuming you use username buildservice on repo.server you can run:

cd ~buidservice
mkdir ./ssh
cat id_rsa.pub >> .ssh/authorized_keys

Then you have to check the following things:

  1. That corresponding id_rsa private key is presented in builservice@build.server:~/.shh/id_rsa.

  2. That fingerprint of repo.server is stored in the buildservice@build.server:~/.ssh/known_hosts file. Typically that will be done after the first attempt of ssh to connect to the repo.server.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
beduin
  • 7,045
  • 3
  • 25
  • 23
  • I forget to say. Under Windows you home directory probably would be C:\Users\Username – beduin Apr 20 '11 at 09:51
  • 5
    Mmm.. Git bash has it all, read more carefully title of the question. Also from question I assumed that key-pair has been already generated (because it was asked how to force repo server to remember id_rsa.pub.) Sorry about formatting. – beduin Apr 20 '11 at 09:56
  • 1
    Ok, never heard of 'git bash' before. Sorry 'bout that – sehe Apr 20 '11 at 09:58
  • I'm assuming this is using msysgit. I'm adding the tag. – Adam Dymitruk Apr 21 '11 at 07:03
  • 3
    Warning! Using `cat id_rsa.pub > .ssh/authorized_keys` will overwrite any existing authorized keys. Use `>>` to add instead of overwrite. – David Pärsson Oct 23 '16 at 11:30