<!--
  canonical-url: https://www.avonture.be/blog/github-connect-using-ssh
  published:     2024-03-09
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# GitHub - Connect your account using SSH and start to work with git@ protocol

> Secure your GitHub connection! Follow this easy, step-by-step guide to set up SSH keys and use the git@ protocol for safer and quicker Git operations.

---
![GitHub - Connect your account using SSH and start to work with git@ protocol](/img/v2/github_tips.webp)

**TL;DR**

This article explains why connecting to GitHub over SSH is more secure than HTTPS, then walks through the setup: generating an ed25519 key pair with `ssh-keygen`, adding it to the SSH agent, and pasting the public key into GitHub's SSH key settings. It finishes with a quick `ssh -T git@github.com` command to verify the connection works, so you can start using the `git@` protocol with `git clone`.

Using SSH instead of HTTPS to connect to GitHub is more secure. Indeed, SSH relies on public-key cryptography. This makes unauthorized access much harder compared to a password, which can be stolen through phishing or brute-force attacks. Also, HTTPS transmits your username and password (encrypted) over the network, which can be intercepted in a Man-in-the-Middle (MITM) attack. SSH doesn't transmit passwords after the initial setup.

Let's see how to add an SSH key and, from now on, be able to work with GitHub using the `git@` protocol with `git clone`.

Adding an SSH key to your computer and using it to connect to GitHub is quite easy.

First, run the command below on your computer. Replace `your_email@example.com` with the email linked to your existing GitHub account.

```bash
$ ssh-keygen -t ed25519 -C "your_email@example.com"
```

You'll be prompted to enter a *passphrase*, this is not required so just press `Enter`.

You'll then see something like this on your console:

```bash
Generating public/private ed25519 key pair.
Enter file in which to save the key (~/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ~/.ssh/id_ed25519
Your public key has been saved in ~/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx your_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
| ( ...... )      |
| ( ...... )      |
| ( ...... )      |
| ( ...... )      |
+----[SHA256]-----+
```

Then you'll need to add the key to your SSH agent. Simply run:

```bash
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519
```

Finally, add the key to GitHub by surfing to <https://github.com/settings/ssh/new>.

![GitHub - Add SSH key](./images/ssh_add_key.webp)

Give a clear title like f.i. `Home computer`.

In the `Key` textarea, you'll need to paste there your **public** key.

Take a look at what appeared on screen previously when running the `ssh-keygen` command. The public key path was mentioned; f.i., `Your public key has been saved in ~/.ssh/id_ed25519.pub`.

So, just run `cat ~/.ssh/id_ed25519.pub` in your Linux console and you'll get the key value. Copy/paste that line into the GitHub settings page and, then, click on the `Add SSH key` button.

Finally, if you want to test if the connection is successfully created, just run `ssh -T git@github.com`. You should get `Hi cavo789! You've successfully authenticated, but GitHub does not provide shell access.` (with your own pseudo of course).

Two follow-ups: if you have repositories already cloned over HTTPS, [Git - Some tips for your .gitconfig file](/blog/git-config) shows a one-line setting to force SSH globally without re-cloning anything; and the same key mechanism works for your hosting server, see [How to connect to your hosting server using SSH](/blog/connect-using-ssh-to-your-hosting-server).
