设置GitHub SSH Key

Huzejun@618 发布于 2022-02-08 930 次阅读


看GitHub入门与实践这本书的时候发现在3.1的内容中,我并没有设置过SSH Key,于是按照步骤完成了笔记本电脑上SSH Key的设置,在此记录。本文中代码部分来自于GitHub设置

1. SSH?

Secure Shell (安全外壳协议,简称 SSH )是一种加密的 网络传输协议 ,可在不安全的网络中为网络服务提供安全的传输环境。

使用 SSH 协议可以连接远程服务器和服务并向它们验证。 利用 SSH 密钥可以连接 GitHub,而无需在每次访问时都提供用户名和个人访问令牌。超过一年不使用的话GitHub会将其删除

2. 在GitHub设置SSH Key

首先在如图位置进入GitHub的SSH Key设置页面

然后可以直观地看到自己有没有设置过SSH Key。如果没有,需要在本地进行命令创建SSH Key。(需要下载Git

下载完成后在开始菜单中可以找到如下图所示的Git Bash

在Git Bash中输入代码:

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

这意味着会以所输入的邮箱地址为标签创建新的SSH密钥。

> Generating public/private algorithm key pair.

接下来是文件位置设定与密码设置

> Enter a file in which to save the key (/c/Users/you/.ssh/id_algorithm):[Press enter]
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

这样本地的SSH Key就创建好了,接下来需要将GitHub与本地SSH Key进行设置。

3. 添加公开密钥

代码的方式如下所示

$ clip < ~/.ssh/id_ed25519.pub
# Copies the contents of the id_ed25519.pub file to your clipboard

也可以通过上一步安装位置下的id_ed25519.pub文件读取密钥(用记事本打开即可)。

将里面的内容输入到如下图所示的位置

添加后界面如下图所示:

3. 测试SSH连接

首先打开Git Bash

然后输入以下内容:

$ ssh -T git@github.com
# Attempts to ssh to GitHub

可能看到类似如下的警告,重点是第二行的指纹,将它与GitHub的RSA公钥指纹进行比对

> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
> RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
> Are you sure you want to continue connecting (yes/no)?

对的话就输入yes,最后弹出

> Hi username! You've successfully authenticated, but GitHub does not
> provide shell access.

大功告成!

如果报错的话,这里是GitHub相关的链接

此作者没有提供个人介绍。
最后更新于 2022-02-10