Appearance
SSH 与认证
与远程仓库通信需要认证。Git 支持两种主要认证方式:HTTPS 和 SSH。本节重点介绍 SSH 认证的配置,以及两种方式的对比。
生成 SSH 密钥
生成 Ed25519 密钥(推荐)
Ed25519 是目前推荐的算法,比传统的 RSA 更安全、更小巧:
bash
# 生成 Ed25519 密钥对
ssh-keygen -t ed25519 -C "zhangsan@example.com"
# 按提示操作:
# Enter file in which to save the key: 回车使用默认路径 ~/.ssh/id_ed25519
# Enter passphrase: 设置密码短语(推荐设置,提高安全性)
# Enter same passphrase again: 重复密码生成后会产生两个文件:
~/.ssh/id_ed25519:私钥(绝对不要分享给任何人!)~/.ssh/id_ed25519.pub:公钥(需要上传到 GitHub/GitLab)
生成 RSA 密钥(备选)
如果服务器不支持 Ed25519:
bash
# 生成 4096 位 RSA 密钥
ssh-keygen -t rsa -b 4096 -C "zhangsan@example.com"查看公钥
bash
# 查看公钥内容(用于复制到平台)
cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... zhangsan@example.com启动 SSH Agent
SSH Agent 可以缓存私钥密码,避免每次使用时都要输入:
bash
# 启动 ssh-agent
eval "$(ssh-agent -s)"
# Agent pid 12345
# 添加私钥到 agent
ssh-add ~/.ssh/id_ed25519
# 查看已添加的密钥
ssh-add -lmacOS 永久配置(添加到 ~/.ssh/config):
Host *
AddKeysToAgent yes
UseKeychain yes # macOS 专用:使用系统钥匙串存储密码
IdentityFile ~/.ssh/id_ed25519添加密钥到 GitHub
- 复制公钥内容:
bash
# macOS
cat ~/.ssh/id_ed25519.pub | pbcopy
# Linux
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
# 或
cat ~/.ssh/id_ed25519.pub
# Windows (PowerShell)
Get-Content ~/.ssh/id_ed25519.pub | Set-Clipboard- 登录 GitHub → 头像 → Settings → SSH and GPG keys → New SSH key
- 填写标题(如 "MacBook Pro 2024")、粘贴公钥 → Add SSH key
测试连接
bash
# 测试 GitHub SSH 连接
ssh -T git@github.com
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.
# 测试 GitLab SSH 连接
ssh -T git@gitlab.com
# Welcome to GitLab, @username!添加密钥到 GitLab
- 复制公钥内容
- 登录 GitLab → 头像 → Preferences → SSH Keys → 粘贴公钥 → Add key
多账户 SSH 配置
当你有多个 GitHub 账户(如个人账户和公司账户)时,需要配置 SSH Config 文件区分。
生成多个密钥
bash
# 个人账户密钥
ssh-keygen -t ed25519 -C "personal@gmail.com" -f ~/.ssh/id_ed25519_personal
# 公司账户密钥
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work配置 ~/.ssh/config
bash
# 编辑 SSH 配置文件
nano ~/.ssh/config# 个人 GitHub 账户
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
AddKeysToAgent yes
# 公司 GitHub 账户
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
AddKeysToAgent yes
# GitLab 账户
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_work
AddKeysToAgent yes使用自定义 Host
配置后,使用自定义 Host 名称代替 github.com:
bash
# 克隆个人仓库
git clone git@github-personal:personal-user/repo.git
# 克隆公司仓库
git clone git@github-work:company-org/repo.git
# 测试连接
ssh -T git@github-personal
ssh -T git@github-work修改已有仓库的 remote URL:
bash
# 查看当前 remote
git remote -v
# origin git@github.com:user/repo.git
# 修改为使用自定义 Host
git remote set-url origin git@github-work:company-org/repo.gitHTTPS vs SSH 认证方式对比
| 对比项 | HTTPS | SSH |
|---|---|---|
| 认证方式 | 用户名 + 密码/Token | 密钥对 |
| 配置难度 | 简单 | 稍复杂 |
| 安全性 | 取决于 Token 管理 | 高(私钥本地保管) |
| 防火墙友好 | 是(端口 443) | 可能被封(端口 22) |
| 适合场景 | 临时操作、CI/CD | 日常开发 |
| 多账户 | 较复杂 | 通过 config 文件管理 |
| 克隆 URL 格式 | https://github.com/user/repo.git | git@github.com:user/repo.git |
HTTPS Token 认证
GitHub 在 2021 年不再支持密码认证,需要使用 Personal Access Token(PAT):
- GitHub → Settings → Developer settings → Personal access tokens → Generate new token
- 选择所需权限(至少需要
repo权限) - 保存 Token(只显示一次!)
使用 Token:
bash
# 克隆时输入 Token 代替密码
git clone https://github.com/user/repo.git
# Username: your-username
# Password: ghp_xxx(你的 Token)凭证存储(Credential Helper)
避免每次 HTTPS 操作都输入 Token,可以使用凭证存储:
系统凭证管理器(推荐)
bash
# macOS(使用系统钥匙串)
git config --global credential.helper osxkeychain
# Windows(使用 Windows 凭证管理器)
git config --global credential.helper manager
# Linux(使用 GNOME 钥匙环)
git config --global credential.helper /usr/lib/git-core/git-credential-gnome-keyring内存缓存(临时)
bash
# 缓存凭证 1 小时(3600 秒)
git config --global credential.helper "cache --timeout=3600"文件存储(不推荐,明文存储)
bash
# 存储到文件(明文,不安全!)
git config --global credential.helper store
# 凭证保存在 ~/.git-credentialsGitHub CLI 认证(推荐)
使用 GitHub 官方 CLI 工具进行认证:
bash
# 安装 GitHub CLI
brew install gh # macOS
winget install GitHub.cli # Windows
# 登录
gh auth login
# 配置 Git 使用 GitHub CLI 进行认证
gh auth setup-git常见问题
SSH 连接超时
bash
# 尝试通过 443 端口连接(某些防火墙会封锁 22 端口)
# 在 ~/.ssh/config 中添加:
Host github.com
Hostname ssh.github.com
Port 443
User git权限被拒绝
bash
# 调试 SSH 连接
ssh -vT git@github.com
# 检查私钥权限(必须是 600)
ls -la ~/.ssh/
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 700 ~/.ssh/总结
对于日常开发,SSH 是更推荐的认证方式,配置一次即可长期使用。对于 CI/CD 环境,使用 HTTPS + Token 更为方便。
配置好认证后,就可以开始与远程仓库顺畅协作了。