How to generate an SSH key for GitHub

18.08.2020 8,820 2

What is an SSH key?

The SSH key is a simple login solution. When you visit a website or a repository in this case, you need to identify yourself. This usually happens with a username and a password. But there is an alternative way. You can identify yourself with an SSH key. It is an access credential encrypted for extra security. It is a combination of a private and a public key.

What is GitHub?

GitHub is a repository service. Developers use it to store and administrate their code. It has good features for managing, keeping track, and collaboration. It is very important to keep a copy of the working code, but also to keep updating it in a safe way. With Git, you can control the version of your software without getting lost.
In case your code is open-source, you can store it in GitHub for free.

How to generate an SSH key?

So, generating an SSH key is easy and fast, but it has a small difference, depending on your OS.

Windows

Open the pre-installed Git Bash from your computer. You can download it from here (https://desktop.github.com/).
Now paste this text in it, changing the email to yours:

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

It will use the email as a label and create the new SSH key. Generating public/private rsa key pair.
You will get a message “Enter a file in which to save the key,”. Press Enter and it will be saved in the default location (c/Users/you/.ssh/id_rsa)).
> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
After that, you will get a prompt, telling you to type in the passphrase.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

If you want to add the SSH key to the ssh-agent, follow the next steps:
Install GitHub Desktop, if you haven’t already. Later you can use it to clone repositories and remove the need to deal with SSH keys.
First, check that the ssh-agent is running. You can start it with the following command:
# start the ssh-agent in the background

$ eval $(ssh-agent -s)

Agent pid 59566
Second, add the private SSH key to the ssh-agent. If the key has a different name than the default id_rsa, replace it in the line:

$ ssh-add ~/.ssh/id_rsa

The steps for uploading it to GitHub can be found further below in this article.

МacOS

First, open the Terminal
Paste this text and change the email to your email for GitHub:

$ ssh-keygen -t rsa -b 4096 -C your_email@example.com

Using your email as a label, create a new SSH key
> Generating public/private rsa key pair.
When you get a message “Enter a file in which to save the key”, click Enter.
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
The next message you will get is about the passphrase, type it in.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Now, for adding it to the ssh-agent, use the default macOS command (ssh-add).
Start the agent in the background with:

$ eval "$(ssh-agent -s)"

Agent pid 59566
You will need to edit your ~/.ssh/config to auto-load keys into the agent and store the passphrases in the keychain. Check first if ~/.ssh/config is in the default location:

$ open ~/.ssh/config

The file /Users/you/.ssh/config does not exist.
If it shows that it does not exist, you need to create it with the following command:

$ touch ~/.ssh/config

Now open it and edit the file, replacing the ~/.ssh/id_rsa if you don’t have the default location and name.
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Now add the private SSH key to the ssh-agent and your passphrase inside the keychain. If the key has a different name, change it in the line:
$ ssh-add -K ~/.ssh/id_rsa
Now take a look at the instructions to upload it on GitHub below.

Linux

You need to open the Terminal first.
Then paste the following text, changing the email to your GitHub email:

$ ssh-keygen -t rsa -b 4096 -C your_email@example.com

It will generate the SSH key, using the email as a label.
> Generating public/private rsa key pair.
Next, you will get the prompt “Enter a file in which to save the key” and press the Enter to save to the default location.
> Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]
Next is the passphrase.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Now that we have the key, let’s add it to the ssh-agent.
Start the agent with:

$ eval "$(ssh-agent -s)"

Agent pid 59566
Add the key inside it. If you have put another name, please rename it in the command:

$ ssh-add ~/.ssh/id_rsa

Done! Now follow the steps below to upload it on the website.

How to upload the SSH key to GitHub?

The interface of GitHub is easy to understand. Here are the instruction on how to upload your SSH key and how to set it up correctly.

Windows

Copy the SSH key. Use the following command. If you have another name for it, please edit it in the code:

$ clip < ~/.ssh/id_rsa.pub

# Copies the contents of the id_rsa.pub file to your clipboard
You can also manually locate the file, open it with a Notepad, and copy the text.
On the page of GitHub click on the SETTINGS at the upper-right corner.
After that open the SSH and GPG keys.
Press the New SSH key. Choose a title that will help you know for which computer it is and paste the Key bellow in the Key field.
Press the Add SSH key, and if it asks for your password, just write it. Ready!

МacOS

Copy the SSH key. You can locate the file, open it and copy the text, or use the command:

$ pbcopy < ~/.ssh/id_rsa.pub

# Copies the contents of the id_rsa.pub file to your clipboard
Got to GitHub and locate the SETTINGS at the top right corner.
In the SETTINGS, open the SSH and GPG.
There press the New SSH key button.
Choose a title that describes well the computer and in the Key field, paste the key.  Press the Add SSH key button and when a message for your password appear, type it in and confirm it. Done!

Linux

Copy the key. You can open the file from the folder you saved it in and copy the text or use the command:

$ sudo apt-get install xclip

# Downloads and installs xclip. If you don’t have `apt-get`, you might need to use another installer (like `yum`)

$ xclip -sel clip < ~/.ssh/id_rsa.pub

# Copies the contents of the id_rsa.pub file to your clipboard
Now on the web page, search for SETTINGS at the top right part of the screen.
Locate the SSH and GPG keys.
Click on the New SSH key and choose an appropriate title. Down in the Key field, paste the SSH key.
When you are ready press the button on the page that says “Add SSH key” and you will need to confirm your password after that. Ready!

Sources:

https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

2 replies on “How to generate an SSH key for GitHub”

ks quik

… [Trackback]

[…] Find More Information here to that Topic: blog.neterra.cloud/en/how-to-generate-an-ssh-key-for-github/ […]

วิ่งมาราธอนครั้งแรก

… [Trackback]

[…] Read More to that Topic: blog.neterra.cloud/en/how-to-generate-an-ssh-key-for-github/ […]

Leave a Reply

Your email address will not be published.