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.
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.
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:
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), 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:
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.
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:
Now it’s time to copy the contents of the SSH key and add to the GitHub account. Type the following:
The SSH key appears as follow. Just copy it!
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.
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.