SSH方式登录github出现Permission denied (publickey)

  今天在公司上传了代码,回到家pull,结果竟然出现了“Permission denied (publickey)“这种东西。第一反应是key不对,可是上次明明用key登录过,不可能不对啊,难道是文件被删除或覆盖?赶紧ls .ssh瞅一眼,发现上次生成的git、git.pub文件好好的在那呢。再想想,我也没动过github上的key设置啊,在公司都是https方式push的呢。确认不是key的设置问题,在http://henzil.easymorse.com/?p=508找到了解决方案。

  下面记录一下解决过程:

x*z@x*z-HP-ProBook-4446s:~/code/game_server$ git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
x*z@x*z-HP-ProBook-4446s:~/code/game_server$ ^C
x*z@x*z-HP-ProBook-4446s:~/code/game_server$ ssh -T git@github.com
Permission denied (publickey).
x*z@x*z-HP-ProBook-4446s:~/code/game_server$ ls .ssh
ls: 无法访问.ssh: 没有那个文件或目录
x*z@x*z-HP-ProBook-4446s:~/code/game_server$ cd ~
x*z@x*z-HP-ProBook-4446s:~$ ls .ssh
git  git.pub  Identity  Identity.pub  known_hosts
x*z@x*z-HP-ProBook-4446s:~$ ssh -vT git@github.com
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [192.30.252.130] port 22.
debug1: Connection established.
debug1: identity file /home/x*z/.ssh/id_rsa type -1
debug1: identity file /home/x*z/.ssh/id_rsa-cert type -1
debug1: identity file /home/x*z/.ssh/id_dsa type -1
debug1: identity file /home/x*z/.ssh/id_dsa-cert type -1
debug1: identity file /home/x*z/.ssh/id_ecdsa type -1
debug1: identity file /home/x*z/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/x*z/.ssh/id_ed25519 type -1
debug1: identity file /home/x*z/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
debug1: Remote protocol version 2.0, remote software version libssh-0.6.0
debug1: no match: libssh-0.6.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /home/x*z/.ssh/known_hosts:4
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/x*z/.ssh/id_rsa
debug1: Trying private key: /home/x*z/.ssh/id_dsa
debug1: Trying private key: /home/x*z/.ssh/id_ecdsa
debug1: Trying private key: /home/x*z/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
x*z@x*z-HP-ProBook-4446s:~$ ssh-add .ssh/git
git      git.pub  
x*z@x*z-HP-ProBook-4446s:~$ ssh-add .ssh/git
Identity added: .ssh/git (.ssh/git)
x*z@x*z-HP-ProBook-4446s:~$ ssh-add .ssh/git.pub 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '.ssh/git.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
x*z@x*z-HP-ProBook-4446s:~/code/game_server$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 2), reused 5 (delta 2)
Unpacking objects: 100% (5/5), done.
来自 github.com:changnet/game_server
   d12f07e..72ef640  master     -> game_server/master
更新 d12f07e..72ef640
Fast-forward
 common/CLogFile.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 common/CLogFile.h   | 30 ++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 common/CLogFile.cpp
 create mode 100644 common/CLogFile.h
x*z@x*z-HP-ProBook-4446s:~/code/game_server$ 

   PS:ssh-add指令我也不是很熟悉,上面尝试添加public key时的警告请大家再查资料了。有时候.ssh目录下的key太多,可能会导致ssh登录不上,这时需要写ssh配置文件解决。

原文地址:https://www.cnblogs.com/coding-my-life/p/4049566.html