CrazyAirhead

疯狂的傻瓜,傻瓜也疯狂——傻方能执著,疯狂才专注!

0%

免密登录GitHub

说明

本文算是对「管理多个Github帐号」的重新整理,在查看该文档重新配置GitHub免密登录时,发现不是很顺畅,于是按步骤的方式进行重新整理。而「管理多个Github账号」是对配置多个GitHub不成的问题分析。

本文基于macOS Monterey(v12.0.1)环境,不同版本可能会有所差异。

步骤

检查是否配置过sshkey

1
2
$ ls -al ~/.ssh
# ls: /Users/airhead/.ssh: No such file or directory

创建sshkey

1
2
3
4
$ ssh-keygen -t rsa -C "[email protected]"
# Generating public/private rsa key pair.
# Enter file in which to save the key (/Users/airhead/.ssh/id_rsa): /Users/airhead/.ssh/airhead-github/id_rsa
# 后续交互可以默认回车即可。

此处修改了默认的生成路径,方便后面管理不同的密钥。如无提示无效路径时,使用mkdir -p /Users/airhead/.ssh/airhead-github 创建对于目录。

创建成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Your identification has been saved in /Users/airhead/.ssh/airhead-github/id_rsa
Your public key has been saved in /Users/airhead/.ssh/airhead-github/id_rsa.pub
The key fingerprint is:
SHA256:hh935Euebatrb1ckBz8/17t2YRwCTtiPbjwaMSKWWRM [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| E. o |
| o . + . |
| + . o = o |
| = o o = o.o+|
| . o S * + o+=|
| o + O + ==|
| . + = + *|
| . ...+o|
| .o=+oo|
+----[SHA256]-----+

将公钥配置到GitHub

通过下面的语法复制公钥内容,并贴到Github上,SSH and GPG keys (github.com)

1
$ pbcopy < ~/.ssh/airhead-github/id_rsa.pub

添加~/.ssh/config

1
2
3
4
5
Host airhead-github
Hostname github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/airhead-github/id_rsa

如果管理多个GitHub账号时,需要对应增加Host节点配置。

测试

1
2
3
4
5
6
ssh -T airhead-github

# This key is not known by any other names
# Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
# Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
# Hi crazy-airhead! You've successfully authenticated, but GitHub does not provide shell access.

验证提交

此处使用Hexo的博客仓库进行测试。

  • 修改Hexo的配置_config.yml
1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: airhead-github:crazy-airhead/crazy-airhead.github.io.git
branch: master
  • 发布
1
hexo d -g

欢迎关注我的其它发布渠道