How to run ssh-add on windows?
Asked Answered
C

15

207

I'm following #335 Deploying to a VPS , and near the end of the episode, we need to run ssh-add to give server access to github repo.

The problem is how do I run it in windows? What need to install?

I know that to run ssh to access the remote server, I can use Putty. But this command needs to run locally, I do know how to use Putty to do this.

Cingulum answered 8/9, 2013 at 11:23 Comment(2)
Actually. You don't need to. Use Pageant (Putty's ssh-agent)... If you actually WANT to do it the Unix/Linux way, you need to install gitbash, mingw or somethig similar. With Pageant, you won't have to.Zigzagger
GitHub provides an excellent script to start the ssh-agent and add private keys to it, which script I found through VonC's answer. I recommend you use it, and I reference it in my own answer here: How to get ssh-agent to load your private ssh keys and require their passwords only once per boot in WindowsGrillo
H
403

Original answer using git's start-ssh-agent

Make sure you have Git installed and have git's cmd folder in your PATH. For example, on my computer the path to git's cmd folder is C:\Program Files\Git\cmd

Make sure your id_rsa file is in the folder c:\users\yourusername\.ssh

Restart your command prompt if you haven't already, and then run start-ssh-agent. It will find your id_rsa and prompt you for the passphrase

Update 2019 - A better solution if you're using Windows 10: OpenSSH is available as part of Windows 10 which makes using SSH from cmd/powershell much easier in my opinion. It also doesn't rely on having git installed, unlike my previous solution.

  1. Open Manage optional features from the start menu and make sure you have Open SSH Client in the list. If not, you should be able to add it.

  2. Open Services from the start Menu

  3. Scroll down to OpenSSH Authentication Agent > right click > properties

  4. Change the Startup type from Disabled to any of the other 3 options. I have mine set to Automatic (Delayed Start)

  5. Open cmd and type where ssh to confirm that the top listed path is in System32. Mine is installed at C:\Windows\System32\OpenSSH\ssh.exe. If it's not in the list you may need to close and reopen cmd.

Once you've followed these steps, ssh-agent, ssh-add and all other ssh commands should now work from cmd. To start the agent you can simply type ssh-agent.

  1. Optional step/troubleshooting: If you use git, you should set the GIT_SSH environment variable to the output of where ssh which you ran before (e.g C:\Windows\System32\OpenSSH\ssh.exe). This is to stop inconsistencies between the version of ssh you're using (and your keys are added/generated with) and the version that git uses internally. This should prevent issues that are similar to this

Some nice things about this solution:

  • You won't need to start the ssh-agent every time you restart your computer
  • Identities that you've added (using ssh-add) will get automatically added after restarts. (It works for me, but you might possibly need a config file in your c:\Users\User\.ssh folder)
  • You don't need git!
  • You can register any rsa private key to the agent. The other solution will only pick up a key named id_rsa
Hoarse answered 21/11, 2016 at 12:44 Comment(12)
start-ssh-agent. That's all I needed after an hour of looking around. Thank you!Spectrum
I think start-ssh-agent does work with PowerShell. However, I was running PowerShell as an administrator, so that might be the key.Freemon
For me the windows service "OpenSSH Authentication Agent" was disabled, this helped, thxHomoeroticism
Thank you! I needed to set the GIT_SSH environment variable to point Git to the OpenSSH used by Windows instead of its internal one.Catercornered
any idea how to change the name of the key file to something other than id_rsa?Neckerchief
When I run ssh-add in Powershell as regular user or as Administrator I get ssh-add : The term 'ssh-add' is not recognized.. How do I make ssh-add run in Powershell?Invariant
@Invariant Have you installed the OpenSSH Authentication Agent? If you have, maybe you need to close and reopen powershell.Hoarse
I also needed to change my remote urls to ssh for git to use ssh auth by default. See How do I get git to default to ssh and not https for new repositoriesFineman
This works in 2022 as well, on Windows 10. I completed the procedure described above, and the SSH key was created and registered to the agent.Ilan
You should also tell Git to use the external instead of built-in ssh in Git's install dialogPointer
Type: ssh-add /.ssh/id_rsa or gitea_key. Worked for git bash. But Sourcetree gives an error on start "'ssh-agent' failed with code -1: Sstem.ComponentModel.Win32Exception: Access is denied 0"Lunchroom
Running Windows 10 & Git Bash. After enabling the OpenSSH Authentication Agent, attempts to run ssh-add resulted in an error: "Could not open a connection to your authentication agent." Resolved by first running exec ssh-agent bash, followed by ssh-add. Found solution here: github.com/ritazh/devopsfun/issues/17Lungi
B
138

One could install Git for Windows and subsequently run ssh-add:

Step 3: Add your key to the ssh-agent

To configure the ssh-agent program to use your SSH key:

