Connecting to GitHub Using SSH
Github now offers different methods to connect to our account. At the moment, there are two(2) different ways to connect to the remote from our local. They are;
- HTTPS (conventional way)
The HTTPS method has been the conventional way of connection and is still the most adapted method for connection, but if you use this method then you can agree that it puts you through the task of almost always having to reconnect to the remote by prompting for username and personal access token.
SSH as a protocol makes it easy to connect and authenticate remote servers and their services without going through the rigorous process of having to supply credentials at every visit.
In this article, we are going to focus on learning how to connect using SSH protocol. Let’s get right into it
Generating an SSH Key from our Command Line
It is important to note that in generating the SSH key, we can use different public key algorithms but we are going to look at RSA using the 4096-bit length key size and Ed25519 which is the most recommended public key available.
While RSA is the most widely used but considered unsafe when a key size that is smaller than 2048-bit length is used in the generation process, the Ed25519 creates a compact public key that has a very fast generation and sign-in processes.
We are going to look at how to create the key using both public-key algorithms but it is advisable to use the more secure and faster Ed25519 algorithm.
Before we start creating a new key, we can check to see if there are any existing keys by inputting the below command in the terminal:
$ ls -al ~/.ssh
this checks the .ssh directory for any keys. If they already exist then we will find files in id_rsa.pub or id_ed25519.pub format. With the .pub extension signifying that they are public keys.
If there are no existing keys or we wish not to use any of the existing ones, then a new key is generated.
To generate a new key, the below command is entered into the terminal(for Linux) or git bash(for windows users):
$ ssh-keygen -t ed25519 -C “email@example.com”
$ ssh-keygen -t rsa -b 4096 -C “firstname.lastname@example.org”
the -t flag specifies the kind of key while the -C lets us include an optional comment but it is common to see people use their email address.
The above command generates a new key using the provided email as the label. After which we will be prompted to type a secure passphrase, and if not provided will be empty.
Adding the SSH key to the SSH agent
In the section, we will add the SSH key to the ssh-agent.
Firstly, we need to start the ssh-agent manually(this can also be auto-launched) by typing the following command in the terminal
$ eval “$(ssh-agent -s)”
An output like the below will signify that the ssh-agent has started in the background.
> Agent pid 59566
The next step will be to add the SSH private key to the ssh-agent using:
$ ssh-add ~/.ssh/id_ed25519
$ ssh-add ~/.ssh/id_rsa
With this, the private key has been added to the ssh-agent.
Copying the Public SSH Key
The first step would be to get the public key which we are going to save in our GitHub account to connect to it.
To copy the public key, we first need to enter the ssh directory. We can achieve that by using the below command:
$ cd ~/.ssh
If we list out the files in the directory using
we should be able to find a public key with an extension of .pub. This can be id_ed25519.pub or id_rsa.pub.
To view the content(key) of the file so we can copy it:
$ cat id_ed25519.pubor$ cat id_rsa.pub
Adding SSH key to Github Account
In our GitHub account, we will navigate to the SSH and GPG keys section on the settings page.
When we click on the “New SSH Key” button, a form with title and key as inputs will come up.
The title should be any word or phrase we want to give the key. Paste the public key copied earlier into the key input and then click on the “Add SSH Key” button.
Now we can go back to the terminal and run the below command which connects to the GitHub account
$ ssh -T email@example.com
We should see an output message informing us that we have successfully connected to that GitHub account.
To avoid going through the pains of having to input our username and personal access tokens at each visit when connecting to Github, you should use an SSH key.
When SSH protocol is used then our choice of a public-key algorithm should be Ed25519, as it provides a faster and safer way to generate keys.