ssh远程提交或者远程连接git仓库报错问题(no such identity: /Users/xxx/.ssh/example_rsa: No such file or directory)

问题说明不存在ssh密钥example_rsa
解决方法:

(1)检查本地ssh有哪些
cd ~/.ssh
ls -al

结果会类似如下:

常见的 SSH 密钥文件名包括:

  • id_rsaid_rsa.pub(RSA 密钥)

  • id_ecdsaid_ecdsa.pub

  • id_ed25519id_ed25519.pub

.pub 是公钥,另一个是私钥。

(2) 查看SSH 配置文件
cat ~/.ssh/config

例如: 

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github

这个配置表示:访问 github.com 时,使用 ~/.ssh/id_rsa_github 这个密钥。

你甚至可以为多个仓库配置多个 Host:

Host github-company
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_company

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
你是否把密钥添加到了 ssh-agent

如果你的私钥需要密码保护(passphrase),建议添加到 ssh-agent,否则 VS Code 可能无法正常推送。

查看当前 ssh-agent 中的密钥:

ssh-add -l
如果没有,可以添加:
ssh-add ~/.ssh/id_rsa
(3)根据报错信息检查ssh是否对应
例如报错:no such identity: /Users/xxx/.ssh/example_rsa: No such file or directory

显示ssh密钥是example_rsa
即配置文件连接git仓库的密钥是example_rsa
IdentityFile ~/.ssh/example_rsa
这是检查git仓库设置的密钥是哪个,不是对应的,这时就需要修改配置文件


nano ~/.ssh/config
使用 nano 编辑器的话:
按 Control + O 保存文件(它会提示你确认文件名,按回车)
然后按 Control + X 退出编辑器

例如查看gitlab的ssh配置

(4)检查是否成功
在项目里可以运行以下命令测试是否用的是 id_rsa:
ssh -v [email protected](填写自己的信息和git仓库地址)
你应该会看到如下内容:
debug1: Offering public key: /Users/xxx/.ssh/id_rsa
debug1: Authentication succeeded (publickey).

表示连接成功,且用了 id_rsa