If you have GitHub for Windows installed, you can use it to clone repositories and not deal with SSH keys. It also comes with the Git Bash tool, which is the preferred way of running git commands on Windows.

  1. Ensure ssh-agent is enabled:

    • If you are using Git Bash, turn on ssh-agent:

      # start the ssh-agent in the background
      ssh-agent -s
      # Agent pid 59566
      
    • If you are using another terminal prompt, such as msysgit, turn on ssh-agent:

      # start the ssh-agent in the background
      eval $(ssh-agent -s)
      # Agent pid 59566
      
  2. Add your SSH key to the ssh-agent:

    ssh-add ~/.ssh/id_rsa
    
Baptistery answered 8/9, 2013 at 12:12 Comment(13)
How to run the first line cd ~/.ssh? We're in windows, shouldn't it be something like 'C:/'?Cingulum
Open up Git Bash and run it. In GitBash ~ is also your user home dir, seems like linux.Baptistery
I found Git Bash!!!!!I thought it was just the git command, but it turns out there is a git bash.exe....Cingulum
How about in 2018?Triturable
Seems to work for me: I call D:\>"c:\Program Files\Git\cmd\start-ssh-agent.cmd".Promotion
eval : The term 'eval' is not recognized as the name of a cmdlet, function, script file, or operable program.Terrazzo
Aside from Git Bash, this answer is flat out wrong, as is the article it was copied/pasted from.Conwell
@BernhardDöbler start-ssh-agent.cmd should be the correct answer! Thank you so much!Gatlin
@BernhardDöbler answer should be the accepted one. This is brutally buried.Girish
On Windows, replace ~/.ssh/id_rsa with c:\users\<userName>\.ssh\id_rsaInvoluted
@Chris Karpyszyn It's not buried. It was the highest voted answer even 2 years before Bernhard mentioned it in their comment.Hoarse
I use Git Bash but I had to run the second command eval $(ssh-agent -s) for it to startFay
Thanks for that hint, @BernhardDöbler. I had been getting unable to start ssh-agent service, error :1058, and https://mcmap.net/q/13483/-starting-ssh-agent-on-windows-10-fails-quot-unable-to-start-ssh-agent-service-error-1058-quot/470749 hadn't helped yet.Katy
C
97

2021 Answer

Microsoft has improved ssh-key support in recent years. There is now a full featured "service" included with Windows. Windows Server Documentation (applies to other versions too).

Enable the ssh-agent service

Via Admin Powershell:

Set-Service   ssh-agent -StartupType Automatic
Start-Service ssh-agent

Or via Services App:

Windows Service Configuration dialog showing ssh-agent set to start Automatically and currently Running

Now ssh-add works

ssh-add path/to/.ssh/id_rsa

Socket path

Programs that need the path to the agent socket should use: \\.\pipe\openssh-ssh-agent.

Chamblee answered 15/7, 2021 at 1:4 Comment(6)
For git to work passwordless I also had to run (as administrator): git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe (source: interworks.com/blog/2021/09/15/…)Nacre
Where does it actually store the private key passphrase? It's not in the Windows Credential Manager.Seigler
@BłażejCzapp I have not observed this behavior. Works for me without that command.Chamblee
@OliverWeichhold I don’t believe it does, like ssh-agent on Linux. It keeps it in memory only. Every boot needs the password.Chamblee
many many thenks!!!Scutellation
Specifically the last note was the important info I could not find anywhere else. The Socket Path, SSH_AUTH_SOCK="\\.\Pipe\openssh-ssh-agent" so that it works with KeePassXCCarcinogen
G
37

If you are not using GitBash - you need to start your ssh-agent using this command

start-ssh-agent.cmd

If your ssh agent is not set up, you can open PowerShell as admin and set it to manual mode

Get-Service -Name ssh-agent | Set-Service -StartupType Manual
Girish answered 3/8, 2020 at 14:35 Comment(3)
It's been the top voted answer for about 5 years lol... https://mcmap.net/q/126502/-how-to-run-ssh-add-on-windows Sometimes you've gotta sort by votes!Hoarse
Fair enough. I suppose I was referring to having to start the service as well as being in the comments. I'll leave this here, hopefully, it's a more concise answer to help people get going quickly.Girish
I'm wondering what the point of setting it to Manual here is though; wouldn't the smarter option be Automatic? I suppose the default may be Disabled, and that it might incur a startup penalty for non-advanced users, but I feel like jumping over that detail isn't helpful. Additionally, you don't even go over starting the service - is it implied that changing the StartupType automatically starts it? Or that you should go back and run start-ssh-agent.cmd? This answer is so short and undescriptive.Carlina
S
21

If you are trying to setup a key for using git with ssh, there's always an option to add a configuration for the identity file.

vi ~/.ssh/config

Host example.com
IdentityFile ~/.ssh/example_key
Sylvanite answered 20/11, 2018 at 16:8 Comment(2)
I did everything in every other comment, but this was what finally made it work for me. I replaced example.com with github.com because I couldn't clone or push or anything (I was getting the Git: Permission denied (publickey) error)Stalwart
Man. I'm on windows 10 using cmd, and this was the only thing that allowed me to use non-default rsa key names. I wasn't able to get ssh-add from OpenSSH to work, neither the ssh-add from the git ssh tool. Making this config file finally sorted it though.Nurse
B
14

