为 GitHub 设置 SSH 密钥

1. 起因

给自己的 github 改个名,顺便就给原来 Hexo 对应的仓库也改了个名。然后发现 ub='hexo clean && hexo generate && hexo deploy' 失败了,报错如下:

sh 复制代码
INFO  Deploying: git
INFO  Clearing .deploy_git folder...
INFO  Copying files from public folder...
INFO  Copying files from extend dirs...
[master 397fba4] Site updated: 2023-07-15 13:57:29
 173 files changed, 728 insertions(+), 559 deletions(-)
 create mode 100644 2023/07/15/Setting-Up-SSH-Keys-for-GitHub/index.html
/Users/ritsu/.ssh/config line 4: no argument after keyword "k%u7h@j2s!wa"
/Users/ritsu/.ssh/config: terminating, 1 bad configuration options
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
FATAL {
  err: Error: Spawn failed
      at ChildProcess.<anonymous> (/Users/ritsu/Study/Hexo/node_modules/hexo-util/lib/spawn.js:51:21)
      at ChildProcess.emit (node:events:512:28)
      at ChildProcess._handle.onexit (node:internal/child_process:293:12) {
    code: 128
  }
} Something's wrong. Maybe you can find the solution here: %s https://hexo.io/docs/troubleshooting.html

划重点:Please make sure you have the correct access rights and the repository exists.

好像我的 ssh 配置出问题了,想来那就重新配一遍吧,顺便记录下。

2. 配置过程

2.1 生成 SSH 密钥

sh 复制代码
ssh-keygen -t ed25519 -C "your_email@example.com"

**注意:**如果您使用的是不支持Ed25519算法的遗留系统,请使用:

shell 复制代码
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
sh 复制代码
> Enter a file in which to save the key (/Users/YOU/.ssh/id_ALGORITHM): [Press enter]
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]å

这时候在 ~/.ssh/ 下应该看到如下文件:

2.2 将 SSH 密钥添加到 ssh-agent

  1. 在后台启动 ssh-agent

    shell 复制代码
    $ eval "$(ssh-agent -s)"
    > Agent pid 51024
  2. 打开文件 ~/.ssh/config ,然后修改文件以包含以下行

    config 复制代码
    Host github.com
      AddKeysToAgent yes
      IdentityFile ~/.ssh/id_ed25519
  3. 将 SSH 密钥添加到 ssh-agent

    sh 复制代码
    ssh-add ~/.ssh/id_ed25519

2.3 将 SSH 公钥添加到 GitHub 上的帐户

  1. 将 SSH 公钥复制到剪贴板

    sh 复制代码
    $ pbcopy < ~/.ssh/id_ed25519.pub
  2. 在 GitHub 任何页面的右上角,点击头像,然后点击设置

  3. 打开左侧的 SSH and GPG keys

  4. New SSH key

    起个好听的名字,然后把第一步中复制的公钥粘贴进去

2.4 恭喜🎉,快来测试下吧

sh 复制代码
$ ssh -T git@github.com
> Hi Kudoryafuka3! You've successfully authenticated, but GitHub does not provide shell access.

3. 参考

相关推荐
likfishdn11 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean31 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=32 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
崔lc4 小时前
linux中安装部署Jenkins,成功构建springboot项目详细教程
运维·jenkins
luojiaao4 小时前
【Jenkins】个人向-Jenkinsfile如何写
运维·jenkins
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
wanhengidc8 小时前
单线服务器和双线服务器分别是指什么?
运维·服务器