GitHub & Gitee SSH 密钥配置指南 🔑

🚀 一站式配置 SSH 密钥,轻松管理代码仓库


📋 目录


🎯 快速开始

🌟 一站式配置脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
# 🚀 GitHub & Gitee SSH 密钥快速配置脚本

echo "开始配置 SSH 密钥..."

# 设置全局 Git 配置
git config --global user.name "meimolihan"
git config --global user.email "meimolihan@gmail.com"

# 创建 SSH 目录
mkdir -p ~/.ssh
chmod 700 ~/.ssh

echo "✅ SSH 配置完成!"

🔑 SSH 密钥生成

🎯 生成 GitHub 密钥

1
2
3
4
5
# 生成 4096 位 RSA 密钥
ssh-keygen -t rsa -b 4096 -C "meimolihan@gmail.com" -f ~/.ssh/id_rsa_github

# 查看公钥内容
cat ~/.ssh/id_rsa_github.pub

🐱 生成 Gitee 密钥

1
2
3
4
5
# 生成 Gitee 专用密钥
ssh-keygen -t rsa -b 4096 -C "meimolihan@gmail.com" -f ~/.ssh/id_rsa_gitee

# 查看公钥内容
cat ~/.ssh/id_rsa_gitee.pub

📊 密钥管理命令

1
2
3
4
5
6
7
8
9
# 查看所有密钥
ls -la ~/.ssh/

# 检查密钥指纹
ssh-keygen -lf ~/.ssh/id_rsa_github.pub
ssh-keygen -lf ~/.ssh/id_rsa_gitee.pub

# 测试密钥有效性
ssh-keygen -y -f ~/.ssh/id_rsa_github

🌐 GitHub 配置

📝 添加 SSH 密钥到 GitHub

  1. 复制公钥到剪贴板:

    1
    2
    3
    4
    5
    6
    7
    8
    # Linux/Mac
    cat ~/.ssh/id_rsa_github.pub | xclip -selection clipboard

    # Windows (Git Bash)
    cat ~/.ssh/id_rsa_github.pub | clip.exe

    # 手动复制
    cat ~/.ssh/id_rsa_github.pub
  2. 登录 GitHub:

⚙️ GitHub 高级配置

1
2
3
4
5
# 添加 GitHub 到已知主机
ssh-keyscan -t rsa,ed25519 github.com >> ~/.ssh/known_hosts

# 验证主机密钥
ssh-keygen -lf ~/.ssh/known_hosts | grep github

🐱 Gitee 配置

📝 添加 SSH 密钥到 Gitee

  1. 复制公钥到剪贴板:

    1
    2
    3
    4
    5
    6
    7
    8
    # Linux/Mac
    cat ~/.ssh/id_rsa_gitee.pub | xclip -selection clipboard

    # Windows (Git Bash)
    cat ~/.ssh/id_rsa_gitee.pub | clip.exe

    # 手动复制
    cat ~/.ssh/id_rsa_gitee.pub
  2. 登录 Gitee:

⚙️ Gitee 高级配置

1
2
3
4
5
# 添加 Gitee 到已知主机
ssh-keyscan -t rsa,ed25519 gitee.com >> ~/.ssh/known_hosts

# 验证主机密钥
ssh-keygen -lf ~/.ssh/known_hosts | grep gitee

⚙️ SSH 配置管理

📁 创建 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
25
26
# 创建或编辑 SSH 配置
cat > ~/.ssh/config << 'EOF'
# GitHub 配置
Host github.com
User git
IdentityFile ~/.ssh/id_rsa_github
Hostname ssh.github.com
Port 443
IdentitiesOnly yes

# Gitee 配置
Host gitee.com
User git
IdentityFile ~/.ssh/id_rsa_gitee
IdentitiesOnly yes

# 通用配置
Host *
AddKeysToAgent yes
UseKeychain yes
ServerAliveInterval 60
ServerAliveCountMax 5
EOF

# 设置正确权限
chmod 600 ~/.ssh/config

🔍 连接验证

✅ 测试 GitHub 连接

1
2
3
4
5
6
7
8
# 测试 SSH 连接
ssh -T git@github.com

# 详细调试模式
ssh -Tv git@github.com

# 预期输出:
# Hi meimolihan! You've successfully authenticated, but GitHub does not provide shell access.

✅ 测试 Gitee 连接

1
2
3
4
5
6
7
8
# 测试 SSH 连接
ssh -T git@gitee.com

# 详细调试模式
ssh -Tv git@gitee.com

# 预期输出:
# Hi meimolihan(@meimolihan)! You've successfully authenticated, but GITEE.COM does not provide shell access.

💡 故障排除

🐛 常见问题解决

1. 权限问题

1
2
3
4
5
6
7
8
# 修复文件权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
chmod 644 ~/.ssh/*.pub
chmod 644 ~/.ssh/known_hosts

# 检查权限
ls -la ~/.ssh/

2. 连接超时

1
2
3
4
5
6
7
8
9
10
# 测试网络连通性
ping github.com
ping gitee.com

# 检查防火墙
sudo ufw status
sudo iptables -L

# 使用不同端口测试
ssh -T -p 443 git@ssh.github.com

🚀 高级用法

🔧 多平台配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 多 GitHub 账户配置
cat >> ~/.ssh/config << 'EOF'
# 个人 GitHub
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_personal

# 工作 GitHub
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_work
EOF

⚡ 性能优化

1
2
3
4
5
6
7
8
9
10
11
12
# SSH 连接优化
cat >> ~/.ssh/config << 'EOF'
Host *
Compression yes
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h:%p
ControlPersist 4h
TCPKeepAlive yes
EOF

# 创建 sockets 目录
mkdir -p ~/.ssh/sockets

🎯 提示: 现在目录链接应该可以正常跳转了!如果还有问题,请告诉我具体是哪个链接无法跳转。

📚 扩展资源: