git clone Permission denied

现象描述

ck@iZwz9caju6kith0s06hhlvZ:~/temp$ ssh -vT git@git.oschina.net
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /home/ck/.ssh/config
debug1: /home/ck/.ssh/config line 1: Applying options for git.oschina.net
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to git.oschina.net [120.55.226.24] port 22.
debug1: Connection established.
debug1: identity file /home/ck/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ck/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
debug1: Remote protocol version 2.0, remote software version OSChina.NET
debug1: no match: OSChina.NET
debug1: Authenticating to git.oschina.net:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc
debug1: Host 'git.oschina.net' is known and matches the ECDSA host key.
debug1: Found key in /home/ck/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/ck/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).
Authenticated to git.oschina.net ([120.55.226.24]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Welcome to Git@OSC, CK星空!
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2648, received 1408 bytes, in 0.2 seconds
Bytes per second: sent 14288.7, received 7597.6
debug1: Exit status 0

这是我的测试代码,显示通过

但是git clone 的时候,依然是

Permission denied (publickey).
fatal: Could not read from remote repository.

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

公钥配置好了,私钥也添加了,甚至防火墙都关了,为什么还报错呢?

阅读 5.7k
4 个回答

我遇到过的几种情况:

  1. ssh公钥放错地方(Git上有用户ssh公钥和项目ssh公钥,通常应当放在用户ssh公钥上);
  2. 配置git的ssh地址时,可以尝试去掉ssh://(如果没有就加上),比如将ssh://git@git.oschina.net写成git@git.oschina.net;
  3. 还有一种情况可能跟路由器有关,不过我没了解过。
git config --global user.name "test"
git config --global user.email "email@test.com"

那么你的git clone的地址,是ssh地址吗

解决了,原来命令是git clone,然后自己习惯性用了sudo git clone 导致的问题。 用sudo的话,用户是root,root用户是没有公钥的,没有公钥当然克隆不了。 这个问题困扰了我整整一天!!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进