Adding an SSH key to GitHub

Adding an SSH key to GitHub

What are SSH keys?

SSH keys are credentials used for the SSH Protocol (Secure Shell) to allow secure access to remote computers over the Internet. This authentication usually takes place in a command line environment.

SSH is used for remote file transfer, network management and remote access to the operating system.

Public key vs Private key

The SSH protocol uses a cryptographic technique called asymmetric cryptography. This term may sound complicated and strange, but nothing could be further from the truth.

Asymmetric cryptography is a system that uses a pair of keys, one of which is public and the other private. The public key can be shared with anyone.

Its main purpose is to encrypt data, converting the message into a secret code or ciphertext.

The private key is the one you keep to yourself. It is used to decrypt the data encrypted with your public key. Without it, it is impossible to decrypt your encrypted information.

This method allows you and the server to maintain a secure communication channel for transmitting information.

Generating an SSH key

Open your terminal and run the following command:

$ ssh-keygen -t ed25519 -C "email_github_of_preference"

Let's understand what each part of the command means:

  • ssh-keygen: The command line tool used to create a new SSH key pair. You can see your flags with ssh-keygen help

  • -t ed25519: The -t flag is used to indicate the algorithm used to create the digital signature of the key pair. If your system supports it, ed25519 is the best algorithm you can use to create SSH key pairs.

  • -C "email_github": The -c flag is used to provide a custom comment at the end of the public key, which is usually the email or ID of the key pair creator.

After typing the command into your terminal, you'll need to enter the file in which you want to save the keys. By default, it is located in the home directory, in a hidden folder called " .ssh ", but you can change it to whatever you want.

Enter file in which to save the key (/home/user/.ssh/id_ed25519): /home/user/.ssh/file_name

You will then be asked to add a password to your key pair. This adds an extra layer of security if, at any time, your device is compromised. It is not compulsory to add a password, but it is always recommended.

Enter passphrase (empty for no passphrase):

Here's the whole process:

$ ssh-keygen -t ed25519 -C "email_here"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/zero/.ssh/id_ed25519): /home/zero/.ssh/github
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/zero/.ssh/github
Your public key has been saved in /home/zero/.ssh/github.pub
The key fingerprint is:
SHA256:dRBEcSeZCIS42Y4xtvIxSFKGNooFqwUm2ta39eho2z0 email_here
The key's randomart image is:
+--[ED25519 256]--+
|++   . oo+*+oo.  |
|=*+.. .   .ooo   |
|*+= .+. . . .    |
|=o. *..o + .     |
|.o o *. S .      |
|  o = .o         |
|   o oo .        |
|    .. o .E      |
|      . . ..     |
+----[SHA256]-----+

At the end, the command generates two files in the directory you selected (usually ~/.ssh): the public key with the .pub extension and the private key without the extension.

Add the SSH key to the ssh agent

The ssh-agent is a program that runs in the background, keeping your private keys and phrases safe and ready for use.

For this reason, you will add your new private key to this agent. So:

Check that ssh-agent is running in the background.

eval `ssh-agent`
# Agent pid 10199

If you get a message similar to this, it means that the ssh agent is running under a specific process ID (PID).

Add your SSH private key (the one without an extension) to the ssh agent.

$ ssh-add ~/.ssh/file_name

If you haven't given your key a new name, the possible name is id_ed25519.

Add SSH key to GitHub account

Follow the instructions below:

Copy your SSH public key to your clipboard. You can open the file in which it is located with a text editor and copy it or use the terminal to display its contents.

./ssh$  cat public_key.pub

In your github make the following path:

Settings -> SSH and GPG keys -> New SSH key

Give your SSH key a title, usually the device from which you will use it, then paste the key into the key field and finish by clicking Add SSH key.

Finally, it's time to test the connection to a repository.

Don't forget to add a reaction if the post has helped.