抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

基于 Github/gen-ssh 官方文档总结

一、生成 SSH 密钥

1. 生成密钥

your_email@example.com 替换为你的邮箱

1
2
3
4
ssh-keygen -t ed25519 -C "your_email@example.com"

# 若系统不支持 ed25519 算法,也可以使用 rsa
# ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

2. 设置密钥位置

默认会提示下方内容,回车会生成到默认目录,强烈建议自定义名称,避免后续生成给覆盖掉

建议改成例如 /Users/YOU/.ssh/id_ed25519_github_your_email 位置

1
> Enter a file in which to save the key (/Users/YOU/.ssh/id_ed25519): [Press enter]

3. 设置密钥密码

根据安全程度自行选择是否设置密钥密码,若不设置可直接按两下回车

4. 确认密钥

执行命令查看密钥文件,应该能看到你的密钥文件和公钥

1
2
3
ls ~/.ssh
- id_ed25519_github_your_email
- id_ed25519_github_your_email.pub

5. 设置密钥配置

编辑该文件

1
nano ~/.ssh/config

并设置 Github.com 指向你的密钥文件

1
2
3
4
5
6
7

Host github.com
AddKeysToAgent yes
UseKeychain yes # 若密钥没有密码,可删掉此行
IdentityFile ~/.ssh/id_ed25519_github_your_email # 修改为你的密钥位置


二、添加 SSH 密钥到 Github

1. 复制公钥

输入命令查看公钥内容并复制

1
2
3
cat ~/.ssh/id_ed25519_github_your_email.pub

ssh-ed25519 AA************Jif your_email@example.com

2. 前往 Github 设置页面

浏览器打开 https://github.com/settings/keys ,并在 SSH Keys 标题右方点击【New SSH Key】按钮,输入要备注的名称,并粘贴公钥保存即可

3. 验证是否配置成功

输入命令,并输入密码(可选),若输出你的 Github 用户名,说明成功

1
2
3
ssh -T git@github.com

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

三、添加密钥到 ssh-agent

若你的密钥设置了密码,每次都要输入密码,可添加到 ssh-agent 缓存起来,不需要频繁输入

1
2
3
4
5
6
7
8
# 后台启动 ssh-agent
eval "$(ssh-agent -s)"

# 添加密钥到 ssh-agent
ssh-add ~/.ssh/id_ed25519_github_your_email

# 若你是 macOS,还可以添加到apple密钥
# ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_your_email

评论