文章目录
一、问题原因及报错展示
1.原因
- 没有权限
Permission denied (publickey)
:权限被拒绝(公钥)
- 出现这错误一般是以下两种原因:
- 1.客户端与服务端未生成 ssh key
- 2.客户端与服务端的ssh key不匹配
2.报错展示
2.1.第一次执行推送本地库到远程库命令报错(报错如下图所示)
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
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.
2.2报错后再次执行推送本地库到远程库命令还是报错(报错如下图所示)
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.
二、解决方法
1.查看是否生成密钥(打开Git Bash执行下面命令)
ls -al ~/.ssh
此处可以看到一对文件,其中以
.pub
结尾的是公钥文件,另一个没有后缀的是私钥文件。
2.生成SSH密钥(打开Git Bash执行下面命令)
ssh-keygen -t rsa -b 4096 -C "邮箱号"
此处邮箱号为创建GitHub账号的电子邮件地址,途中会让你输入密码啥的,不用管,一路回车即可,则会生成SSH密钥(如果重新生成的话会覆盖之前的SSH密钥)。
3.查看公钥文件(根据生成SSH密钥提示查看公钥文件所在目录位置)
4.将SSH公钥添加到GitHub
-
登录GitHub,点击头像
-
Settings
-
SSH and GPG keys→New SSH key
-
复制公钥
-
填写标题→粘贴公钥→Add SSH key
-
添加成功
-
推送本地库到远程
-
GitHub查看推送内容