GIT没有此身份:id_rsa:没有这个文件或目录。

26

我在 Github 上创建了一个仓库并想要推送我的代码,但是收到了以下提示:

$ git push -u origin master
no such identity: id_rsa: No such file or directory
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

但 id_rsa 文件已存在,我试图再次将 id_rsa 公钥添加到我的 GitHub 设置中,但是 GitHub 告诉我这个公钥已经被使用了。

$ ls
config      id_rsa      id_rsa.pub  known_hosts

这是我的.ssh/文件夹。

配置文件包含了多个用于不同 git 站点的 git 配置。它们都使用了相同的公钥,且能够成功工作。

我所做的:

$git init
$git add .
$git commit -m "first commit"
$git remote add [my remote git repository address]
$git push -u origin master
// ....  error message

更新:

$ git config core.sshCommand "ssh -v"
$ git push -u origin master
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data ~/.ssh/config
debug1: ~/.ssh/config line 2: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file id_rsa type -1
debug1: identity file id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.9
debug1: Remote protocol version 2.0, remote software version babeld-003ebee6
debug1: no match: babeld-003ebee6
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in ~/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: Will attempt key: id_rsa  explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: id_rsa
no such identity: id_rsa: No such file or directory
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

2
运行 git config core.sshCommand "ssh -v",然后执行 git push -u origin master 产生的新错误。 - Kevin
1
似乎您将私钥配置到了错误的路径。正确的路径应该像这样记录:“debug1:尝试私钥:/Users/lpp/.ssh/id_dsa”。 - Kevin
1
@Kevin 谢谢你的回答,我编辑了我的配置文件 ~/.ssh/config,将 IdentityFile id_rsa 编辑为 IdentityFile ~/.ssh/id_rsa,然后重新尝试了 git push -u origin master,现在它可以正常工作了。 - afraid.jpg
好的。我已将完整答案添加到社区维基中。 - Kevin
1
如果你和我一样,可能是个蠢蛋,忘记运行 ssh-add <options> <path/to/your/key> - Devin Rhode
3个回答

33
debug1: Will attempt key: id_rsa  explicit
...
debug1: Trying private key: id_rsa
no such identity: id_rsa: No such file or directory

你配置了错误的私钥路径。尝试像这样进行配置。

Host github.com
IdentityFile ~/.ssh/id_rsa

3
在哪里进行配置?哪个配置文件? - CGFoX
4
@CGFoX ~/.ssh/config - Kevin

12

过去2年中,IdentityFile id_rsa 对我来说一直有效。今天,它只接受绝对路径,例如IdentityFile ~/.ssh/id_rsa。很奇怪。


1
对我来说,问题在于引号和IdentityFile '~/.ssh/id_rsa' 或者IdentityFile '/home/user/.ssh/id_rsa' 不幸的是,需要删除引号。 - R. W. Prado

0

这是一个不同的原因,导致OP出现了这个错误,但我突然也开始遇到了这个错误。结果发现github实际上已经崩溃了。在深入研究你的ssh密钥有什么问题之前,先查看https://www.githubstatus.com/

第一次遇到这个问题时,页面显示一切正常。大约花了10分钟他们才更新状态,所以如果它说一切正常,请过几分钟再检查。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接