从2021年8月13号开始GitHub不再支持用户名和密码的方式验证,提交更新时会收到这样的提示:
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: unable to access ‘https://github.com/xxxx/xxxx.git/’: The requested URL returned error: 403
那么接下来就来修它,以windows为例,打开git bash:
1、检查本地是否配置ssh key密钥
admin@IBM MINGW64 /
$ cd
admin@IBM MINGW64 ~
$ ls .ssh
admin@IBM MINGW64 ~
$
如果已经配置过的会在.ssh目录中列出密钥文件:
id_rsa (私钥)
id_rsa.pub(公钥)
如果没有就进行下面的新建一对
2、新建一对ssh key
admin@IBM MINGW64 ~
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
参数-C后面是你的github的邮箱账号
Enter file in which to save the key(/c/Users/admin/.ssh/id_rsa):
上面是提示你密钥文件的保存路径,默认即可
Enter passphrase(empty for no passphrase)
提示你输入私钥的密码,可以不输入,这样每次提交的时候就不用输密码了,直接回车密码为空
Your identification has been saved in /c/User/admin/.ssh/id_rsa
Your public key has been saved in /c/Users/admin/.ssh/id_rsa.pub
最后出现上面的提示说明创建成功。
3、将id_rsa.pub公钥导入到github仓库中
首先查看一下公钥的内容
admin@IBM MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCqHr05FvFOCV23YZ1przK9SRIY9h3pFy8NiUd58IDJcIvHX+HbmSGqwLWH2OxddUqRCqQOZWZmZ0YtWkdcDJ0sAl2vCoqG7qvUezCC3NwU20ByOcndKp08SUDqIT8lQ+JMXcBYSTWqwcEMPHiNlx8KPJER64IwvJadhaFMeVVHBCXO9V8PrU/sF/d45tmvfDcgXjjE/Ww7m73IzErLKaSc4GliCnXI1zimZBxukcrL9r4azRpUSXYyuS7ACM8LCv+CofZrySqh99h4UbyZ6u4fM2sHRCRJxLp9s3r25c+1nw7m69BAXKM4vvpkFPfMeRSVIbU2VgRwqTEOtqVzr9v [email protected]
将上面公钥内容全部复制,然后打开GitHub:
按照上图选择
找到左边的ssh选项,然后再点右边的new一个,将刚才复制的公钥内容全部贴进来,名字随便写,我写的是p52
4、测试是否成功
admin@IBM MINGW64 ~/.ssh
$ ssh -T [email protected]
Hi XXXXX! You've successfully authenticated, but GitHub does not provide shell access.
admin@IBM MINGW64 ~/.ssh
$
会有如上提示,或者
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 XXXX! You've successfully authenticated, but GitHub does not
provide shell access.
到此说明配置成功。goodluck!
5、补充一点:其他设备导入已经创建的私钥
同样以windows为例,linux也同样(证书权限设置成600),没有mac设备测试,有设备的可以测试一下
首先要将id_rsa私钥文件传到需要导入的设备上,路径要自己记住,打开git-bash:
admin@IBM MINGW64 ~/.ssh
$ ssh-add ~/.ssh/id_rsa
如果出现类似这样的错误:Could not open a connection to your authentication agent
解决办法就是先执行下面的命令再执行导入命令:
admin@IBM MINGW64 ~/.ssh
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa
完活!