今天在ubuntu下安装部署Hexo Blog的时候,遇到一个不能Deploy到github的错误:

1
2
3
4
5
6
7
8
9
10

git@github.com: Permission denied (publickey).
fatal: 无法读取远程仓库。

请确认您有正确的访问权限并且仓库存在。
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: git@github.com: Permission denied (publickey).
fatal: 无法读取远程仓库。

请确认您有正确的访问权限并且仓库存在。

搜索了一阵子,发现可以清理key-pari的教程。教程如下。

重置当前电脑的key-pair

  1. 清除所有的key-pair
  • ssh-add -D
  • rm -r ~/.ssh
  1. 删除github中的public-key

  2. 重新生成ssh密钥对

    1
    2
    3
    ssh-keygen -t rsa -C "youremail@email.com"
    chmod 0700 ~/.ssh
    chmod 0600 ~/.ssh/id_rsa*

你可以用通过命令ssh-add -l 查看当前key agent的key list

如果生成key的时候选择了自定义的key名字,可以使用下面的命令添加到key agent里面:
ssh-add ~/.ssh/YOUR-PUBLIC-KEY-NAME

  1. 在Github添加公钥public-key

在终端运行 xclip -sel ~/.ssh/id_rsa.pub 把公钥内容复制到剪贴板
然后在github上添加公钥

  1. 校验是否能够连同Github
    ssh -T git@github.com

正常的话,github会返回你的登陆用户名:

1
2

Hi USER-NAME! You've successfully authenticated, but GitHub does not provide shell access.

解决Permission issue

我发现我错误的原因是,先用这招来生成 id_rsa.pub

ssh-keygen -t rsa -b 4096 -C "some@email.com"

告诉我没有权限,于是我加了sudo 操作,结果文件生成到了根目录而不是家目录下:

/var/root/.ssh/id_rsa/var/root/.ssh/id_rsa.pub

正确的做法是把根目录下的复制到家目录下:

1
2
sudo cp /var/root/.ssh/id_rsa /Users/michael/.ssh/id_rsa
sudo cp /var/root/.ssh/id_rsa.pub /Users/michael/.ssh/id_rsa.pub

然后测试:
ssh -T git@github.com

Hi michael! You've successfully authenticated, but GitHub does not provide shell access.

知乎链接

参考资源

Adding a new SSH key to your GitHub account

Generating a new SSH key and adding it to the ssh-agent