[TOC] #### 1. 问题描述 --- 使用 git clone 拉取 github 仓库时报错 ``` $ git clone git@github.com:shinyboys/test.git Cloning into 'test'... 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. ``` ``` Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hos ``` 当使用 git push 将本地代码推送到远程仓库时也会报错 ![](https://img.itqaq.com/art/content/df57553be856d0dab88c897d2ae6e21a.png) #### 2. 生成 SSH 公钥 --- 这个应该是很多 github 新手会遇到的问题,原因就是当前电脑没有权限操作 github 仓库。解决办法: 当前电脑生成 ssh 密钥文件,将 ssh 公钥添加到 github 账号上,然后就可以正常拉取、推送代码了 执行生成 ssh 密钥的命令, 会有交互性操作,提示让输入密码, 直接一路回车即可 ssh-keygen 用于生成、管理、转换认证秘钥 参数: -t,type 指定要创建的秘钥类型,可取值: rsa1(SSH-1)、rsa(SSH-2)、dsa(SSH-2) ``` ssh-keygen -t rsa ``` 查看生成的 SSH 公钥 ``` cat ~/.ssh/id_rsa.pub ``` ![](https://img.itqaq.com/art/content/bd724077a7b8197a6f7192127d7e4f39.jpg) mac 系统生成 ssh 公钥 ![](https://img.itqaq.com/art/content/fa2f0ab0c2697ca2a96cc2c418b3ab34.png) #### 3. 在 github 上添加 SSH 公钥 --- **前往个人中心设置 `Settings`** ![](https://img.itqaq.com/art/content/9e81e67acb8b0b3cb5b7830445dfd13c.jpg) **点击左侧栏目 `SSH and GPG keys`** ![](https://img.itqaq.com/art/content/9786b887c8dad15714858f750dc3786c.jpg) **将生成的 SSH 公钥保存到 github 中, 建议标题标明 SSH 公钥来源** ![](https://img.itqaq.com/art/content/65bcb53c98ca587e6cd42ef36df822dd.jpg) #### 4. 在 gitee 上添加 SSH 公钥 --- ![](https://img.itqaq.com/art/content/3ac8567b9c0a331ed468e62245338b86.png) #### 5. 重新拉取项目 --- ``` git clone git@github.com:shinyboys/test.git ``` ![](https://img.itqaq.com/art/content/46e38aac59871ec2ca1c84dd45e6d520.jpg)