GitHub & Gitee SSH 密钥配置指南 🔑

🚀 一站式配置 SSH 密钥,轻松管理代码仓库
📋 目录
🎯 快速开始
🌟 一站式配置脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #!/bin/bash
echo "开始配置 SSH 密钥..."
git config --global user.name "meimolihan" git config --global user.email "meimolihan@gmail.com"
mkdir -p ~/.ssh chmod 700 ~/.ssh
echo "✅ SSH 配置完成!"
|
🔑 SSH 密钥生成
🎯 生成 GitHub 密钥
1 2 3 4 5
| 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
| 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 2 3 4 5 6 7 8
| cat ~/.ssh/id_rsa_github.pub | xclip -selection clipboard
cat ~/.ssh/id_rsa_github.pub | clip.exe
cat ~/.ssh/id_rsa_github.pub
|
登录 GitHub:
⚙️ GitHub 高级配置
1 2 3 4 5
| ssh-keyscan -t rsa,ed25519 github.com >> ~/.ssh/known_hosts
ssh-keygen -lf ~/.ssh/known_hosts | grep github
|
🐱 Gitee 配置
📝 添加 SSH 密钥到 Gitee
复制公钥到剪贴板:
1 2 3 4 5 6 7 8
| cat ~/.ssh/id_rsa_gitee.pub | xclip -selection clipboard
cat ~/.ssh/id_rsa_gitee.pub | clip.exe
cat ~/.ssh/id_rsa_gitee.pub
|
登录 Gitee:
⚙️ Gitee 高级配置
1 2 3 4 5
| 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
| cat > ~/.ssh/config << 'EOF'
Host github.com User git IdentityFile ~/.ssh/id_rsa_github Hostname ssh.github.com Port 443 IdentitiesOnly yes
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 -T git@github.com
ssh -Tv git@github.com
|
✅ 测试 Gitee 连接
1 2 3 4 5 6 7 8
| ssh -T git@gitee.com
ssh -Tv git@gitee.com
|
💡 故障排除
🐛 常见问题解决
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
| cat >> ~/.ssh/config << 'EOF'
Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_personal
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
| cat >> ~/.ssh/config << 'EOF' Host * Compression yes ControlMaster auto ControlPath ~/.ssh/sockets/%r@%h:%p ControlPersist 4h TCPKeepAlive yes EOF
mkdir -p ~/.ssh/sockets
|
🎯 提示: 现在目录链接应该可以正常跳转了!如果还有问题,请告诉我具体是哪个链接无法跳转。
📚 扩展资源:
GitHub & Gitee SSH 密钥配置指南 🔑