Ubuntu 系统下多 GitHub 账号管理 Hexo 博客并部署到 GitHub Pages

Ubuntu 系统下多 GitHub 账号管理 Hexo 博客并部署到 GitHub Pages

不再孤独ing MVP++

Ubuntu 系统下多 GitHub 账号管理 Hexo 博客并部署到 GitHub Pages

本文详细讲解在 Ubuntu 系统中,如何配置两个不同的 GitHub 账号,分别管理独立的 Hexo 博客文件夹,并通过 SSH Key 认证将博客源码推送到对应账号的 main 分支、静态页面推送到 gh-pages 分支的完整流程。

一、前置条件

  1. Ubuntu 系统已安装 Git、Node.js(Hexo 运行依赖)

  2. 两个 GitHub 账号(以下简称「账号 A」「账号 B」)

  3. 已从账号 A 克隆 Hexo 博客文件到本地,且本地有两个独立的 Hexo 博客文件夹(如 blog-account-Ablog-account-B

    二、核心原理

    通过 SSH 配置文件区分不同 GitHub 账号 + Git 仓库级别的 user 配置,解决多账号认证冲突;利用 Hexo 自带的部署工具,分别将不同博客推送到对应账号的 main(源码)和 gh-pages(页面)分支。


三、步骤 1:生成并配置多账号 SSH Key

3.1 生成两个账号的 SSH Key

Ubuntu 终端执行以下命令,分别为两个 GitHub 账号生成 SSH Key(替换邮箱为对应账号的邮箱):

1
2
3
4
# 生成账号 A 的 SSH Key(文件名区分:id_rsa_accountA)
ssh-keygen -t ed25519 -C "your-email-for-accountA@xxx.com" -f ~/.ssh/id_rsa_accountA
# 生成账号 B 的 SSH Key(文件名区分:id_rsa_accountB)
ssh-keygen -t ed25519 -C "your-email-for-accountB@xxx.com" -f ~/.ssh/id_rsa_accountB
  • 执行时按回车跳过密码设置(也可设置密码,后续需输入);

  • 生成后,~/.ssh 目录下会出现 id_rsa_accountA(私钥)、id_rsa_accountA.pub(公钥)、id_rsa_accountBid_rsa_accountB.pub 四个文件。

    3.2 将公钥添加到对应 GitHub 账号

  1. 查看账号 A 的公钥内容:

    1
    cat ~/.ssh/id_rsa_accountA.pub

    复制输出的全部内容(以 ssh-ed25519 开头,邮箱结尾)。

  2. 登录 GitHub 账号 A → 点击右上角头像 → SettingsSSH and GPG keysNew SSH key

    • Title:自定义(如 Ubuntu-blog-A);
    • Key type:Authentication Key
    • Key:粘贴复制的公钥内容 → 点击 Add SSH key
  3. 重复上述步骤,将 id_rsa_accountB.pub 的内容添加到 GitHub 账号 B。

    3.3 配置 SSH 配置文件(核心:区分账号)

  4. 创建/编辑 SSH 配置文件:

    1
    nano ~/.ssh/config
  5. 粘贴以下内容(替换注释中的信息):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 配置账号 A 的 SSH 规则(自定义 Host 名:github-accountA)
    Host github-accountA
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_accountA
    IdentitiesOnly yes
    # 配置账号 B 的 SSH 规则(自定义 Host 名:github-accountB)
    Host github-accountB
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_accountB
    IdentitiesOnly yes
    • Host:自定义别名(后续仓库地址用这个别名代替 github.com);
    • IdentityFile:指定对应账号的私钥路径;
    • IdentitiesOnly yes:强制使用指定的私钥,避免认证冲突。
  6. 保存退出(Ctrl+O → 回车 → Ctrl+X)。

    3.4 测试 SSH 连接

    分别测试两个账号的 SSH 连通性:

    1
    2
    3
    4
    # 测试账号 A
    ssh -T git@github-accountA
    # 测试账号 B
    ssh -T git@github-accountB
  • 首次连接会提示 Are you sure you want to continue connecting (yes/no/[fingerprint])?,输入 yes
  • 若输出 Hi [你的账号A用户名]! You've successfully authenticated...,说明配置成功。

四、步骤 2:配置 Hexo 博客仓库(区分账号)

4.1 初始化/克隆博客仓库(以账号 A 为例,账号 B 同理)

场景 1:已克隆账号 A 的 Hexo 仓库(你的情况)

1
2
3
4
5
6
7
8
# 进入账号 A 的博客文件夹
cd ~/path/to/blog-account-A
# 重置仓库远程地址(替换为账号 A 的 SSH 别名)
# 格式:git@自定义Host名:GitHub用户名/仓库名.git
git remote set-url origin git@github-accountA:your-accountA-username/your-blog-repo-name.git
# 验证远程地址是否正确
git remote -v
# 正确输出:origin git@github-accountA:xxx/xxx.git (fetch/push)

场景 2:新建账号 B 的 Hexo 仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
# 1. 新建博客文件夹并初始化 Hexo
mkdir ~/path/to/blog-account-B
cd ~/path/to/blog-account-B
npm init -y
npm install hexo-cli --save
hexo init .
npm install
# 2. 关联到账号 B 的 GitHub 仓库(先在账号 B 新建空仓库)
git init
git add .
git commit -m "init hexo blog"
git remote add origin git@github-accountB:your-accountB-username/your-blog-repo-name.git
git push -u origin main

4.2 配置仓库级别的 Git 用户信息(避免提交者信息混乱)

每个博客仓库单独设置 Git 用户名/邮箱(对应 GitHub 账号):

1
2
3
4
5
6
7
8
# 配置账号 A 的博客仓库
cd ~/path/to/blog-account-A
git config user.name "your-accountA-username"
git config user.email "your-email-for-accountA@xxx.com"
# 配置账号 B 的博客仓库
cd ~/path/to/blog-account-B
git config user.name "your-accountB-username"
git config user.email "your-email-for-accountB@xxx.com"
  • 注意:不要使用 git config --global(全局配置),否则会覆盖所有仓库的用户信息。

五、步骤 3:配置 Hexo 部署(推送到 gh-pages 分支)

Hexo 需安装 hexo-deployer-git 插件,实现一键将静态页面推送到 gh-pages 分支,源码仍手动推送到 main 分支。

5.1 安装部署插件

在每个博客文件夹下执行:

1
2
3
4
5
6
# 进入账号 A 的博客文件夹
cd ~/path/to/blog-account-A
npm install hexo-deployer-git --save
# 进入账号 B 的博客文件夹
cd ~/path/to/blog-account-B
npm install hexo-deployer-git --save

5.2 修改 Hexo 配置文件(_config.yml)

编辑每个博客文件夹下的 _config.yml 文件(重点修改 deploy 部分):

1
2
3
4
5
6
7
8
9
10
11
12
# 账号 A 的 _config.yml 部署配置
deploy:
type: git
repo: git@github-accountA:your-accountA-username/your-blog-repo-name.git
branch: gh-pages # 静态页面推送到 gh-pages 分支
message: "Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }}"
# 账号 B 的 _config.yml 部署配置
deploy:
type: git
repo: git@github-accountB:your-accountB-username/your-blog-repo-name.git
branch: gh-pages
message: "Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }}"
  • repo:必须使用 SSH 别名配置的地址(git@github-accountA:xxx/xxx.git);
  • branch:固定为 gh-pages(GitHub Pages 读取该分支)。

六、步骤 4:日常使用流程(新增博客并推送)

以账号 A 为例,账号 B 操作完全相同,仅需切换文件夹:

6.1 新增博客并生成静态文件

1
2
3
4
5
6
7
8
# 进入账号 A 的博客文件夹
cd ~/path/to/blog-account-A
# 新建博客文章(标题自定义)
hexo new "new-blog-title"
# 编辑文章(用 vim/nano 或编辑器打开 source/_posts/new-blog-title.md)
nano source/_posts/new-blog-title.md
# 生成静态页面(输出到 public 目录)
hexo clean && hexo generate # 简写:hexo cl && hexo g

6.2 推送源码到 main 分支

1
2
3
4
5
6
7
8
# 进入账号 A 的博客文件夹
cd ~/path/to/blog-account-A
# 添加所有修改
git add .
# 提交修改
git commit -m "add new blog: new-blog-title"
# 推送到账号 A 的 main 分支
git push origin main

6.3 推送静态页面到 gh-pages 分支

1
2
3
4
# 进入账号 A 的博客文件夹
cd ~/path/to/blog-account-A
# Hexo 一键部署到 gh-pages 分支
hexo deploy # 简写:hexo d

6.4 开启 GitHub Pages(首次部署需配置)

登录对应 GitHub 账号 → 进入博客仓库 → SettingsPages

  • Build and deploymentSource:选择 Deploy from a branch
  • Branch:选择 gh-pages/(root) → 点击 Save
  • 等待 1-2 分钟,页面会显示 Your site is live at https://xxx.github.io/xxx/

七、常见问题解决

  1. SSH 连接报错:Permission denied (publickey)
    • 检查 ~/.ssh 目录权限:chmod 700 ~/.ssh
    • 检查私钥文件权限:chmod 600 ~/.ssh/id_rsa_accountA
    • 重启 SSH 代理:eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa_accountA
  2. Hexo 部署时报错:fatal: could not read Username for ‘https://github.com
    • 确认 _config.ymlrepo 是 SSH 地址(不是 HTTPS 地址);
    • 检查 SSH 别名配置是否正确。
  3. GitHub Pages 页面 404
    • 确认 gh-pages 分支已推送成功;
    • 检查仓库设置中 Pages 的分支是否选对 gh-pages
    • 等待 5-10 分钟(GitHub Pages 部署有延迟)。

总结

  1. 核心配置:通过 ~/.ssh/config 区分多账号 SSH Key,避免认证冲突;
  2. 仓库隔离:每个博客文件夹单独配置 Git 用户信息和 Hexo 部署地址;
  3. 推送规则:源码手动推送到 main 分支,Hexo 静态页面通过 hexo d 推送到 gh-pages 分支。
    按上述步骤操作后,你可以在 Ubuntu 系统中独立管理两个 GitHub 账号的 Hexo 博客,无需每次切换账号或重新配置 SSH,实现一键推送源码和页面。

thumbnail:(此处粘贴的时候不留空格,粘贴后手动敲个空格即可) ../images/Ubuntu-系统下多-GitHub-账号管理-Hexo-博客并部署到-GitHub-Pages/cd137bd917c6816a56ad90c94081f76215de8d79.jpeg

缩略图的问题就是把链接后点个空格就可以,不然的话退出就会把链接地址删除,不明所以但是可用。

Comments