MAC M1 配置 Git SSH

背景

换了新笔记本,本地想要克隆github 上的项目需要配置ssh 公钥到自己的github账户中,否则使用ssh 地址克隆会报错,如下。

bash 复制代码
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

操作

1. 生成ssh key

在 ~./ssh 目录中是没有已创建好的ssh key的,所以这里需要生成一下。
生成 ssh key

bash 复制代码
ssh-keygen -t ed25519 -C "这里是你的github注册的邮箱"

要求输入生成ssh key 的路径

2. 将生成的公钥添加到github账户中

找到settings 中的 SSH and GPG keys。

新建 SSH key,通过记事本打开生成的xxx.pub 公钥文件,将内容拷贝到新建 SSH key当中。

测试结果

失败继续排查

再次使用git clone 命令克隆项目,,,依然报错。

bash 复制代码
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

检查生成的公钥和github中配置的公钥没有问题。

查看github 官方文档帮助

https://docs.github.com/cn/authentication/troubleshooting-ssh/error-permission-denied-publickey

根据文档提示我们一步步排查

需要配置git config 全局邮箱和账号名称

通过命令查找是否配置邮箱、账号

bash 复制代码
git config --global --list

通过 git config 命令配置自己的github账号名和邮箱到git 全局config中。

bash 复制代码
git config --global  user.name "xxxxx"
bash 复制代码
git config --global  user.email "xxxxx"

配置完成后再次尝试 克隆项目,发现依然失败,继续查阅帮助文档排查。

通过命令验证连接

bash 复制代码
ssh -T [email protected]
bash 复制代码
[email protected]: Permission denied (publickey).

连接依然被拒,继续排查。

确保私钥已经加载到SSH中

通过如下命令检查是否有刚刚生成的ssh key

bash 复制代码
ssh-add -l -E sha256

笔者这里发现没有自己生成的key,根据帮助文档进行手动添加。
注意:在大多数系统中,默认私钥(~/.ssh/id_rsa 和 ~/.ssh/identity)会自动添加到 SSH 身份验证代理中。 应无需运行 ssh-add path/to/key,除非在生成密钥时覆盖文件名。

通过命令添加 ssh key 到本机ssh服务

ssh-add [私钥路径]

bash 复制代码
ssh-add /Users/xxxxx/.ssh/xxxxx

再次测试连接,连接成功。

bash 复制代码
ssh -T [email protected]
xxxxxxxxxx You've successfully authenticated, but GitHub does not provide shell access.

再次 git clone ,项目克隆成功。

可能遇到的其他问题

如果.ssh路径中生成的私钥公钥文件的读写权限不够也会导致,连接失败。

可以使用 sudo chmod -R 777对权限进行修改。

总结

折腾了一个小时多,最终查阅git官方帮助文档帮我从坑中爬了出来。总结一下处理过程中的要点。

  1. 生成ssh key,将公钥配置到github账户中。
  2. 本地的git config --global 配置邮箱要和生成的ssh key 指定的邮箱一致。
  3. 确保ssh key 是否添加到了本机ssh服务中。
  4. 生成的公钥私钥文件权限要可以读写。
相关推荐
ITKEY_2 小时前
archlinux中挂载macOS的硬盘
macos·archlinux
寧笙(Lycode)2 小时前
React系列——nvm、node、npm、yarn(MAC)
react.js·macos·npm
AI风老师6 小时前
2、ubuntu系统配置OpenSSH | 使用vscode或pycharm远程连接
linux·运维·服务器·ssh
仍然探索未知中15 小时前
Git分支管理
git
xmweisi0215 小时前
【Linux】ssh命令 – 安全的远程连接服务
linux·安全·ssh
小妖66615 小时前
windows11 安装好后右键没有 git bash 命令
git
只做开心事16 小时前
Git 多人协作
git
freejackman16 小时前
Git从入门到精通
git·gitee·gitlab·github
小草cys18 小时前
Mac 在恢复模式下出现 旋转地球图标 但进度非常缓慢
macos
兔子坨坨19 小时前
pycharm连接github(详细步骤)
windows·git·学习·pycharm·github