3

I am trying to add an ssh key to the git ssh-agent in windows, from powershell. However, when I add it it adds it to the regular SSH agent but not the git one. I read this stackoverflow answer of it using a different SSH-agent. I tried starting the agent running C:\Program Files\Git\cmd> .\start-ssh-agent.cmd and it starts properly however when i try to do any ssh-add commands I get the error:

Error connecting to agent: No such file or directory

All the docs I have found is of adding the key to the .ssh file and then starting the ssh agent. is there a way to get files outside that directory in the Git SSH agent?

2
  • Git just runs ssh, so it's more typical to use the system's ssh with Git, which makes the system's ssh-agent also work. I don't use Windows so I'm not sure when it might be reasonable to try to set up a whole separate ecosystem here, though.
    – torek
    Commented Mar 27, 2021 at 0:44
  • Git on windows does have their own system, I think it was because it was created before windows did ssh natively Commented Mar 27, 2021 at 13:47

2 Answers 2

6

From this issue I was able to find that you can change git to use the windows ssh agent by running the following command git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"

0
1

Another option (if you don't use modern slow fat buggy version of Windows) would be to add start-ssh-agent.cmd to startup and make it universal for all shells. This can be done by placing it f.ex. in your .ssh dir, making symlink to startup folder (with minimized run option) and applying the following patch to it:

--- "C:\\Program Files\\Git\\cmd\\start-ssh-agent.cmd"  2023-06-01 16:34:16.000000000 +0300
+++ start-ssh-agent.cmd 2023-08-09 00:31:44.304425700 +0300
@@ -25,6 +25,7 @@
     @FOR %%s IN ("!SSH_AGENT!") DO @SET BIN_DIR=%%~dps
     @FOR %%s in ("!BIN_DIR!") DO @SET BIN_DIR=!BIN_DIR:~0,-1!
     @FOR /D %%s in ("!BIN_DIR!\ssh-add.exe") DO @SET SSH_ADD=%%~s
+    @FOR /D %%s in ("!BIN_DIR!\cygpath.exe") DO @SET CYGPATH=%%~s
     @IF NOT EXIST "!SSH_ADD!" @GOTO ssh-agent-done
     @REM Check if the agent is running
     @FOR /f "tokens=1-2" %%a IN ('tasklist /fi "imagename eq ssh-agent.exe"') DO @(
@@ -77,9 +78,11 @@
 :failure
 
 @ENDLOCAL & @SET "SSH_AUTH_SOCK=%SSH_AUTH_SOCK%" ^
-          & @SET "SSH_AGENT_PID=%SSH_AGENT_PID%"
+          & @SET "SSH_AGENT_PID=%SSH_AGENT_PID%" ^
+          & @SET "CYGPATH=%CYGPATH%"
 
-@ECHO %cmdcmdline% | @FINDSTR /l "\"\"" >NUL
-@IF NOT ERRORLEVEL 1 @(
-    @CALL cmd %*
+@for /f %%c in ('"%CYGPATH%" -m %SSH_AUTH_SOCK%') do @(
+    @setx SSH_AUTH_SOCK "%%c" > nul
+    @set "SSH_AUTH_SOCK=%%c"
 )
+@setx SSH_AGENT_PID %SSH_AGENT_PID% > nul

Thus you will get SSH_AUTH_SOCK in every shell usable. To the bottom of that file you can add any of your ssh-add commands.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.