DEV Community

Cover image for How to Generate and add an SSH key to GitHub
Jose Horta Calvario
Jose Horta Calvario

Posted on

How to Generate and add an SSH key to GitHub

Introduction

When dealing with a GitHub repository using commands, you will frequently need to identify yourself with your username and password. Instead, you can use an SSH key to authenticate yourself. As a result, entering the email and password will no longer be necessary.

SSH keys come in pairs, with a public key shared with services such as GitHub and a private key held solely on your machine. You will be allowed access if the keys match.

What is an SSH key?

An SSH key is an access credential in the SSH protocol. Its function is similar to that of user names and passwords, but the keys are primarily used for automated processes.

Hands-on Lab:

Effort : 30 mins

Objectives

After completing this reading, you will be able to:

  1. Generate an SSH key
  2. Adding an SSH key to GitHub

Prerequisites

If you intend to run this lab on your system, please ensure you have the following:

  • A basic understanding of working with the bash terminal
  • A Laptop or PC with Windows installed
  • A Internet connection
  • Git installed on your local machine. For more details on accomplishing this, review Installing & Setting Up Git on Windows.
  • A GitHub account.
  • The latest version of Visual Studio Code is installed on your machine.

1. Creating a GitHub account (optional)

To create an account in GitHub, complete the following steps:

1 - Go to the Join GitHub page and signup and create an account.

Note: If you already have a GitHub account, log in now.

GitHub sign in/sign up.

2 - For more details on accomplishing this, review:
How to get started with git and github

2. Generating an SSH key

To generate an SSH key, complete the following steps:

1 - Launch a terminal. If you are using Windows, launch Git Bash.

2 - Type the following command in your terminal, replacing your email address with the email address that is linked to your GitHub account. When you have typed the command, press Enter.

ssh-keygen -t rsa -b 4096 -C "your email address"
Enter fullscreen mode Exit fullscreen mode
A new SSH key is generated.
Enter fullscreen mode Exit fullscreen mode

3 - You will be prompted to enter a directory to save the key. You can simply press Enter to accept the default location, which is a .ssh folder in the home directory. This means you will be able to locate the key in ~/.ssh/id_rsa.

4 - You will be prompted to choose a passphrase. You also have the option not to create a passphrase. To skip the passphrase, press Enter twice to confirm that the passphrase is empty.

5 - Optional: To navigate to the .ssh directory, and check the contents of the directory, run the following commands in the terminal:

cd ~/.ssh
Enter fullscreen mode Exit fullscreen mode

and then,

ls
Enter fullscreen mode Exit fullscreen mode

When you list the contents of the .ssh directory, you should see id_rsa and id_rsa.pub in the list of contents, where id_rsa is the private version of your key and id_rsa.pub is the public version of your key.

6 - You now need to add the SSH key to the ssh-agent, which helps with the authentication process. To start the ssh-agent, run the following command in the terminal:

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

7 - To add the key to the agent, run the following command in the terminal:

ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

3. Adding an SSH key to GitHub

To add an SSH key to GitHub, you need to copy the SSH key that you generated in the previous lab. Open a terminal and then complete the following steps:

1 - In the terminal, run the following command:

cat ~/.ssh/id_rsa.pub | clip
Enter fullscreen mode Exit fullscreen mode

Note: If clip doesn't work, run cat ~/.ssh/id_rsa.pub in the command line and copy the output.

2 - Sign in to GitHub. At the top right, click the drop-down menu on your profile image and select Settings.

image 1

3 - From the "Personal settings" menu, select SSH and GPG keys, as shown in the following image:

image 2

4 - Click New SSH key.

image 3

5 - Enter a title for the new SSH key.

5.2 - In the Key field, paste the key that you copied in step 2. Genarating an SSH key, above.

Note: The pasted key should include Your email address at the end.

5.3 - Hit the button Add SSH key

image 4

6 - Click Add SSH Key. The SSH key is added to your account.

Summary

Congratulations! You have now learned how to generate the SSH key and add an SSH key to GitHub.

Git and GitHub learning resources

There are a lot of helpful Git and GitHub learning resources on the web. This is a short list of my favorites!

Conclusion

Now you know more about generating and adding an SSH key to Github, Feel free to these steps to make sure you understand how they work.

Download this Git commands cheat sheet to have all Git commands in one place for future use.

What's Next?

If you want to follow along with more advanced stuff in the next article I'll talk about GitBash Installation on your Desktop.

Thanks for the read! Now go practice & build something awesome!

Top comments (0)