前提环境:node.js git

1.hexo安装

1
2
3
4
5
6
mkdir MyBlog
cd MyBlog
npm install hexo-cli -g
hexo init
npm install
hexo server

这个时候应该在console会显示:

Hexo is running at http://0.0.0.0:4000/.

这个时候直接访问该地址,即部署到本地的博客:

本地hexo博客

2.公钥和私钥生成及其配置

进入~/.ssh目录,目录中可能已经存在其他公钥和私钥对,现在要专门添加一个新的公私钥对。

1
2
//以指定名称生成公私钥对
ssh-keygen -t rsa -C "B" -f B

然后将公钥添加到github账号下

这个时候,.ssh目录下应该有多对公私钥,现在查看密钥列表

1
ssh-add -l

密钥列表里面应该没有刚才新建的密钥,现在将它添加进去

1
ssh-add ~/.ssh/B

然后再使用ssh-add -l 发现已经添加了,A是我之前添加的。

1
2
2048 ac:51:8d:fc:1e:6f:07:ca:3b:3d:c7:0b:79:ee:a1:27 A (RSA)
2048 73:81:8f:03:04:7e:56:a0:cf:22:98:52:47:12:ee:3b /Users/user/.ssh/B (RSA)

现在.ssh目录中就了2对连接github.com的公私钥对,现在通过.ssh下config文件去配置host,在不同的情况下通过对应的公私钥对连接github,配置如下:

1
2
3
4
5
6
7
8
Host github
HostName github.com
User git
IdentityFile ~/.ssh/A
Host myBlog
HostName github.com
User git
IdentityFile ~/.ssh/B

然后通过如下方式,确认以私钥B连接到github

1
ssh -T git@myBlog

输出:

Hi yourName! You’ve successfully authenticated, but GitHub does not provide shell access.

说明已经成功

3.在github上新建repo,以yourName.github.io方式命名(必须),youName是github上的账号名称。

4.配置hexo部署到github

1
vim _config.yml
1
2
3
4
deploy:
type: git
repository: git@myBlog:yourName/yourName.github.io.git
branch: master

hexo配置文件的语法是冒号后面必须有空格,否则在之后部署的时候会报错。

然后执行如下命令,安装git部署的工具:

1
npm install hexo-deployer-git --save

最后生成静态文件并部署:

1
hexo g -d

访问yourName.github.io可以查看最后部署到github上的效果

问题解答:

1.执行hexo g -d的时候,遇到:

Error: Permission denied(publickey).

fatal: Could not read from remote repository

解决办法:

1.先看一下.ssh目录下的配置文件config

cd ~/.ssh

vim config

看一下host 别名配置的是什么,比如

Host github

​ HostName github.com

​ user git

​ IdentityFile ~/.ssh/zhengzhiwenBlog

host的别名就是github

通过ssh -T git@github 看一下是否能够连接,如果不能连接,说明github账号下没有配置公钥,先去github上配置公钥

2.确定可以通过ssh连接github之后:

查看一下系统ssh-key的代理:

ssh-add -l

如果出现 The agent has no identities。说明没有代理,需要添加,按上文中提到的方式:

ssh-add ~/.ssh/zhengzhiwenBlog

得到提示:

Identity added: /Users/zhengzhiwen/.ssh/moma (/Users/zhengzhiwen/.ssh/zhengzhiwenBlog)

说明添加成功

再试试hexo g -d命令,进行部署。