Setting up SSH-Agent in Windows for Passwordless Git Authentication
SSH-Agent and OpenSSH are tools in Windows that can be used to authenticate to remote Git repositories, such as GitLab and GitHub. Once set up as a service that stores your various SSH keys, this can facilitate authentication without entering a password each time, removing the irritation of entering a password every time you wish to push/pull/etc. from a Git repository.
How to Install the SSH-Agent Service in Windows¶
Using an elevated PowerShell window (run as admin), execute the following command to install the SSH-Agent service and configure it to start automatically when you log into your machine:
Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Service
To avoid needing to restart your system to get the service running for the first time, execute this command:
start-ssh-agent.cmd
Setting up an SSH Key Pair to Access a Git Remote Provider¶
Using a command line tool such as Windows PowerShell you should able to generate the SSH key pair.
ssh-keygen -t ed25519 -C "denizraifdurmaz@gmail.com"
By default, the file will be stored in your local user’s SSH repository in Windows. You can choose another storage location if you wish or rename the file by entering a new file path to save the key.
You can set a passphrase for added security. If you don't want a passphrase, just press Enter
.
- The key pair will be saved in the
C:\Users\<YourUsername>\.ssh\
directory:id_ed25519
orid_rsa
: Private key.id_ed25519.pub
orid_rsa.pub
: Public key.
In GitLab (or the appropriate location of your Git remote repository), you can now add this public key to your user profile. In GitLab, you can do this by adding it under the SSH Keys section of your user settings
Danger
Ensure that your are using public key, not private key.
Adding the SSH Key to the SSH-Agent Service¶
Our goal is to be able to connect to a Git repository without entering a password. At this stage, we have a working SSH key pair and the SSH-Agent service installed and running.
Add your private key to the SSH agent:
ssh-add C:\Users\<YourUsername>\.ssh\id_ed25519
Configuring Git to Leverage the Windows SSH-Agent¶
In an elevated console (run as admin), execute the following command to modify your existing Git configuration to leverage the windows OpenSSH service as the core SSH command:
git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe
Congratulations! You have now set up your environment to automatically authenticate to your Git remote provider through an SSH key pair, without using passwords.