How to Setup SSH with GitHub Repo?
The most common problem with GitHub using a username and password and then using an SSH key to authenticate to a GitHub repo is that the SSH key needs to be configured appropriately. It must be added to the user’s account on GitHub and then added to the local machine. Additionally, the public key must be uploaded to the server. If any of these steps are not completed correctly, authentication will fail.
What is SSH?
SSH (Secure Shell) is a network protocol that allows secure communication between two computers over an unsecured network. It provides strong authentication, encryption, and data integrity for secure communication between two systems. SSH keys authenticate users on remote systems and allow them to securely access resources on the remote system without having to enter a password every time they connect. In this blog post, we’ll discuss how to generate SSH keys and add them to the ssh-agent step-by-step.
About authentication to GitHub
There are several ways to authenticate to GitHub:
- Using a username and password: You can log into your GitHub account using your username and password. This is the most common way of authenticating with GitHub.
- Using SSH keys: You can also authenticate to GitHub using SSH keys, which are used to securely connect two computers over a network. With an SSH key, you can securely access repositories on GitHub without having to type in your username and password each time.
- Using two-factor authentication: You can also use two-factor authentication when logging into your GitHub account. This adds an extra layer of security by requiring you to enter a code from your mobile device in addition to your username and password.
- Using OAuth tokens: OAuth tokens allow you to access certain parts of the GitHub API without having to enter your username and password each time.
Recently, GitHub removed the option to authenticate using a username and password in the command line interface (CLI). The only way to authenticate is now using an access token. This change was made to increase security and prevent malicious actors from accessing user accounts.
Let's say you want to clone the code from this repo URL git@github.com:saedHasan/setup-jenkins-awsEC2.git , the first step you go to your Terminal if you are on macOS or if you using vsCode IDE terminal, and then clone the repo using below git command :
git clone git@github.com:saedHasan/setup-jenkins-awsEC2.git
If you didn’t setup the SSH Key already, you will receive an error as below :
How to fix this?
Step 1: Generate an SSH key
First, you will need to generate an SSH key. You can do this by running the following command in your terminal:
ssh-keygen -t ed25519 -C "your_github_email@example.com"
This will generate a private and public key pair. The private key should be kept secure and should never be shared with anyone. The public key can be shared freely.
Note: hit enters when you ask to enter the file name to save the key, and for the passphrase as well.
Step 2: Add the SSH key to the SSH-agent
Once you have generated the SSH key, you need to add it to the SSH-agent so that it can be used for authentication. To do this, run the following command in your terminal:
eval "$(ssh-agent -s)"
The output should be like this :
Next step you will need to modify your ~/.ssh/config
file to automatically load keys into the ssh-agent and store passphrases in your keychain — for macOS users.
First, check to see if your ~/.ssh/config
file exists in the default location.
$ open ~/.ssh/config
> The file /Users/YOU/.ssh/config does not exist.
If the file doesn’t exist, create the file.
$ touch ~/.ssh/config
Open your ~/.ssh/config
file, then modify the file to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup.
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Next is to Add your SSH private key to the ssh-agent and store your passphrase in the keychain.
$ ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Step 3: Add the SSH key to your account on the GitHub account
To copy the SSH key, open the folder ~/.ssh/ by running the below command :
open ~/.ssh/
This will open the folder that contains the public key in .pub file , open the file and copy the content.
In your GitHub web browser in the upper-right corner of any page, click your profile photo, then click Settings.
Then, In the “Access” section of the sidebar, click SSH and GPG keys and click on the New SSH key button.
In the “Title” field, add a descriptive label for the new key, and Paste your public key into the “Key” field.
Click Add SSH key to save the change. If prompted, confirm access to your account on GitHub.
Step 4: Test your setup:
Finally, you can test your setup by trying to connect to a remote server using your newly generated SSH key. To do this, run the following command in your terminal:
$ ssh -T git@github.com
If everything is configured correctly, a successful welcome message will prompt and you should be able to connect without having to enter a password.
Conclusion
In conclusion, generating SSH keys and adding them to the ssh-agent is an important part of setting up secure communication between two systems over an unsecured network. By following the steps outlined in this blog post, you should now have a better understanding of how generating SSH keys works and how they can be added to the ssh-agent for authentication purposes when connecting with another computer or server remotely.
I hope this article has been helpful in understanding authenticating with GitHub. If you found this article interesting and helpful, please show your appreciation by giving it a clap!