Skip to main content

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.

· 3 min read
View raw
Christophe
Markdown, WSL and Docker lover ~ Insatiable curious.
Cet article existe aussi en français — lire la version française.

Changelog

Latest
remove /root in paths; replaced by ~ for the current user

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

⚡
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.

>user@machine: ~/yourproject

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:

>user@machine: ~/yourproject

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

>user@machine: ~/yourproject

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

GitHub - Add SSH key

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 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.


Was this article helpful?
Did you try the steps in this article?