Tutorial

How to Connect to GitHub Using SSH Key

2024-12-13 #GitHub#SSH

If you need to perform GitHub pull/push operations frequently, binding an SSH key to GitHub is much more convenient. Once bound, it uses public key cryptography to authenticate the user, which is more secure than the traditional username and password method.

The commands and screenshots in this article correspond to the following operating system versions. You need to install Git first:

  • Windows 11 : Generally install the 64-bit Git for Windows Setup, and keep all installation options at their default settings.
  • macOS 15.1 : It is recommended to obtain the Git program by installing Homebrew. See How to Install Homebrew on macOS.

Opening the Terminal Program

Windows

  • Right-click the Start menu and select Terminal (or Windows Terminal).
  • Or press the Windows + R shortcut to open the Run window, type cmd, and press Enter to open it.

macOS

  • Launchpad > Other > Terminal
  • Use Launchpad or press Command⌘ + Space to open Spotlight search, then search for Terminal or Terminal.app.

After installing Git, test it with git -v. If successful, the version number will be displayed:

1
2
> git -v
git version 2.47.1

Then configure your Git personal information:

1
2
git config --global user.name "yourname"
git config --global user.email "[email protected]"

Testing connection to github.com

Windows 10 users can use Git Bash.

The .ssh folder will be automatically created during the test, so you should run the test.

Open the terminal program and run the test command. If it is the first time running it, you need to type yes to confirm:

1
ssh -T [email protected]

Since we haven't added any SSH key yet, it will prompt Permission denied (publickey).

Generating an SSH Key

1
2
3
cd .ssh

ssh-keygen -t rsa -C "[email protected]"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
C:\Users\ME\.ssh>ssh-keygen -t rsa

Generating public/private rsa key pair.
# Set the filename to save the key, leave empty for id_rsa
Enter file in which to save the key (C:\Users\ME/.ssh/id_rsa):
# Set a passphrase, leave empty for no passphrase
Enter passphrase (empty for no passphrase):
# Confirm passphrase
Enter same passphrase again:

# Generated private key
Your identification has been saved in C:\Users\ME/.ssh/id_rsa
# Generated public key
Your public key has been saved in C:\Users\ME/.ssh/id_rsa.pub

The one with the .pub extension is the public key, which can be made public. The private key is equivalent to your personal password; keep it safe and secure.

By default, Git will look for an SSH key named id_rsa.

If you specified a custom file name or generated an SSH key in another format, you can refer to the section on using SSH config later in this article.

RSA format keys are quite long. You can use ssh-keygen -t ed25519 to generate an ed25519 format key, which is shorter and more secure.

Adding the Public Key to GitHub

View the content of the public key using a command or a text editor:

  • Windows: Use type id_rsa.pub
  • macOS / Linux: Use cat id_rsa.pub

Visit GitHub - SSH Keys and click New SSH key in the top right corner.
Manual navigation path: Click your Avatar in the top right > Settings > SSH and GPG keys > New SSH key

github-add-ssh-key

  • Title: Enter a descriptive note name.
  • Key type: Keep the default Authentication Key.
  • Key: Paste the content of your id_rsa.pub file.
  • Click Add SSH key to save.

github-add-ssh-key-details

Once successfully added, it will display in the list.
github-add-ssh-key-done

Now when we run the test command again, we will receive a successful welcome message:

1
2
>ssh -T [email protected]
Hi <Your username>! You've successfully authenticated, but GitHub does not provide shell access.

Using with SSH Config

If your private key is not named id_rsa, you will still be unable to connect to GitHub successfully. You can try renaming the private key and testing again:

1
2
3
4
> rename id_rsa id_mysshkey

> ssh -T [email protected]
[email protected]: Permission denied (publickey).

To fix this, we can create a file named config in the .ssh folder and fill it with the following content to tell Git where to find the key:

1
2
3
4
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_mysshkey

In this file, IdentityFile specifies the path to the private key, and ~ represents the user's Home directory:

  • Windows: Equivalent to C:\Users\<username>\
  • macOS: Equivalent to /Users/<username>/
  • Linux: Equivalent to /root/ or /home/<username>/

Run the test again, and you should see the successful welcome message.

1
2
>ssh -T [email protected]
Hi <Your username>! You've successfully authenticated, but GitHub does not provide shell access.

Now you can use Git with ease!

Comments
Share

Comments