How to Connect to GitHub with SSH Authentication

For GitHub users, there are several ways to connect and manage the GitHub repository from a local computer. I am currently learning how to use GitHub and connect my repository from a laptop. This way, I can use my laptop and then I can push any updates I made to the repository.

There are several steps to connect to GitHub with SSH. The very first thing is to make sure that we have a SSH Key. Let’s check the existing SSH Keys before we create a new one.

The command will list all the files inside the .ssh directory. In my case, I don’t have any ssh keys yet. The command returns the following:

This means, I don’t have any ssh keys yet. So, let’s create a new one:

Generate a new SSH Key and Adding to SSH Agent

So now we are going to create a new SSH key. Open Terminal and then paste the following command. Change the email address with your GitHub email address.

ssh-keygen -t rsa -b 4096 -C “dhani@gmail.com

You will need to supply a new passphrase but you can also skip it by pressing Enter twice if you don’t want to put any passphrase.

At this point, we have successfully created a new SSH Key. Now we are going to add this new key to ssh-agent

Start the ssh-agent in the background with this command

eval “$(ssh-agent -s)”

Output:

Now let’s add the SSH private key to the ssh-agent with this command

Add the SSH Key to GitHub Account

On the previous step we’ve created a new Private SSH Key and add it to our ssh agent. Now, we are going to copy the key to our GitHub Account. To do this, we need to install xclip. This program will help us to easily copy the key to clipborad.

Now, open GitHub Account and go to Settings. Click SSH and GPG Keys and then click New SSH Key. Give a name for this new key for example: Ubuntu-laptop or anything you like. Then, paste your key.

Finally, click Add SSH Key to complete the step. At this point, we have successfully add a new SSH Key to our GitHub account.

Switching Remote URL from HTTPS to SSH

The following step is used to change the remote URL from HTTPS to SSH. If you want to connect to your GitHub repository via Terminal using SSH, do the following:

Make sure to change the ~/Documents/zenity-script with your local GitHub repository.

As you can see, currently I am connecting to my GitHub using HTTPS mode. Now let’s switch to SSH mode. The syntax is:

In my case, it would be

Now let’s check again

As you can see the connection is now start with git@github, not HTTPS. So now, I am connecting to my GitHub using SSH.

Admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.