github:Error: Permission denied (publickey)解决方法

permittion denied错误,其实是暗示你对git服务器的链接被拒绝。一般原因有以下几个:

不要在git 命令前面加上sudo

一般情况下,不要在使用git的时候加上sudo
如果你非要用,在你需要取得一个以root身份登陆的shell的场合下,你可以用su 替代 sudo

确保你访问的server名字是[email protected]

打字的时候难免会发生错误。有可能你不小心把github.com打成了githib.com
下面的命令用来测试你访问的是正确的github:

ssh -vT [email protected]

OpenSSH_5.6p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data /Users/you/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: Applying options for *
debug1: Connecting to github.com [IP ADDRESS] port 22.

上面的输出代表你正在访问github.com

测试连接的时候用git用户

使用这条命令测试会发生错误:
ssh -T [email protected]
输出如下:

Permission denied (publickey).

正确的测试方式是:
ssh -T [email protected]

Hi username! You’ve successfully authenticated…
上面这样的输出表示认证成功。

确保你正在使用一个ssh key

使用下面的测试步骤来查看你有没有使用ssh key:

  1. 打开ssh-agent:
    eval "$(ssh-agent -s)"

    Agent pid 59566`

  2. 确认是否有ssh key且载入到ssh.
    ssh-add -l #OpenSSH 6.7 or older

    2048 a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/you/.ssh/id_rsa (RSA)

    或者
    ssh-add -l -E md5 # OpenSSH 6.8 or newer

    2048 MD5:a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/you/.ssh/id_rsa (RSA)

  3. 如果没有任何输出,表示当前没有在使用的ssh key .你需要去生成一个,并把它添加到github.

更多测试细节

也可以通过这条命令来查看是否有ssh key在使用:
ssh -vT [email protected]


debug1: identity file /Users/you/.ssh/id_rsa type -1
debug1: identity file /Users/you/.ssh/id_rsa-cert type -1
debug1: identity file /Users/you/.ssh/id_dsa type -1
debug1: identity file /Users/you/.ssh/id_dsa-cert type -1

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/you/.ssh/id_rsa
debug1: Trying private key: /Users/you/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).

注意到上面的命令中,-1说明找不到ssh key 文件。Trying private key也说明了这个问题。
如果连接正常,输出应该是下面这样:


debug1: identity file /Users/you/.ssh/id_rsa type 1

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/you/.ssh/id_rsa

确保你的ssh key 添加到了你的github账户

通过ssh-add -l查看你当前在使用的ssh key.
然后到github上点击你的用户照片,点击设置, 找到SSH and GPG keys,
就可以看到你添加的ssh key.
将这个key和你在本地用ssh-add -l看到的key 进行对比,确保你的key已经添加到了github。

参考链接:Error: Permission denied (publickey)

猜你喜欢

转载自blog.csdn.net/njnujuly/article/details/80881934