DEV Community

Cover image for Generating A New SSH Key and Adding It to Github
Maedah Batool πŸ‘©β€πŸ’»
Maedah Batool πŸ‘©β€πŸ’»

Posted on

Generating A New SSH Key and Adding It to Github

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:

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:

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!

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.

πŸ‘©β€πŸ« 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).

Top comments (9)

Collapse
 
daanwilmer profile image
Daan Wilmer

If you're on a mac, which seems to be the assumption anyway, you can also use pbcopy < ~/.ssh/id_rsa.pub to get your public key into the clipboard. If you're using linux, you can use use xclip and aliases to get exactly the same result: garywoodfine.com/use-pbcopy-on-ubu...

If you're using windows, well... I guess you could use the WSL to run bash?

Collapse
 
maedahbatool profile image
Maedah Batool πŸ‘©β€πŸ’»

Thanks for sharing it. I had this step in mind probably it got missed. :P

Collapse
 
darkain profile image
Vincent Milum Jr

This is one of those times why I freaggin LOVE using Yubikeys. They generate the SSH key internally, so they are secured. It has a full GUI experience that is quite simple to setup, and one-click copy to clipboard for the public key. Plus it's super convenient to switch between multiple computers, even public terminals, without worry of the private key being compromised, because it can never leave the Yubikey itself.

Collapse
 
tandavala profile image
Jose Tandavala

Thank you, this helped me!

Collapse
 
serranomorante profile image
Patricio

Thanks!

Collapse
 
tandavala profile image
Jose Tandavala

Thank you!

Collapse
 
jonathonllopez profile image
Jonathon Lopez

Thank you for making this post! I just spent 10 minutes following GitHub's documentation on this process, and could not get it to work. This was much simpler!

Collapse
 
jghar profile image
JGHar

If you are on Windows PC use cat ~/.ssh/id_rsa.pub | clip to get your public key into the clipboard.

Collapse
 
stacktracy profile image
πŸ‘©β€πŸ’»Tracy A King

This saved me! Thank you so much!