git 添加ssh公钥
解决git@github.com: Permission denied (publickey). Could not read from remote repository
原因分析
Permission denied(publickey)一般有两种原因。
- 客户端与服务端未生成 ssh key
- 客户端与服务端的ssh key不匹配
1. 生成ssh key
ssh-keygen -t rsa -C "xxxx@xxx.com"
# 注意ssh和-keygen之间没有空格
# 之后需要输入密码等,不管一路回车即可。
# 默认的就会在C/User/CurrentUser/.ssh/id_rsa下面生成 id_rsa 和 id_rsa.pub两个文件。
2.测试登录
ssh -v git@github.com
最后会出现
debug1: No more authentication methods to try.
git@github.com: Permission denied(publickey).
3.终端执行
ssh-agent -s
# SSH_AUTH_SOCK=/tmp/ssh-csx2das143s/agent.1974; export SSH_AUTH_SOCK;
# SSH_AGENT_PID=1975; export SSH_AGENT_PID;
# echo Agent pid 1975;
4.终端执行
ssh-add ~/.ssh/id_rsa
# Could not open a connection to your authentication agent.
5.终端执行
ssh-agent bash
6.终端执行
ssh-add ~/.ssh/id_rsa
# Identity added: /C/User/CurrentUser/.ssh/id_rsa (xxxx@xxx.com)
7.配置 github端
- 点头像,进入Personal settings/SSH and GPG keys
- 点击 New SSH key
- title 随便起
- 内容是 /C/User/CurrentUser/.ssh/id_rsa下的id_rsa.pub的内容。
8.验证Key
ssh -T git@github.com
#Hi sweatma! You've successfully authenticated, but GitHub does not provide shell access.