github添加SSH Key

时间:2022-05-01 06:49:05

1.背景介绍

缘由:在github上下载*时出现错误。

在终端使用命令:git clone git@github.com:phuslu/*.git

会报权限错误如下:

Cloning into '*'...
The authenticity of host 'github.com (192.30.252.128)' can't be established.
RSA key fingerprint is ::ac:a5:::2d:::1b::4d:eb:.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

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

2.原因及解决办法

  使用git clone命令从github上同步github上的代码库时,如果使用SSH链接(如git clone git@github.com:phuslu/*.git),而你的SSH key没有添加到github帐号设置中,系统会报上面的错误。

(补充知识点:SSH key allows you to establish a secure connection between your computer and GitLab)

  这时需要在本地创建SSH key,然后将生成的SSH key文件内容添加到github帐号上去。

3.解决问题的步骤

(1)To generate a new SSH key just open your terminal and use code below:

ssh-keygen -t rsa -C "你的邮箱"

会提示SSH key存储的路径(不能自己改路径?)

(2)获取SSH key:

生成的SSH key文件保存在中~/.ssh/id_rsa.pub文件中。

用文本编辑工具打开该文件,在linux下可以用cat命令显示id_rsa.pub中的内容(cat  ~/.ssh/id_rsa.pub),然后复制其内容;如果在linux下,也可以直接使用记事本打开该文件,但一定要注意其内容中包含空格和换行,要将其去掉。

(3)接着拷贝.ssh/id_rsa.pub文件内的所以内容,将它粘帖到github帐号管理中的添加SSH key界面中:

进入https://github.com/settings/ssh,点击SSH Keys 》Add SSH Key,粘贴.ssh/id_rsa.pub文件内的所以内容,如下图:

github添加SSH Key

此时,再使用SSH链接(如git clone git@github.com:phuslu/*.git),就可以成功clone了。