• Post category:Git
  • Post comments:0 Comments
  • Reading time:5 mins read

Jeff Brown

Cloud and DevOps Engineer specializing in Microsoft 365, Azure, and PowerShell. Twitter | LinkedIn

Are you working with GitHub repositories but tired of typing in your username and personal access token each time you clone or push changes? You need to use SSH authentication! Using the SSH protocol, you can connect and authenticate to GitHub without typing in a password.

In this tutorial, you will learn:

  • How to check for existing SSH keys
  • Generate an SSH key
  • Add the SSH key to your GitHub account
  • Test your SSH connection

Prerequisites

If you want to follow along with this tutorial, you will need:

Why use SSH Authentication?

Configuring SSH authentication allows you to download your repositories and upload changes without supplying a username and password. Without configuring SSH authentication, you cannot download your repositories if you are using the SSH protocol. Let’s demonstrate what this looks like.

  1. Navigate to your GitHub account and sign in.
  2. Access one of your repos. This tutorial is using the author’s Python repo.
  3. On the repo home page, select the Code button.
  4. In the code button, ensure the SSH tab is select.
  5. Copy the SSH URI using the copy button.
copy repo ssh uri
  1. With this SSH URI copied, open the Windows 10 Start Menu and select the Git Bash program.
  2. In the Git Bash console, run the git clone command and paste the SSH URI copied earlier. This action will download the repo to your local system. If you receive a message about continue connecting, enter yes.
git clone <SSH URI>
git clone
  1. Since you don’t have an SSH public key configured, you are unable to download your own public repository! Git responds with the message:
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights and the repository exists.
git authentication error

Now that you’ve seen what it looks like without SSH authentication configured, continue with the tutorial to learn how to configure SSH authentication.

Check for Existing SSH Keys

Now, this might seem like a silly step. Why check for existing keys? Just in case you have an existing public/private key pair that you can reuse! To verify if you have an existing SSH key:

  1. Open Git bash console if you closed it from the previous step.
  2. Ensure you are in your home directory by running the command cd $home.
  3. Enter ls -al ~/.ssh to see if any existing SSH keys are available.
check for existing ssh keys
  1. Check to see if any of the following files already exist. If they do, you already have an SSH key to use. We are going to assume you don’t before moving onto the next section.
  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

Generate New SSH Key

Now that you have established you don’t have an existing SSH key, it’s time to generate one!

  1. Return to the Git bash console.
  2. Enter the following command, substituting your email address with the one associated with your GitHub account.
ssh-keygen -t ed25519 -C 'your_email@domain.com'
  1. When prompted where to save the key pair file, press Enter to accept the default.
  2. When prompted to enter a passphrase, enter a passphrase, or press Enter to leave it blank. Adding a passphrase adds security to the SSH key, but you have to enter it each you authenticate. This tutorial is leaving the passphrase blank.
  3. The public key, SHA256 hash, and randomart image is display in the bash console.
view generated ssh key

Next, add the SSH key to the SSH agent. The SSH agent manages your SSH key and remembers your passphrase so you don’t have to reenter it each you use your SSH key.

  1. Ensure the ssh-agent is running by using the following command:
eval $(ssh-agent)
  1. Add the SSH private key to the ssh-agent using the following command:
ssh-add ~/.ssh/id_ed25519
add ssh key to ssh agent

Add SSH Key to GitHub Account

With the SSH key generated on your local system, it is time to add the public key to GitHub. Adding the public key to GitHub allows your local system to use the private key pair to authenticate.

  1. In the Git bash console, copy the SSH public key to your clipboard using the clip command.
clip < ~/.ssh/id_ed25519.pub
  1. Navigate back to GitHub and log in with your account.
  2. In the upper-right corner, select your profile photo, then select Settings.
  3. In the Settings sidebar, select SSH and GPG keys.
  4. Click on the New SSH key button.
  5. Enter a description of the key in the Title field. This should describe which system uses the key so you can identify it later.
  6. Paste the public key into the Key field.
  7. Finish by selecting Add SSH key.
add ssh key to github account

Test your SSH Connection to GitHub

With the key added, you can test your connection to verify the SSH key is working.

  1. In the Git bash window, enter the following command to test the SSH connection out to github.com. If you receive a warning about host authenticity, enter yes, then press enter.
ssh -T git@github.com
  1. Verify the returning message displays your username. If so, you’ve successfully authenticated out to GitHub using your new SSH key!
test ssh connection to github

If I attempt to clone one of my repos using the SSH URI, the command should now be successful.

successfully clone repo using ssh

Summary

With the SSH key now generated and added to your GitHub account, you no longer need to enter your password and access token to authenticate. Using SSH keys provides a seamless experience when working with remote Git repositories.

Check out more of my articles around working with Git!

Start a discussion!