配置 GitHub 通过 SSH 秘钥连接

1. 生成key

1
2
ssh-keygen -t ed25519 -f ~/.git/github_auth -C '备注,可用邮箱'

2. 添加秘钥到 ssh-agent

如果自定义了秘钥文件名,就需要把秘钥添加到 ssh-agent 中。

1
2
3
ssh-add ~/.ssh/github_auth
# 如果秘钥有口令,则加上 -K 参数,输入口令后,就不用每次连接都要输入 key 口令
ssh-add -K ~/.ssh/github_auth

这一步也可以通过 ssh config 文件来显式设置域名用指定的秘钥

1
2
3
4
5
6
7
vim ~/.ssh/config

# 加入下面的内容

Host github.com # ssh 主机名
HostName github.com # 连接域名或IP
IdentityFile ~/.ssh/github_auth # 私钥路径

3. 添加秘钥到 GitHub 账户

个人中心 Settings -> SSH and GPG keys -> New SSH Key

~/.ssh/github_auth.pub 内容粘贴到 Key 输入框。

4. 验证

用一下命令验证是否配置成功。

1
ssh -T git@github.com

注:

安装好 git 后,需要先设置全局用户名和邮箱才能使用。

1
2
3
git config --list
git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

配置 GitHub 通过 SSH 秘钥连接

https://coderpan.com/tools/github-set-ssh-key.html

作者

CoderPan

发布于

2023-09-22

更新于

2024-04-21

许可协议

评论