将hexo部署到github上遇到的一些问题

将hexo博客deploy到github时出现 git@github.com:Permission denied (publickey).

问题描述

执行 sudo hexo d 命令将hexo部署到github的时候遇到如下报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
INFO  Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
1 file changed, 1 insertion(+), 1 deletion(-)
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.
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (/home/users/blog/node_modules/_hexo-util@1.5.0@hexo-util/lib/spawn.js:51:21)
at ChildProcess.emit (events.js:210:5)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)

分析与解决

_config.yml 设置错误

一般是将github的地址填错,正确填写应该为:

1
2
3
4
deploy:
type: git
repo: git@github.com:用户名/项目名.git
branch: master

假设用户名为users,项目名为 users.github.io ,即为:

1
repo: git@github.com:users/users.github.io.git

要特别注意冒号后的空格。

ssh设置出错

使用命令:ssh -T git@github.com如果报错:Permission denied (publickey).
那么 重新生成ssh秘钥文件并将其加入github

ssh -T git@github.com未报错

分析:
  在非 root 用户下安装使用hexo,在后期使用过程中因为加入某些些文件后在执行 hexo clean 等命令时出现permission denied的情况,需要加sudo才可执行。问题就出现在使用 hexo d 命令使用 sudo 命令执行时,由于.ssh也是安装在非root里面的,root用户并没有ssh的秘钥,故执行 sudo hexo d 命令时报错。
解决方法:
  使用 sudo chmod -R 777 filename 修改在hexo下生成的blog文件的读取权限,然后使用 hexo d 即可将部署到 github 而不需要加 sudo 指令。最根本的解决方案是在 root 下重新安装 hexo ,所有操作均在 root 下进行。

参考

关于Hexo部署文章时出现的问题-SSH
关于git push没有权限的问题,已设置公钥