187

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.

030
  • 10,842
  • 12
  • 78
  • 123
ZK Zhao
  • 19,885
  • 47
  • 132
  • 206
  • 1
    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. – svin83 Sep 27 '18 at 16:16
  • [GitHub provides an excellent script](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases?platform=windows#auto-launching-ssh-agent-on-git-for-windows) to start the `ssh-agent` and add private keys to it, which script I found [through VonC's answer](https://stackoverflow.com/a/18404557/4561887). 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 Windows](https://stackoverflow.com/a/76568760/4561887) – Gabriel Staples Jun 27 '23 at 21:52

15 Answers15

377

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
starball
  • 20,030
  • 7
  • 43
  • 238
tamj0rd2
  • 4,910
  • 2
  • 22
  • 22
  • 92
    `start-ssh-agent`. That's all I needed after an hour of looking around. Thank you! – tkore Aug 01 '17 at 12:47
  • 3
    I think `start-ssh-agent` does work with PowerShell. However, I was running PowerShell as an administrator, so that might be the key. – Trevor Oct 05 '18 at 00:06
  • 4
    For me the windows service "OpenSSH Authentication Agent" was disabled, this helped, thx – petrumo Apr 11 '19 at 10:08
  • 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. – Kirill Osenkov Jul 07 '20 at 19:08
  • 1
    any idea how to change the name of the key file to something other than id_rsa? – Krzysztof Krzeszewski Oct 02 '20 at 13:52
  • 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? – FlexMcMurphy Mar 21 '21 at 12:01
  • 1
    @FlexMcMurphy Have you installed the OpenSSH Authentication Agent? If you have, maybe you need to close and reopen powershell. – tamj0rd2 Mar 23 '21 at 18:58
  • 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 repositories](https://stackoverflow.com/questions/11200237/how-do-i-get-git-to-default-to-ssh-and-not-https-for-new-repositories) – AverageAdam May 16 '21 at 14:36
  • 1
    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. – Foxy Fox Jan 10 '22 at 06:19
  • 1
    You should also tell Git to use the external instead of built-in ssh in Git's install dialog – SWdV May 01 '22 at 13:48
  • 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" – Pedro77 Oct 11 '22 at 15:02
  • 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: https://github.com/ritazh/devopsfun/issues/17 – Jim Fell Nov 18 '22 at 14:56
133

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
    
izstas
  • 5,004
  • 3
  • 42
  • 56
RaymondChou
  • 1,404
  • 1
  • 10
  • 3
84

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.

Cameron Tacklind
  • 5,764
  • 1
  • 36
  • 45
  • 3
    For git to work passwordless I also had to run (as administrator): git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe (source: https://interworks.com/blog/2021/09/15/setting-up-ssh-agent-in-windows-for-passwordless-git-authentication/) – Błażej Czapp Feb 14 '23 at 15:02
36

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
Chris Karpyszyn
  • 867
  • 10
  • 16
  • 2
    It's been the top voted answer for about 5 years lol... https://stackoverflow.com/a/40720527/5362826 Sometimes you've gotta sort by votes! – tamj0rd2 Mar 16 '21 at 17:29
  • 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. – Chris Karpyszyn Mar 17 '21 at 15:52
  • 1
    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. – Xevion Feb 09 '23 at 02:17
19

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
Brian Ng
  • 1,005
  • 12
  • 13
  • 4
    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) – neveraskedforthis Jun 06 '19 at 00:12
  • 1
    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. – Geoff Davids Dec 29 '19 at 00:24
13

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.

farizmamad
  • 171
  • 2
  • 6
9

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> 
030
  • 10,842
  • 12
  • 78
  • 123
  • 2
    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\bin – Noitidart Dec 20 '17 at 18:58
2
eval "$(ssh-agent -s)" 

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

git clone repo_link
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Hasan Tezcan
  • 1,116
  • 1
  • 11
  • 23
2

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@email.com »

-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
Lina L.
  • 21
  • 2
1

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/

1

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:

user2430771
  • 1,326
  • 4
  • 17
  • 33
0

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 
mosh
  • 323
  • 2
  • 8
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.

midenok
  • 930
  • 10
  • 14
-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.

ehambright
  • 1,416
  • 19
  • 27
-2

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

ssh-keygen -t rsa -b 4096 -C "your email"
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567