Generating A New SSH Key and Adding It to Github
Tags: codenewbie, github, girldevminute
Published: 2019-04-02 13:18:33

---
title: Generating A New SSH Key and Adding It to Github
published: true
description: Generating an SSH key may sound like a cumbersome task but in fact, it is a fun task with a great learning experience. Today, I am going to share this quick dev-exercise with you. By the end of this piece, you'll learn how to create an SSH key and how to add it to your GitHub account.
tags: #CodeNewbie, #GitHub, #GirlDevMinute
cover_image: https://on.ahmda.ws/003b655213b6/c
---

Sometimes I behave like a crazy weird nerd. The developer instinct has always made me learn new things be it skimming through some random code, a tip or some dev-workflow. There are concepts which I just keep dumping in my brain without trying them. But the only motivation is that if needed I will definitely do it someday.

Each time I clone a GitHub repo I come across the option to clone it via SSH. I had an idea about SSH keys but never came across a situation where I had to generate one and add it to my GitHub account.

Today, I confronted the scenario where I had to connect my GitHub account with an SSH key since my GitHub repo couldn't be cloned locally. After figuring out the process, I finally did it and thought about writing my experience with you folks.

![](https://maedahbatool.com/wp-content/uploads/2019/04/SSH-Keys.jpg)

So, if you're finding it difficult to create an SSH key and connect it with your GitHub account, then this article is definitely for you.

Let's start with some basic concepts about SSH keys.
<h2>What are SSH Keys?</h2>
<strong>SSH (Secure Shell)</strong>, is an encrypted protocol to communicate with servers allowing users to avail several network-services. SSH keys, in turn, provide a secured mechanism against brute force attacks which may occur while logging into a server with a password.

When cloning a GitHub repo, you sometimes come across an error saying:

<code>Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.</code>

This calls for the creation of an SSH key. Generating an SSH key results in <em>two long string of characters</em> which includes a <strong>public</strong> and a <strong>private</strong> key. The public key is used to unlock any server and then connect it with the client (typically your computer) where the private part of the SSH key resides.
<h2>Figuring Out Any Existing SSH Keys</h2>
Before you generate an SSH key, you must check if you have any existing SSH keys stored in your computer. To do so, open your terminal, type the following command and hit Enter.

<code>ls -al ~/.ssh</code>

It displays a list of files in your .ssh directory. Go through this list to find the existence of any public SSH keys. By default, public keys are stored with the .pub file extension.

Normally, public keys appear against the following names:
<ul>
 	<li>id_rsa.pub</li>
 	<li>id_dsa.pub</li>
 	<li>id_ecdsa.pub</li>
 	<li>id_ed25519.pub</li>
</ul>
If there aren't any public keys, it's time to create a new SSh key which I will teach shortly. On the contrary, if you find an existing public/private key pair (e.g., <code>id_rsa.pub</code> and <code>id_rsa</code>), you can use it to add to the GitHub account and get started.

I checked for the presence of SSH keys and here's the GIF:

![](https://maedahbatool.com/wp-content/uploads/2019/04/Screen-Recording-2019-04-01-at-12.22-PM.gif)

<h2>Generating a New SSH Key</h2>
In case, no public key exists or you want to create a new key for your GitHub, type the following command in the terminal and hit Enter.

<code>ssh-keygen</code>

The process of key generation will start. It asks for the file in which to save the key with other related info. Fill these details. However, you can leave it empty, and it'll generate your first key with the default name as <code>id_rsa.pub</code>. Remember I didn't change any defualts here and just pressed enter or return key for all options like password and key path.

Here's the GIF:

![](https://maedahbatool.com/wp-content/uploads/2019/04/ezgif.com-gif-maker.gif)

<h3>Copying the SSH Key</h3>
Now it's time to copy the contents of the SSH key and add to the GitHub account. Type the following:

<code>cat ~/.ssh/id_rsa.pub</code>

The SSH key appears as follow. Just copy it!

![](https://maedahbatool.com/wp-content/uploads/2019/04/Screen-Recording-2019-04-02-at-01.35-PM.gif)

<h3>Adding SSH Key to GitHub</h3>
Now heading to the final part where I'll add the SSH key to my GitHub account. In your GitHub <strong>Profile</strong> open <strong>Settings</strong>. Go to the section for <strong>SSH and GPG Keys</strong>. Click the <strong>New SSH key</strong> button, write a suitable <strong>Title</strong>, paste the copied <strong>Key</strong> and hit the <strong>Add SSH key</strong> button.

🤜 BOOM! Here you go. You have successfully added your first SSH to GitHub. Now the GitHub repo can be easily cloned. Moreover, you also get a confirmatory email to inform you about the addition of this new key. This is to make sure no unauthorized keys get added.

I am uploading the GIF to explain the aforementioned process.

![](https://maedahbatool.com/wp-content/uploads/2019/04/ezgif.com-gif-maker-2.gif)

<h2>👩‍🏫 Your Turn!</h2>
See how simple and easy it is to create an SSH key. You can generate multiple SSH keys and add to your GitHub account. But I'll recommend you to keep it as less as possible to support simplicity. Now it's your turn to replicate the process and share your feedback in the comments section below.

Moreover, you can <a href="https://twitter.com/intent/tweet?text=%23TIL%20%E2%80%94%20How%20to%20generate%20an%20SSH%20key%20and%20add%20to%20a%20GitHub%20account.%0A%0AGive%20this%20%23GirlDevMinute%20a%20try!%20%0A%0Avia%20%40MaedahBatool%20%0A%0ARead%20here%20%0Ahttps%3A//maedahbatool.com/generating-a-new-ssh-key/%0A">Share on Twitter</a> with the hashtag <strong>#GirlDevMinute</strong> or reach out to me through my twitter account (<a href="https://twitter.com/MaedahBatool">@MaedahBatool</a>).