前言

GitHub仓库,用着用着就会出现推送和拉取失败,提示认证失败,上一次出现是半个月前,啥都没改过,有必要记录一下。

可能原因

根据日志提示,很确定的是无权限被GitHub给拒绝了,至于原因可能有:

  • 账号未登录:这个原因很明显不成立,公开的仓库本来就不需要登录GitHub账号
  • token过期:这个有可能,退出重新登录尝试
  • 没权限访问该仓库:如果是某些私有仓库,未登录自己的账号则会被拒绝,此处也不是这个原因
  • 仓库被存档:如果仓库是存档或删除状态,此时去操作是会失败的,自行确认
  • SSH认证失败:这个是常见原因

现象

The error was parsed as 2: Authentication failed

同时在coding和GitHub使用的同一个秘钥对,但就是GitHub仓库无法认证,GitHub explorer程序日志提示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(The error was parsed as 2: Authentication failed. Some common reasons include:

- You are not logged in to your account: see File > Options.
- You may need to log out and log back in to refresh your token.
- You do not have permission to access this repository.
- The repository is archived on GitHub. Check the repository settings to confirm you are still permitted to push commits.
- If you use SSH authentication, check that your key is added to the ssh-agent and associated with your account.)
2019-10-12T14:24:58.619Z - info: [ui] Current tutorial step is NotApplicable
2019-10-12T14:25:00.202Z - info: [ui] Current tutorial step is NotApplicable
2019-10-12T14:25:00.203Z - info: [ui] Background fetch for 16 repositories took 34.089sec
2019-10-12T14:29:18.575Z - info: [ui] Current tutorial step is NotApplicable
2019-10-12T14:29:23.113Z - info: [ui] Executing fetch: git -c credential.helper= fetch --progress --prune origin (took 1.658s)
2019-10-12T14:29:24.144Z - info: [ui] Current tutorial step is NotApplicable
2019-10-12T14:29:30.855Z - info: [ui] [BranchPruner] Last prune took place in a day - skipping
2019-10-12T14:29:31.834Z - info: [ui] Current tutorial step is NotApplicable
2019-10-12T14:29:33.558Z - info: [ui] Executing fetch: git -c credential.helper= -c protocol.version=2 fetch --progress --prune origin (took 2.515s)
2019-10-12T14:29:33.558Z - error: [ui] `git -c credential.helper= -c protocol.version=2 fetch --progress --prune origin` exited with an unexpected code: 128.
stderr:
Host key verification failed.
fatal: Could not read from remote repository.

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

解决方案

针对以上可能的原因,我们需要逐个去检查确认,然后测试结果。一些常见账号操作没什么好说的,直接跳到重点部分。

重新配置SSH密钥

有时候系统环境变化,可能会出现本地SSH私钥丢失等情况,那么我们可以重新配置一遍,本地重新生成秘钥对或者GitHub上删除重新添加公钥。重新生成秘钥对方法如下:

打开 gitbash 程序,运行如下命令

1
2
3
4
git config --global user.email 'youremail@example.com'  #设置Git邮箱
git config --global user.name 'yourusername' #设置Git用户名
ssh-keygen -t rsa -C 'youremail@example.com'
#上面的命令一般一路默认就可以了,`.pub`是公钥,把私钥和公钥自己妥善保管

设置Git邮箱和用户名这个如果没提示不操作也可以,或者在客户端设置中设置即可,都是一样的。生成的公钥用记事本打开,全部复制里面的内容添加到GitHub账号的 SSH keys 设置中。

删除 known_hosts 中已有GitHub地址

这个文件在系统用户目录下,是个系统保护目录,Windows路径为:

1
C:\Users\用户名\.ssh\known_hosts  #这里是win10路径,其他版本大同小异

找到后用记事本打开,将GitHub相关地址整行删除掉,保存即可。下次重新连接时会提示是否添加,输入 yes 即可。

测试SSH

配置完 SSH keys 后,我们可以对结果进行测试,有时候这一步客户端软件还不灵,在bash中测试一把还能解决某些问题。测试命令如下:

1
ssh -Tv 'vps上的git用户名@vps地址' -p 'ssh端口'  #测试ssh连接是否成功

测试GitHub的连通性则命令是这样的:

1
ssh -Tv git@github.com

不出意外的话会输出一坨信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
PS D:\Git\Shell> ssh -Tv git@github.com
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
debug1: Connecting to github.com [13.250.177.223] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_ed25519-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_xmss type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\zgw50/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_7.7
debug1: Remote protocol version 2.0, remote software version babeld-003ebee6
debug1: no match: babeld-003ebee6
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: read_passphrase: can't open /dev/tty: No such file or directory
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

如果提示是否添加指纹,输入 yes

1
2
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes

最后提示成功认证,就表示可以正常操作了:

1
Hi sobaigu.com! You've successfully authenticated, but GitHub does not provide shell access.

参考文档

git用户设置

SSH密钥生成与部署