Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (2024)

SSH-Agent and OpenSSH are tools in Windows that can be used to authenticate to remote Git repositories, such as GitLab, GitHub, Azure DevOps, etc. 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.

Prerequisites

  • The OpenSSH Client optional service must be enabled on your machine, and OpenSSH must be added to your PATH environment variable. You can read how to do that here.
  • A remote Git repository that you wish to access. We will use a GitLab repository for this article; however, the process will be similar for other Git management providers.
  • Git must be installed on your machine.

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

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (1)

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 SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (2)

Setting up an SSH Key Pair to Access a Git Remote Provider

Using a command line tool such as Bash or PowerShell, you should be able to follow these steps to create a local SSH key pair. For our example, we will create an ED25519 key, but you can create other keys such as an RSA.

Create a new SSH ED25519 key, giving it a useful comment:

ssh-keygen -t ed25519 -C "Git-Demo"

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. Leave it blank to stick with the default.

In our example, we rename the file from the default id_ed25519 to id_ed25519_git_demo:

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (3)

You can also add a password if you like or leave this blank:

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (4)

You will then be shown the key’s randomart image to confirm creation:

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (5)

Copy the contents of the public key to your clipboard. You can read this key with the following command:

cat path\to\ssh\key.pub

For example, our code is likely:

cat C:\Users\chastie/.ssh\id_ed25519_git_demo.pub

Note: We access the public key with the .pub suffix.

A sample is shown here. You can then select this and copy it with a right-click of your mouse:

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (6)

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 Keyssection of your user settings:

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (7)

Test that you can connect to the repository when using the SSH private key directly with this command:

ssh -i path/to/ssh/private/key -T git@host

For example, our command could be:

ssh -i C:\Users\chastie/.ssh\id_ed25519_git_demo -T git@gitlab.mycompany.comorssh -i C:\Users\chastie/.ssh\id_ed25519_git_demo -T git@github.com

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (8)

We have now established an SSH key pair that can authenticate to our Git remote provider. It remains to set this up in the SSH-Agent service to automatically provide access. We can demonstrate the issue by attempting the same connection, but without specifically naming the SSH key, with the command below:

ssh -T git@host

As we can see, if we execute this command without specifying an SSH key, we are prompted for a password:

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (9)

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.

Execute the following command to add your SSH key to your SSH-Agent service:

ssh-add path/to/ssh/private/key

For our example, our command could be:

ssh-add C:\Users\chastie/.ssh\id_ed25519_git_demo

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (10)

We can now test our connection to our Git remote provider without specifying a key and connect successfully:

ssh -T git@host

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (11)

Configuring Git to Leverage the Windows SSH-Agent

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (12)

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

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (13)

Congratulations! You have now set up your environment to automatically authenticate to your Git remote provider through an SSH key pair, without using passwords. If you wish to facilitate access to any other Git remote providers, simply follow the same steps to generate a key pair ( as outlined above) and add it to your existing SSH-Agent service.

About SSH-Agent and OpenSSH

SSH-Agent and OpenSSH are tools in Windows that can be used to authenticate to remote Git repositories, such as GitLab, GitHub, Azure DevOps, etc. These tools 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. The process involves setting up the SSH-Agent service, creating an SSH key pair, adding the SSH key to the SSH-Agent service, and configuring Git to leverage the Windows SSH-Agent.

SSH-Agent and OpenSSH Concepts

  1. OpenSSH Client: The OpenSSH Client optional service must be enabled on your machine, and OpenSSH must be added to your PATH environment variable.

    • The OpenSSH Client optional service must be enabled on your machine, and OpenSSH must be added to your PATH environment variable.
  2. Remote Git Repository: A remote Git repository that you wish to access, such as GitLab, GitHub, or Azure DevOps.

    • You need a remote Git repository that you wish to access. The process will be similar for other Git management providers.
  3. Git Installation: Git must be installed on your machine.

    • Git must be installed on your machine.
  4. Installing the SSH-Agent Service: Using an elevated PowerShell window (run as admin), execute a command to install the SSH-Agent service and configure it to start automatically when you log into your machine.

    • Using an elevated PowerShell window (run as admin), execute a command to install the SSH-Agent service and configure it to start automatically when you log into your machine.
  5. Setting Up an SSH Key Pair: Using a command line tool such as Bash or PowerShell, you can create a local SSH key pair, such as an ED25519 key.

    • Using a command line tool such as Bash or PowerShell, you can create a local SSH key pair, such as an ED25519 key.
  6. Adding SSH Key to SSH-Agent Service: After creating the SSH key pair, you can add the SSH key to your SSH-Agent service to automatically provide access.

    • Execute a command to add your SSH key to your SSH-Agent service.
  7. Configuring Git: Modify your existing Git configuration to leverage the Windows OpenSSH service as the core SSH command.

    • Execute a command to modify your existing Git configuration to leverage the Windows OpenSSH service as the core SSH command.

Conclusion

By following these steps, you can set up your environment to automatically authenticate to your Git remote provider through an SSH key pair, without using passwords. If you wish to facilitate access to any other Git remote providers, simply follow the same steps to generate a key pair and add it to your existing SSH-Agent service.

Setting up SSH-Agent in Windows for Passwordless Git Authentication - InterWorks (2024)
Top Articles
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 5964

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.