一、问题描述
在git clone一个项目时出现如下报错:
(第一个选项,问你是否继续连接?输入yes然后回车)
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
通过报错信息可以看出,这是因为Github上没有本机的公钥导致的。
二、解决办法
1、在终端输入
ssh-keygen -t rsa -C "[email protected]"
其中双引号中填自己Github注册账号用的邮箱地址。
点击回车,看到提示
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yichen/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yichen/.ssh/id_rsa
Your public key has been saved in /home/yichen/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:KfhnvKTaASEb6Efb0LKofhCnu6rLgBX72FC3gRykSQU [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| E++ |
| o +.o |
|. *+=.o |
|..oO*+ o . |
| oO+o.o S |
|o+.= o o |
|+ + o o = |
|+. . . * . |
|*=o ..o . |
+----[SHA256]-----+
说明:前三个冒号分别让你输入保存公钥的文件、一次性密钥和密钥确认,可以直接回车,他会默认创建密钥文件到指定路径下。看到如上图所示的乱码,说明公钥创建完成。
2、到路径下查看
我的路径是/home/yichen/.ssh/id_rsa.pub,(注意是后缀为.pub的文件,而不是id_rsa)
cat 显示文件内容,
cat /home/yichen/.ssh/id_rsa.pub
文件形式为
ssh-rsa 后面一堆字符
复制文件里的全部内容(包括前缀ssh-rsa)。
3、打开GitHub
点击右上角自己头像,打开下拉菜单,选择设置Settings
左边找到SSH和PGP密钥,点击new SSH Key,将之前复制的内容粘贴到里面,就行了。
Title栏可以不填,密钥类型选择Authentication Key。
然后GitHub让你确认登录,确认登录后就大功告成,可以正常Git Clone了。
版权归原作者 不是AI 所有, 如有侵权,请联系我们删除。