学习目标
- 了解SSH密钥的工作原理
- 掌握生成SSH密钥的方法
- 学会配置GitHub SSH连接
- 掌握SSH连接的优势和使用技巧
学习计划
- SSH密钥基础知识
- 生成SSH密钥对
- 配置GitHub SSH密钥
- 测试SSH连接
- 配置Git使用SSH
- 常见问题和解决方案
1. SSH密钥基础知识
1.1 SSH密钥的作用
SSH密钥是一种安全的身份验证方式,比密码更安全,可以避免每次输入密码的麻烦。
1.2 SSH密钥的工作原理
- 公钥:存储在GitHub服务器上,用于验证身份
- 私钥:存储在本地计算机上,用于身份验证
- 非对称加密:公钥加密,私钥解密
1.3 SSH连接的优势
- 安全性高:比密码更安全
- 无需密码:配置后无需每次输入密码
- 自动化友好:适合脚本和自动化工具
- 多设备支持:可以在多台设备上使用
2. 生成SSH密钥对
2.1 检查现有SSH密钥
1
2
3
4
5
6
7
8
| # 检查是否已有SSH密钥
ls -la ~/.ssh
# 输出:
# total 20
# drwx------ 2 user user 4096 Jun 10 10:00 .
# drwx------ 3 user user 4096 Jun 10 10:00 ..
# -rw------- 1 user user 1679 Jun 10 10:00 id_rsa
# -rw-r--r-- 1 user user 399 Jun 10 10:00 id_rsa.pub
|
2.2 生成新的SSH密钥
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| # 生成SSH密钥对
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 输出:
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/user/.ssh/id_rsa): [按回车使用默认路径]
# Enter passphrase (empty for no passphrase): [可以设置密码短语,也可以直接回车]
# Enter same passphrase again: [再次输入密码短语]
# Your identification has been saved in /home/user/.ssh/id_rsa.
# Your public key has been saved in /home/user/.ssh/id_rsa.pub.
# The key fingerprint is:
# SHA256:abcdef1234567890abcdef1234567890abcdef12 your_email@example.com
# The key's randomart image is:
# +---[RSA 4096]----+
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# +----[SHA256]-----+
|
2.3 启动SSH代理
1
2
3
4
5
6
7
| # 启动SSH代理
eval "$(ssh-agent -s)"
# 输出: Agent pid 12345
# 添加SSH密钥到代理
ssh-add ~/.ssh/id_rsa
# 输出: Identity added: /home/user/.ssh/id_rsa (your_email@example.com)
|
3. 配置GitHub SSH密钥
3.1 复制公钥内容
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 查看公钥内容
cat ~/.ssh/id_rsa.pub
# 输出:
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC... your_email@example.com
# 或者使用clipboard命令复制(Windows)
clip < ~/.ssh/id_rsa.pub
# 或者使用pbcopy命令复制(macOS)
pbcopy < ~/.ssh/id_rsa.pub
# 或者使用xclip命令复制(Linux)
xclip -sel clip < ~/.ssh/id_rsa.pub
|
3.2 在GitHub中添加SSH密钥
- 登录GitHub
- 点击右上角头像 → Settings
- 左侧菜单选择 “SSH and GPG keys”
- 点击 “New SSH key”
- 填写信息:
- Title: 给密钥起个名字(如:My Laptop)
- Key: 粘贴刚才复制的公钥内容
- 点击 “Add SSH key”
3.3 验证SSH密钥添加
1
2
3
4
5
6
7
8
9
10
| # 测试SSH连接
ssh -T git@github.com
# 首次连接会看到:
# The authenticity of host 'github.com (140.82.112.4)' can't be established.
# RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
# Are you sure you want to continue connecting (yes/no)? yes
# 成功后会看到:
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.
|
4. 测试SSH连接
4.1 测试连接状态
1
2
3
4
5
6
7
| # 测试SSH连接
ssh -T git@github.com
# 输出: Hi username! You've successfully authenticated, but GitHub does not provide shell access.
# 测试连接详细信息
ssh -vT git@github.com
# 输出详细的连接信息,包括密钥验证过程
|
4.2 检查SSH配置
1
2
3
4
5
6
| # 查看SSH配置文件
cat ~/.ssh/config
# 如果没有配置文件,创建一个
touch ~/.ssh/config
chmod 600 ~/.ssh/config
|
4.3 配置SSH配置文件
1
2
3
4
5
6
7
8
9
| # 编辑SSH配置文件
nano ~/.ssh/config
# 添加以下内容:
# Host github.com
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_rsa
# IdentitiesOnly yes
|
5. 配置Git使用SSH
5.1 配置Git用户信息
1
2
3
4
5
6
7
8
9
| # 设置Git用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
# 验证配置
git config --global user.name
# 输出: Your Name
git config --global user.email
# 输出: your_email@example.com
|
5.2 克隆仓库使用SSH
1
2
3
4
5
6
7
8
9
10
11
| # 使用SSH URL克隆仓库
git clone git@github.com:username/repository.git
# 输出:
# Cloning into 'repository'...
# remote: Enumerating objects: 100, done.
# remote: Counting objects: 100% (100/100), done.
# remote: Compressing objects: 100% (80/80), done.
# remote: Total 100 (delta 20), reused 100 (delta 20), pack-reused 0
# Receiving objects: 100% (100/100), 1.23 MiB | 2.45 MiB/s, done.
# Resolving deltas: 100% (20/20), done.
|
5.3 修改现有仓库的远程URL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 查看当前远程URL
git remote -v
# 输出:
# origin https://github.com/username/repository.git (fetch)
# origin https://github.com/username/repository.git (push)
# 修改为SSH URL
git remote set-url origin git@github.com:username/repository.git
# 验证修改
git remote -v
# 输出:
# origin git@github.com:username/repository.git (fetch)
# origin git@github.com:username/repository.git (push)
|
5.4 测试Git操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 测试推送
git push origin main
# 输出:
# Enumerating objects: 5, done.
# Counting objects: 100% (5/5), done.
# Delta compression using up to 8 threads
# Compressing objects: 100% (3/3), done.
# Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
# Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
# To github.com:username/repository.git
# abc1234..def5678 main -> main
# 测试拉取
git pull origin main
# 输出:
# Already up to date.
|
6. 常见问题和解决方案
6.1 问题1:SSH密钥权限错误
1
2
3
4
5
6
7
| # 错误信息
# Permissions 0644 for '/home/user/.ssh/id_rsa' are too open.
# 解决方案
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 700 ~/.ssh
|
6.2 问题2:SSH代理未启动
1
2
3
4
5
6
| # 错误信息
# Could not open a connection to your authentication agent.
# 解决方案
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
|
6.3 问题3:多个SSH密钥
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 创建SSH配置文件
nano ~/.ssh/config
# 添加多个密钥配置
# Host github.com
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_rsa_github
# IdentitiesOnly yes
#
# Host gitlab.com
# HostName gitlab.com
# User git
# IdentityFile ~/.ssh/id_rsa_gitlab
# IdentitiesOnly yes
|
6.4 问题4:连接超时
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 测试网络连接
ping github.com
# 检查防火墙设置
# Windows: 检查Windows防火墙
# macOS: 检查系统偏好设置
# Linux: 检查iptables或ufw
# 使用代理(如果需要)
# 在SSH配置中添加代理设置
# Host github.com
# HostName github.com
# User git
# ProxyCommand nc -X connect -x proxy.example.com:8080 %h %p
|
6.5 问题5:密钥被拒绝
1
2
3
4
5
6
7
8
9
10
| # 检查密钥是否正确添加
ssh -T git@github.com
# 重新生成密钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 重新添加到GitHub
# 1. 复制新公钥
cat ~/.ssh/id_rsa.pub
# 2. 在GitHub中删除旧密钥,添加新密钥
|
7. 高级配置和技巧
7.1 配置SSH密钥密码短语
1
2
3
4
5
6
7
| # 生成带密码短语的密钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 设置密码短语后,每次使用需要输入密码
# 可以使用ssh-add避免重复输入
ssh-add ~/.ssh/id_rsa
# 输入密码短语后,会话期间无需重复输入
|
7.2 使用SSH配置文件管理多个账户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| # 编辑SSH配置文件
nano ~/.ssh/config
# 配置多个GitHub账户
# Host github-personal
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_rsa_personal
# IdentitiesOnly yes
#
# Host github-work
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_rsa_work
# IdentitiesOnly yes
# 使用不同账户克隆仓库
git clone git@github-personal:username/repository.git
git clone git@github-work:company/repository.git
|
7.3 自动化SSH密钥管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # 创建SSH密钥管理脚本
nano ~/scripts/ssh-setup.sh
# 脚本内容:
#!/bin/bash
# 启动SSH代理
eval "$(ssh-agent -s)"
# 添加所有SSH密钥
for key in ~/.ssh/id_rsa*; do
if [[ -f "$key" && ! "$key" =~ \.pub$ ]]; then
ssh-add "$key"
fi
done
# 测试GitHub连接
ssh -T git@github.com
# 设置脚本权限
chmod +x ~/scripts/ssh-setup.sh
# 添加到启动脚本
echo "~/scripts/ssh-setup.sh" >> ~/.bashrc
|
7.4 使用SSH密钥进行部署
1
2
3
4
5
6
7
8
9
10
11
| # 在服务器上配置SSH密钥
# 1. 生成服务器密钥
ssh-keygen -t rsa -b 4096 -C "server@example.com"
# 2. 将服务器公钥添加到GitHub
cat ~/.ssh/id_rsa.pub
# 3. 在GitHub仓库设置中添加Deploy Keys
# 4. 在服务器上克隆仓库
git clone git@github.com:username/repository.git
|
8. 总结
8.1 关键要点
- SSH密钥生成:使用
ssh-keygen
生成密钥对
- GitHub配置:将公钥添加到GitHub账户
- SSH代理:使用
ssh-agent
管理密钥
- Git配置:使用SSH URL进行Git操作
- 权限设置:正确设置SSH密钥文件权限
8.2 操作流程总结
- 生成SSH密钥:
ssh-keygen -t rsa -b 4096 -C "email"
- 启动SSH代理:
eval "$(ssh-agent -s)"
- 添加密钥到代理:
ssh-add ~/.ssh/id_rsa
- 复制公钥:
cat ~/.ssh/id_rsa.pub
- 添加到GitHub:在GitHub设置中添加SSH密钥
- 测试连接:
ssh -T git@github.com
- 配置Git:使用SSH URL克隆或修改远程URL
8.3 最佳实践
- 定期更新SSH密钥
- 为不同用途使用不同密钥
- 设置强密码短语
- 定期检查SSH配置
- 备份SSH密钥
8.4 学习收获
通过这次学习,我掌握了:
- SSH密钥的生成和配置方法
- GitHub SSH连接的完整流程
- Git使用SSH的优势和技巧
- 多账户SSH配置方法
- SSH连接故障排查技能
这些知识对于开发者来说非常重要,能够提高Git操作的效率和安全性,是日常开发工作的必备技能。
8.5 注意事项
- 私钥必须保密,不要分享给他人
- 定期更换SSH密钥
- 在不同设备上需要重新配置
- 生产环境使用更严格的权限设置
- 备份重要的SSH密钥