问题描述:
执行
git push origin master
提示 :
Permission denied (publickey).
fatal: Could not read from remote respository.
查到资料http://stackoverflow.com/questions/19660744/git-push-permission-denied-public-key,按照Eric Leschinski的步骤往下走。但是有依旧报错。究其原因,是最后两步cat ~/.ssh/id_rsa.pub和ssh -T git@github.com没有生效。
解决办法:
1,将连接方式从http更换为ssh。注意,github.com后面一定有(冒号):
git remote rm origin
git remote add origingit@github.com:username/respository.git
2,生成新的ssh key。这里会提示要不要rewrite,键入y,还有提示输入passPhrase,输入空格即可。
cd ~/.ssh
ssh-keygen
3,测试一下连接。
ssh -T -v git@github.com
不出意外的话这里会报错,最后几行提示信息如下:
debug1: Offering RSA public key: yourpath/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key:yourpath/.ssh/id_dsa
debug1: Trying private key:yourpath/.ssh/id_ecdsa
debug1: Trying private key:yourpath/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
提示信息表明路径 yourpath/.ssh下id_rsa文件存在,并且提供public key,然而无法和网站中的ssh-key匹配上。于是程序转而寻找yourpath/.ssh路径下的id_dsa文件,id_ecdsa文件,id_ed25519文件,看它们是不是能提供新的public key。
查看一下yourpath/.ssh,会发现下面根本不存在id_dsa,id_ecdsa,id_ed25519文件。 出错的原因其实在于步骤4更新了本地的ssh key,然而网站上的ssh key还为旧值,所以匹配不上,就报错Permission denied (publickey)
4,打开yourpath/.ssh/id_rsa.pub(.pub为公钥文件,id_rsa为私钥文件),复制里面的内容。
5,打开https://github.com/settings/profile,在SSH key那栏选择New SSH Key,并将复制内容拷贝到其中,选择保存。
6,再次测试$ ssh -T git@github.com,提示
You've successfully authenticated, but GitHub does not provide shell access.
错误排查完毕 _