为 GitHub 设置 SSH 密钥

1. 起因

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

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 密钥
ssh-keygen -t ed25519 -C "your_email@example.com"

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

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
> 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/ 下应该看到如下文件:

image-20230715140759430

2.2 将 SSH 密钥添加到 ssh-agent
  1. 在后台启动 ssh-agent

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

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

    ssh-add ~/.ssh/id_ed25519
    
2.3 将 SSH 公钥添加到 GitHub 上的帐户
  1. 将 SSH 公钥复制到剪贴板

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

    截屏2023-07-15 14.16.31
  3. 打开左侧的 SSH and GPG keys

    截屏2023-07-15 14.17.16
  4. New SSH key

    image-20230715141846805

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

2.4 恭喜🎉,快来测试下吧
$ ssh -T git@github.com
> Hi Kudoryafuka3! You've successfully authenticated, but GitHub does not provide shell access.

3. 参考

  • https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  • https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Linux上配置GitHubSSH密钥,您可以按照以下步骤进行操作: 1. 首先,建议您设置一个密码以增加安全性。您可以使用以下命令复制公钥文件的内容:`cat ~/.ssh/id_rsa.pub`。\[1\] 2. 在GitHub上,打开"Settings",然后选择"SSH and GPG keys",点击"New SSH key"。 3. 在"Key"文本框中粘贴之前复制的公钥内容,并设置一个标题,然后点击"Add SSH key"。\[1\] 4. 您可以通过运行以下命令来检查SSH连接是否成功:`ssh -T git@github.com`。如果您看到类似于"Hi XXXXXX! You've successfully authenticated, but GitHub does not provide shell access."的消息,说明连接成功。\[2\] 如果您在配置过程中没有打印出name和email,这意味着您还没有进行相应的配置。您可以使用以下命令进行配置: - 配置用户名:`git config --global user.name "<userName>"` - 配置邮箱:`git config --global user.email "<email>"`\[3\] 另外,如果您还没有生成SSH密钥,您可以使用以下命令生成:`ssh-keygen -t rsa -C "<email>"`。\[3\] 这样,您就可以在Linux上成功配置GitHubSSH密钥了。 #### 引用[.reference_title] - *1* [Github使用ssh密钥登陆](https://blog.csdn.net/weixin_39591031/article/details/121368476)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [解决github配置ssh密钥后仍然需要登录](https://blog.csdn.net/weixin_44639164/article/details/122389112)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Github 配置ssh密钥](https://blog.csdn.net/qq_25473795/article/details/128245065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值