18

Whenever I enter a git command in powershell, I'm asked:

Enter passphrase for key '/c/Users/***/.ssh/id_rsa'

This key is created with a passphrase:

ssh-keygen -t rsa -b 4096 -C "myemail@somewhere.com" 

I've setup ssh-agent to load on startup of my powershell, and the environment variables SSH_AUTH_SOCK and SSH_AGENT_PID are setup. The connection to github is successful:

ssh -T git@github.com replies with: Hi tkvw! You've successfully authenticated, but GitHub does not provide shell access.

This shows the git urls are used instead of https.

git remote -v
origin  git@github.com:tkvw/pshazz.git (fetch)
origin  git@github.com:tkvw/pshazz.git (push)

This shows the agent is active and the key is loaded:

ssh-add -l
4096 b1:73:2a:11:....63:e8:2a:34 /c/Users/***/.ssh/id_rsa (RSA)

But calling a git operation:

git fetch 
Enter passphrase for key '/c/Users/***/.ssh/id_rsa':

I have no ideas left. I don't want to use git bash or something, I like this to work from powershell.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Dennie de Lange
  • 2,776
  • 2
  • 18
  • 31

5 Answers5

40

None of these worked for me. The solution on this page did though: https://gist.github.com/danieldogeanu/16c61e9b80345c5837b9e5045a701c99

Here it is:

You should not use the Open SSH client that comes with Git for Windows. Instead, Windows 10 has its own implementation of Open SSH that is integrated with the system. To achieve this:

  1. Start the ssh-agent from Windows Services:
  • Type Services in the Start Menu or Win+R and then type services.msc to launch the Services window;
  • Find the OpenSSH Authentication Agent in the list and double click on it;
  • In the OpenSSH Authentication Agent Properties window that appears, choose Automatic from the Startup type: dropdown and click Start from Service status:. Make sure it now says Service status: Running.
  1. Configure Git to use the Windows 10 implementation of OpenSSH by issuing the following command in Powershell: git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe;

  2. Configure SSH to automatically add the keys to the agent on startup by editing the config file found at C:\Users\%YOUR_USERNAME%\.ssh\config, and add the following lines:

Host *
    AddKeysToAgent yes
    IdentitiesOnly yes

You can also add the following lines if you generated an SSH key with custom name or multiple SSH keys:

Host github.com
    HostName github.com
    User your_user_name
    IdentityFile ~/.ssh/your_file_name
  1. Add your SSH key to the ssh-agent by issuing the ssh-add command and entering your passphrase:
ssh-add $HOME/.ssh/your_file_name
  1. Done! Now restart your Powershell and even Windows if necessary.
Philip Beber
  • 1,115
  • 12
  • 18
  • I had to run the command ```Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0``` to add the server before I was able to get this working. I followed the instructions from: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse – Daniel Cumings Jun 07 '21 at 14:23
  • That was it, thank you Philip!! – Hans Nov 22 '22 at 12:19
  • Any idea why only worked for me specifying the domain instead of *? `Host domain.com AddKeysToAgent yes IdentitiesOnly yes` Thank you anyway – Claudio Torres May 03 '23 at 15:42
5

I'm using openssh, which is shipped with Windows 10 Pro. git is installed with scoop (scoop install git).

I had to set GIT_SSH environment variable.

$env:GIT_SSH = (gcm ssh | select -expand source) or for scoop users $env:GIT_SSH = (scoop which ssh)

xoryves
  • 1,447
  • 18
  • 13
  • Windows 10 and Git seem to still have the problem out of the box of not working nicely together. (Windows 10 ver: "Microsoft Windows [Version 10.0.18362.449]"). Issuing the command `$Env:GIT_SSH=$((Get-Command -Name ssh).Source)` corrects the issue. Setting it into my user environment (Open explorer, right-click 'This PC'->Properties, 'Advanced system settings', 'Environment Variables...', under 'User variables for me' click New... and add GIT_SSH=[path-to-ssh.exe]) corrects it for all sessions. – d3r3kk Nov 09 '19 at 21:21
3

You're seeing this prompt because the SSH agent you configured with Git isn't running or doesn't exist.

I installed Git with choco install git and configured it with Window's implementation of Open SSH.

Here's an overview with Windows Powershell in Admin Mode:

  • Start the Windows Open SSH service

    Start-Service ssh-agent

  • Verify that the service is running

    Get-Service ssh-agent

  • Get the location of the service

    Get-WmiObject win32_service | ?{$_.Name -like 'ssh-agent'} | select PathName

    or if it doesn't work:

    Get-CimInstance win32_service | ?{$_.Name -like 'ssh-agent'} | select PathName

  • Automatically start the service

    Set-Service ssh-agent -StartupType Automatic

  • Configure Git to use Windows Open SSH service

    git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe

  • Create SSH config file

    New-Item ~/.ssh/config

  • With contents

    Host *
      AddKeysToAgent yes
      IdentitiesOnly yes
    
  • Add private key (id_ed25519, id_rsa, etc.) to SSH agent. Note that you will be prompted for the passphrase.

    ssh-add ~/.ssh/private_key

At this point, you can perform Git operations pull, push, fetch, etc. without passphrase.

Saurabh
  • 5,176
  • 4
  • 32
  • 46
1

I am using scoop to install the git and openssh packages. I removed these packages and installed the package git-with-openssh and it works now. Not entirely sure what's the difference between the packages, but I expect incompatible versions or something.

Dennie de Lange
  • 2,776
  • 2
  • 18
  • 31
0

You might need to use posh-git (a PowerShell module that integrates Git and PowerShell)

Then see "Using git with ssh-agent on Windows" in order to start the ssh agent within your Powershell session:

Import-Module ~\Documents\WindowsPowerShell\Modules\posh-git\posh-git
Set-Alias ssh-agent "$env:ProgramFiles\git\usr\bin\ssh-agent.exe"
Set-Alias ssh-add "$env:ProgramFiles\git\usr\bin\ssh-add.exe"
Start-SshAgent -Quiet
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250