Generating A New SSH Key and Adding It to Github

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.

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.



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.

What are SSH Keys?

SSH (Secure Shell), 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:

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

This calls for the creation of an SSH key. Generating an SSH key results in two long string of characters which includes a public and a private 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.

Figuring Out Any Existing SSH Keys

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.

ls -al ~/.ssh

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:

  • id_rsa.pub
  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

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., id_rsa.pub and id_rsa), 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:

Checking for existing SSH keys

Generating a New SSH Key

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.

ssh-keygen

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 id_rsa.pub. 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:

Generating a new SSH key

Copying the SSH Key

Now it’s time to copy the contents of the SSH key and add to the GitHub account. Type the following:

cat ~/.ssh/id_rsa.pub

The SSH key appears as follow. Just copy it!

Copying the SSH Key

Adding SSH Key to GitHub

Now heading to the final part where I’ll add the SSH key to my GitHub account. In your GitHub Profile open Settings. Go to the section for SSH and GPG Keys. Click the New SSH key button, write a suitable Title, paste the copied Key and hit the Add SSH key 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.

add ssh key to github

👩‍🏫 Your Turn!

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 Share on Twitter with the hashtag #GirlDevMinute or reach out to me through my twitter account (@MaedahBatool).

Maedah Batool

ⓦ WordPress Marketing Team Representative & WP Core Contributing Developer ❯ 🎩 #OpenSourceress ❯ ✍️ Tech Journalist, Developer & Teacher ❯ 👩‍💻 OSS Content Program Manager at TheDevCouple ❯ 👩‍🏫 Taught thousands of girls how to code #GirlsWhoCode ❯ 💜 Love my husband (Ahmad Awais), Minions, and 🍕

WPCouple Partner:

Leave a Reply