Debug School

rakesh kumar
rakesh kumar

Posted on

How to generate an SSH key and add it to your GitHub account:

Step 1: Check for Existing SSH Keys

Before generating a new SSH key, you should check if you already have one. To do this, open your terminal and run the following command:

ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

This command will list all the files in your ~/.ssh directory. If you see files like id_rsa and id_rsa.pub, you already have an SSH key.

Step 2: Generate a New SSH Key (if necessary)

If you don't have an SSH key or want to generate a new one, you can use the ssh-keygen command. Replace "your_email@example.com" with the email associated with your GitHub account:

ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
$ ssh-keygen -t ed25519 -C "rakeshdev.cotocus@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Image description

You can also use the older RSA algorithm instead of Ed25519, but Ed25519 is considered more secure.

The command will ask you where to save the SSH key. You can simply press Enter to accept the default location, which is ~/.ssh/id_ed25519 (or ~/.ssh/id_rsa if you choose RSA).

It will also ask you to set a passphrase for your SSH key. While it's not required, adding a passphrase can provide an extra layer of security. Enter a passphrase or press Enter to skip it.

Step 3: Start the SSH Agent

If you created a new SSH key with a passphrase, you should add it to the SSH agent. Use the following command to start the SSH agent:

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

Step 4: Add Your SSH Key to the SSH Agent

Now, add your SSH key to the agent:

ssh-add ~/.ssh/id_ed25519   # For Ed25519 key
# OR
ssh-add ~/.ssh/id_rsa       # For RSA key
Enter fullscreen mode Exit fullscreen mode

Step 5: Copy the Public Key to Your Clipboard

To add your public key to your GitHub account, you'll need to copy its contents to your clipboard. You can use the following command to do this (replace id_ed25519.pub or id_rsa.pub with your actual public key filename):

cat ~/.ssh/id_ed25519.pub   # For Ed25519 key
# OR
cat ~/.ssh/id_rsa.pub       # For RSA key
Enter fullscreen mode Exit fullscreen mode
$ cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

OUTPUT

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGpcj91NfiqlLttr9xLFpZuEVVBRrnWhQbwapHDp7wi1 rakeshdev.cotocus@gmail.com
Enter fullscreen mode Exit fullscreen mode

The command will display your public key. Copy it to your clipboard.

Step 6: Add the Public Key to Your GitHub Account

1.Open your web browser and go to GitHub (https://github.com).

2.Log in to your GitHub account if you aren't already.

3.Click on your profile picture in the top-right corner, and from the drop-down menu, select "Settings."

4.In the left sidebar, click on "SSH and GPG keys."

5.Click on the "New SSH key" button.

6.In the "Title" field, give your SSH key a descriptive name (e.g., "My Laptop SSH Key").

7.In the "Key" field, paste the public key you copied earlier.

8.Click the "Add SSH key" button.

Image description

Your SSH key is now added to your GitHub account, and you should be able to use it for authentication when interacting with GitHub repositories over SSH. This allows you to clone, push, and pull from GitHub without needing to enter your GitHub username and password each time.

Top comments (0)