Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remember git passphrase in WSL

I run Windows 10 with WSL. I have the desired behaviour on one computer, but cannot replicate elsewhere. Here's what I'm after:

  1. First time I run a remote git command using my ssh key, git prompts me for the passphrase
  2. Subsequent times no prompt, including in new terminal windows (I use ConEmu)
  3. When all console windows are closed, back to #1

Things I've tried:

  • using eval $(ssh-agent), followed by ssh-add; it will remember the passphrase, but if I put it in my ~/.bash_profile then it prompts me for every new console window, and I open a lot - many of which I'm not using git in.
  • setting git config --global credential.helper to cache or store
  • everything here
  • using bash.exe and wsl.exe to get git-credentials-manager.exe to work

Here's an example of what I've put in my ~/.gitconfig: [credential] helper = "/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

I've installed the git credential manager from here and have also tried the main Git For Windows installation as an alternative.

How can I encourage it to remember my passphrase?

like image 463
Conan Avatar asked Sep 20 '18 11:09

Conan


People also ask

How do I find my Git passphrase?

If you lose your SSH key passphrase, there's no way to recover it. You'll need to generate a brand new SSH keypair or switch to HTTPS cloning so you can use a personal access token instead. If you lose your SSH key passphrase, there's no way to recover it.

How do I get Git to stop asking for passphrase?

You can avoid being prompted for your password by configuring Git to cache your credentials for you. Once you've configured credential caching, Git automatically uses your cached personal access token when you pull or push a repository using HTTPS.

How can I have Git remember the passphrase for my key on Windows?

Add your key to the ssh-agent Open your shell of preference (I'll use Windows Powershell in this example, applies to Powershell Core too). Add your SSH key to the ssh-agent : ssh-add (you can add the path to your key as the first argument if it differs from the default). Enter your passphrase if/when prompted to do so.

How do I save my SSH key passphrase?

Saving your passphrase in the keychain The first time you use your key, you will be prompted to enter your passphrase. If you choose to save the passphrase with your keychain, you won't have to enter it again. Otherwise, you can store your passphrase in the keychain when you add your key to the ssh-agent.


3 Answers

I tried the option to add AddKeysToAgent yes to ~/.ssh/config but it doesn't keep it between new tabs on the terminal.

The best solution I found so far is to do the following:

sudo apt install keychain

Find your hostname using the terminal:

hostname

Then add the following to your ~/.bashrc or ~/.zshrc file:

/usr/bin/keychain --nogui ~/.ssh/id_rsa
source $HOME/.keychain/YOUR-HOSTNAME-HERE-sh

Now, each time you reboot, you’ll have to enter your passphrase. But you only have to do it one time until you reboot or terminate WSL.

If you want to use the same key you already have on Windows you can follow this post Sharing SSH keys between Windows and WSL 2

like image 65
Marcelo Dapper Avatar answered Nov 02 '22 14:11

Marcelo Dapper


I found the answer!

First, make sure you have ssh-agent running all the time by adding eval $(ssh-agent) to your .bash_profile.

Then add AddKeysToAgent yes to your ssh config:

touch ~/.ssh/config
chmod 600 ~/.ssh/config
echo "AddKeysToAgent yes" >> ~/.ssh/config

You'll get prompted when you first do some ssh, but the passphrase will be automatically added to the ssh-agent so you won't have to type it again until you end your session and start a new one.

like image 43
Conan Avatar answered Nov 02 '22 15:11

Conan


I tried both methods in previous answers (as well as others found elsewhere) on WSL 2 and they either did not work or had caveats I couldn't live with. This is what worked for me.

Install keychain:

sudo apt install keychain

Then add the following line to your shell's configuration file (likely ~/.bashrc or ~/.zshrc):

eval `keychain --quiet --eval --agents ssh id_rsa`

Now you will only have to enter your password when booting WSL!

Thank you Birk Holland for this article.

like image 16
Nolan Strait Avatar answered Nov 02 '22 15:11

Nolan Strait