Reputation: 21533
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
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
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
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
Reputation: 7224
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).
ssh-agent
serviceVia Admin Powershell:
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Or via Services App:
ssh-add
worksssh-add path/to/.ssh/id_rsa
Programs that need the path to the agent socket should use: \\.\pipe\openssh-ssh-agent
.
Upvotes: 110
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.
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.
Open Services
from the start Menu
Scroll down to OpenSSH Authentication Agent
> right click > properties
Change the Startup type from Disabled to any of the other 3 options. I have mine set to Automatic (Delayed Start)
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
.
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 thisSome nice things about this solution:
id_rsa
Upvotes: 433
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
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
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
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
Reputation: 1422
The below solution solved my problem. Be sure to run your powershell in admin mode and perform the below operation:
Check the current status of ssh-agent: "Get-Service | select -property name,starttype" --> should be Disabled
Set the new type : "Set-Service -Name ssh-agent -StartupType Manual"
Start it: "Start-Service ssh-agent"
Add simply your key as before: "ssh-add" (Eg. ssh-add keyfile)
I found the solution here:
Upvotes: 2
Reputation: 1232
eval "$(ssh-agent -s)"
ssh-add C:/Users/Dell/.ssh/gitlab (your path)
git clone repo_link
Upvotes: 3
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:
Upvotes: 2
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
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.
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
Add your SSH key to the ssh-agent:
ssh-add ~/.ssh/id_rsa
Upvotes: 141
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