Hexo使用笔记

本文最后更新于 2025年1月22日 下午

常用命令

代码中[]表示可以用[]里的东西替换[]前面的单词

创建新文章命令:

1
$ hexo new "文章名"

部署博客:

1
$ hexo deploy [d]

在部署之前要先清除再生成静态文件:

1
2
$ hexo clear
$ hexo generate [g]

在正式部署到github上之前我们可以先用 hexo s 命令看看效果怎么样

目前我用到的就是上面这些,部署博客相关的命令是可以用下面这种更简洁:

1
$ hexo clear & deploy

网络连接问题的解决

我在用hexo d命令经常会出现下面的错误:

1
2
3
4
5
6
7
fatal: unable to access 'https://github.com/Qingyun0118/Qingyun0118.github.io.git/':  Failed to connect to github.com port 443 after 21097 ms: Could not connect to server
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (D:\fileForWhile\MyBlog\node_modules\.store\hexo-util@2.7.0\node_modules\hexo-util\lib\spawn.js:51:21)
at ChildProcess.emit (node:events:519:28)
at cp.emit (D:\fileForWhile\MyBlog\node_modules\.store\cross-spawn@7.0.3\node_modules\cross-spawn\lib\enoent.js:34:29)
at ChildProcess._handle.onexit (node:internal/child_process:294:12)

看起来像是网络问题,但我尝试用vpn也没有成功,使用下面的命令检查了一下:

1
curl -I https://github.com

输出:

1
2
$ curl -I https://github.com 
curl: (28) Failed to connect to github.com port 443 after 21072 ms: Could not connect to server

无法通过 HTTPS(端口 443)访问 GitHub

那我直接换ssh部署:

以下是使用 SSH 部署 Hexo 博客到 GitHub 的详细步骤:


1. 生成 SSH 密钥

如果你还没有 SSH 密钥,需要先生成一个。打开 Git Bash 或终端,运行以下命令:

bash复制

1
ssh-keygen -t rsa -C "your_email@example.com"
  • 按提示操作,直接回车即可(可选设置密码,但不设置密码可以免密登录)。
  • 生成的密钥默认保存在 ~/.ssh/id_rsa~/.ssh/id_rsa.pub 文件中。

2. 将 SSH 密钥添加到 GitHub

  1. 打开 ~/.ssh/id_rsa.pub 文件,复制其中的内容。
  2. 登录 GitHub,进入 Settings > SSH and GPG keys
  3. 点击 New SSH key,输入一个标题(如 My Laptop),然后将公钥粘贴到 Key 框中,点击 Add SSH key

3. 测试 SSH 连接

在终端中运行以下命令测试连接:

bash复制

1
ssh -T git@github.com

如果连接成功,你会看到类似以下的提示:

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

如果连接失败,可能需要检查密钥是否正确添加。

连接失败情况:

1
2
$ ssh -T git@github.com
ssh: connect to host github.com port 22: Connection timed out

尝试换一个端口:

1
ssh -T -p 443 git@ssh.github.com

输出:

1
2
3
4
5
6
7
8
$ ssh -T -p 443 git@ssh.github.com
The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:15: github.com
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.
Hi Qingyun0118! You've successfully authenticated, but GitHub does not provide shell access.

输出来看,已经成功通过端口 443 使用 SSH 连接到 GitHub,并且认证通过。这说明你的网络环境支持通过端口 443 访问 GitHub,但默认的端口 22 被限制或不可用。

接下来,需要确保在部署 Hexo 时也使用端口 443 的 SSH 配置。以下是修改 Hexo 配置和 SSH 配置的详细步骤:


3.1. 修改 SSH 配置

为了确保每次连接 GitHub 都使用端口 443,你需要在 SSH 配置文件中添加或更新相关设置。

  1. 打开或创建 ~/.ssh/config 文件:

    bash复制

    1
    nano ~/.ssh/config
  2. 添加以下内容:

    plaintext复制

    1
    2
    3
    4
    5
    Host github.com
    Hostname ssh.github.com
    Port 443
    User git
    IdentityFile ~/.ssh/id_rsa

    这样,当你尝试通过 SSH 连接到 github.com 时,SSH 客户端会自动使用 ssh.github.com 的 443 端口。


3.2. 验证 SSH 配置

运行以下命令验证 SSH 配置是否生效:

bash复制

1
ssh -T git@github.com

如果配置正确,你应该看到类似以下的输出:

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

4. 修改 Hexo 配置文件

打开 Hexo 项目的 _config.yml 文件,找到 deploy 部分,修改为以下内容:

yaml复制

1
2
3
4
deploy:
type: git
repo: git@github.com:username/username.github.io.git
branch: master
  • username 替换为你的 GitHub 用户名。

5. 清理并重新部署

在终端中运行以下命令清理缓存并重新部署:

bash复制

1
2
3
hexo clean
hexo generate
hexo deploy

如果部署成功,你的博客将被推送到 GitHub。


6. 解决网络问题(可选)

如果你仍然遇到网络连接问题,可以尝试以下方法:

  1. 修改 SSH 配置:在 ~/.ssh/config 文件中添加以下内容:

    plaintext复制

    1
    2
    3
    Host github.com
    Hostname ssh.github.com
    Port 443

    这会将 SSH 连接的端口从默认的 22 改为 443。

  2. 修改 Hosts 文件:在 Windows 的 C:\Windows\System32\drivers\etc\hosts 文件中添加 GitHub 的域名映射。


Hexo使用笔记
https://qingyun0118.github.io/2025/01/11/Hexo使用笔记/
作者
Wei Jicheng
发布于
2025年1月11日
许可协议