I have been in similar situation before. In Command prompt, you type:

start-ssh-agent

and voila! The ssh-agent will be started. Input the passphrase if it asked you.

EDIT: Git Bash is required to run this command. thanks to @jkmartindale

Behling answered 1/1, 2020 at 14:42 Comment(4)
This should be higher. Works in Powershell or CMD.Belita
'start-ssh-agent' is not recognized as an internal or external commandMusket
This requires Git BashNorthern
HOw do you ensure it runs everytime restrat??Lachrymal
D
10

In order to run ssh-add on Windows one could install git using choco install git. The ssh-add command is recognized once C:\Program Files\Git\usr\bin has been added as a PATH variable and the command prompt has been restarted:

C:\Users\user\Desktop\repository>ssh-add .ssh/id_rsa
Enter passphrase for .ssh/id_rsa:
Identity added: .ssh/id_rsa (.ssh/id_rsa)

C:\Users\user\Desktop\repository> 
Deviltry answered 26/8, 2015 at 18:55 Comment(1)
Yes! Doing C:\Program Files\Git\usr\bin to path finally fixed it! Everone was doing all kinds of other folders in \Git but not usr\binWarmth
T
4

To generate, check and add ssh keys in Windows with PowerShell:

Open PowerShell with Win + X then click on "Terminal (administrator)".

To generate a key use:

ssh-keygen -t rsa -b 4096 -C « [email protected] »

-t specifies the type of encryption, -b the strength of encryption, and -C is the email attached to the account you want to be able to access with your key.

It's supposed to be stored by default in C:\Users\yourusername/.ssh/id_rsa , and you should find a yourkey file and a yourkey.pub file there.

To display your public key from the terminal do this:

cat yourkey.pub

To check if the SSH agent is running do:

Get-Service ssh-agent

If it's not running, do this to set it up manually:

Get-Service ssh-agent | Set-Service -StartupType Manual

Then:

Start-Service ssh-agent

To check the keys already added to your ssh agent do this:

ssh-add -L

And to add your key, do this:

ssh-add yourkey
Telescope answered 16/6, 2023 at 18:10 Comment(1)
Add ssh key in Windows 11. It works!Prokofiev
U
3
eval "$(ssh-agent -s)" 

ssh-add C:/Users/Dell/.ssh/gitlab (your path)

git clone repo_link
Utilitarian answered 10/3, 2021 at 11:49 Comment(0)
T
2

The Git GUI for Windows has a window-based application that allows you to paste in locations for ssh keys and repo url etc:

https://gitforwindows.org/

Twelve answered 11/3, 2019 at 23:32 Comment(0)
K
2

The below solution solved my problem. Be sure to run your powershell in admin mode and perform the below operation:

  1. Check the current status of ssh-agent: "Get-Service | select -property name,starttype" --> should be Disabled

  2. Set the new type : "Set-Service -Name ssh-agent -StartupType Manual"

  3. Start it: "Start-Service ssh-agent"

  4. Add simply your key as before: "ssh-add" (Eg. ssh-add keyfile)

I found the solution here:

Kenrick answered 18/5, 2021 at 18:28 Comment(0)
E
1

This works with plain cmd on win7 and win10 and cygwin ssh/git/github:

c:\> type ssh-agent-start-cmd.cmd

@echo off
@ by github/moshahmed
if "%1" == "" (
  echo "Usage: ssh-agent-cmd keyfile .. starts ssh-agent and load ~/.ssh/*keyfile*"
  goto :eof
)

taskkill /f /im ssh-agent.exe
:: pskill ssh-agent 2> nul
for /f "tokens=1 delims=;" %%a in ('ssh-agent') do (
  echo %%a | findstr /C:"SSH" 1>nul
  if errorlevel 1 (
    echo Ignore %%a
  ) else (
    echo set %%a
         set %%a
  ) 
)
ssh-add ~/.ssh/*%1*
ssh-add -l 
Ellis answered 21/5, 2021 at 18:26 Comment(0)
M
0

Supplementing the answer about start-ssh-agent.cmd would be to run it at 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:

--- "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.

Mordred answered 8/8, 2023 at 21:21 Comment(0)
O
-1

I just set up the SSH authentication with Github. Just can just use "Pageant" which is installed with Putty.

You'll need to add pageant to your Windows startup folder so that it starts when windows does (or start it each time before you need to authenticate)

This blog post does a nice job of explaining everything you need to do to configure Github on Windows with Putty and Pageant.

Overeat answered 21/7, 2021 at 20:18 Comment(0)
P
-2

You should start ssh agent and generate ssh key with recommand command

ssh-keygen -t rsa -b 4096 -C "your email"
Piegari answered 14/7, 2021 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.