ZK Zhao
ZK Zhao

Reputation: 21533

How to run ssh-add on windows?

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.

Upvotes: 221

Views: 571072

Answers (15)

farizmamad
farizmamad

Reputation: 359

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

Upvotes: 19

midenok
midenok

Reputation: 1215

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.

Upvotes: 1

Lina L.
Lina L.

Reputation: 41

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

Upvotes: 4

Cameron Tacklind
Cameron Tacklind

Reputation: 7224

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.

Upvotes: 110

tamj0rd2
tamj0rd2

Reputation: 5690

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

Upvotes: 433

Chris Karpyszyn
Chris Karpyszyn

Reputation: 897

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

Upvotes: 37

thegiau.pham
thegiau.pham

Reputation: 65

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

ssh-keygen -t rsa -b 4096 -C "your email"

Upvotes: -2

ehambright
ehambright

Reputation: 1593

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.

Upvotes: -1

mosh
mosh

Reputation: 333

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 

Upvotes: 1

user2430771
user2430771

Reputation: 1422

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:

Upvotes: 2

Hasan Tezcan
Hasan Tezcan

Reputation: 1232

eval "$(ssh-agent -s)" 

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

git clone repo_link

Upvotes: 3

Arne Olafson
Arne Olafson

Reputation: 21

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/

Upvotes: 2

Brian Ng
Brian Ng

Reputation: 1085

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

Upvotes: 21

RaymondChou
RaymondChou

Reputation: 1484

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
    

Upvotes: 141

030
030

Reputation: 11689

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> 

Upvotes: 10

Related Questions