这一篇博客主要讲解怎么在windows环境下配置SSH key,以及添加到gubhub帐号中。官方教程传送门Generating SSH keys

SSH keys are a way to identify trusted computers, without involving passwords.

Step 1:检查电脑中是否已经有已存在的SSH key


首先我们需要检查电脑中已存在的SSH key,打开Git bash 输入下面命令:

1
2
3
yaohp@Lenovo-PC MINGW64 ~/Desktop
$ ls -al ~/.ssh
ls: cannot access /c/Users/yaohp/.ssh: No such file or directory

报这个错,表示电脑不存在SSH key

如果电脑中存在SSHkey的话将会列出类似下面的目录结构

1
2
3
4
id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub

 ~/.   表示当前目录,注意先cd 到当前 .ssh目录 然后再开始下面生成ssh key的步骤

Step 2:生成一个新的SSH key

在Git bash终端输入下面命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
yaohp@Lenovo-PC MINGW64 ~/Desktop
$ ssh-keygen -t rsa -b 4096 -C "youremail@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/yaohp/.ssh/id_rsa): filename
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in filename.
Your public key has been saved in filename.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx youremail@qq.com
The key's randomart image is:
+---[RSA 4096]----+
| .. ..o...+|
| ..+ . . ..|
| ..o o . .|
| .E. . . . |
| ....S.+ . o |
| +. o+xxx =.|
| o . =.+.=+ =|
| =.+o=* |
| .===+o.|
+----[SHA256]-----+

出现上面这样就代表SSH key生成成功了,直接进入Step 3。

Step 3:登录git 设置ssh key

登录git账号点击头像进入个人中心---------》点击SSH and GPG keys------------>点击New SSh key
title随便写一个,然后key 的内容,则进入.ssh目录中打开刚才你生成的 xx.pub,复制里面的内容黏贴到key中保存

Step 4:测试连接

1
2
3
4
5
6
7
yaohp@Lenovo-PC MINGW64 ~
$ ssh -T git@github.com
Hi xxxxxxx! You've successfully authenticated, but GitHub does not provide shell access.
连接成功
参考链接:https://www.yaohepeng.com/2015/05/08/Github%E4%BD%BF%E7%94%A8%EF%BC%88%E9%85%8D%E7%BD%AESSHkey%EF%BC%89/