When using GitHub’s SSH method to check out projects, it is necessary to set up an SSH key. This article explains how to generate an SSH key and how to configure it to Github.

Problem Background

On a new computer, when trying to check out a GitHub project, the following alert is displayed “Permission denied (publickey)”.

1
2
3
4
5
6
7
D:\workspace\github>git clone git@github.com:waylau/waylau.github.io.git
Cloning into 'waylau.github.io'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

At this point it is necessary to generate the SSH key.

Setting up a Git account and email

Because it is a new computer, you will need to set up your account and email after installing Git.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
D:\workspace\github>git config --global --list
fatal: unable to read config file 'C:/Users/wayla/.gitconfig': No such file or directory

D:\workspace\github>git config --global user.name waylau

D:\workspace\github>git config --global user.email waylau521@gmail.com


D:\workspace\github>git config --global --list
user.name=waylau
user.email=waylau521@gmail.com

where --list is used to view the Git configuration, and user.name and user.email are used to set the account and email.

Generating an SSH key

An example of generating an SSH key is shown below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
D:\workspace\github>ssh-keygen -t rsa -C waylau521@gmail.com
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\wayla/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\wayla/.ssh/id_rsa
Your public key has been saved in C:\Users\wayla/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:I+AU5AdjoMDyWQ6paW8rTK7LLLXXXO9LAHBe5ZkXdhmHYU waylau521@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
|+ .+B +...++     |
|++o=.* + E       |
|o=+==-+          |
|+oo*.o           |
|..* . . S        |
| o.*   . .       |
|+ +..            |
|.=.o .           |
|+.o.o            |
+----[SHA256]-----+

This will generate the private and public key files in the C:\Users\wayla/.ssh/id_rsa and C:\Users\wayla/.ssh/id_rsa.pub paths.

Adding a public key file to your GitHub account

The steps are as follows.

  1. Go to Settings.
  2. In “Access” click on “SSH and GPG keys”.
  3. Click on “New SSH key”.
  4. Add the SSH public key, copying the contents of id_rsa.pub

Verify

If the following command is executed successfully, the verification passes.

1
2
3
4
5
6
7
8
D:\workspace\github>git clone git@github.com:waylau/waylau.github.io.git
Cloning into 'waylau.github.io'...
remote: Enumerating objects: 3140, done.
remote: Counting objects: 100% (571/571), done.
remote: Compressing objects: 100% (263/263), done.
remote: Total 3140 (delta 298), reused 561 (delta 288), pack-reused 2569Receiving objects:  99% (3123/3140), 37.18 MiB |Receiving objects: 100% (3140/3140), 37.39 MiB | 3.05 MiB/s, done.
Resolving deltas:  28% (463/1651)
Resolving deltas: 100% (1651/1651), done.

Ref

  • https://waylau.com/github-generating-ssh-keys/