GitHub 设置教程:配置 SSH 密钥(适用于 macOS/Linux/Windows)

欢迎使用这个简单易懂的 GitHub SSH 密钥设置教程!通过 SSH 密钥,你可以安全地连接 GitHub,无需每次输入用户名和密码。整个过程只需 5-10 分钟。

前提条件

  • 已安装 Git(如果没有,运行 git --version 检查;安装命令:macOS 用 brew install git,Windows 用 Git for Windows)。
  • 一个 GitHub 账号(https://github.com)。
  • 终端工具(macOS/Linux 用 Terminal,Windows 用 Git Bash 或 PowerShell)。

🔑 步骤 1: 检查现有 SSH 密钥

首先,检查是否已有 SSH 密钥。如果有,可以跳过生成步骤。

在终端运行:

ls -al ~/.ssh
  • 查找文件如 id_rsa.pubid_ed25519.pub 等。
  • 如果看到 .pub 文件,说明已有公钥。跳到步骤 4 获取公钥内容。
  • 如果没有,继续下一步。

🔑 步骤 2: 生成新的 SSH 密钥

我们推荐使用 ED25519 算法(更安全、更现代)。运行以下命令,替换 your_email@example.com 为你的 GitHub 邮箱:

ssh-keygen -t ed25519 -C "your_email@example.com"
  • 提示输入文件路径时,按 Enter 使用默认路径(~/.ssh/id_ed25519)。
  • 提示输入密码时(可选),输入一个强密码或直接 Enter(无密码,但需每次输入 passphrase)。
  • 生成后,会看到类似输出:
    Generating public/private ed25519 key pair.
    Your identification has been saved in /Users/yourname/.ssh/id_ed25519
    Your public key has been saved in /Users/yourname/.ssh/id_ed25519.pub
    

注意:如果你的系统不支持 ED25519(旧版 OpenSSH),用 RSA 代替:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

🔧 步骤 3: 启动 SSH 代理并添加密钥

SSH 代理会管理你的密钥。

  1. 启动代理(macOS/Linux):

    eval "$(ssh-agent -s)"
    
    • Windows:运行 start-ssh-agent(或在 Git Bash 中运行上述命令)。
  2. 添加私钥到代理:

    ssh-add ~/.ssh/id_ed25519
    
    • 如果有密码,会提示输入。

提示:要让代理自动启动,添加到 ~/.ssh/config(如果文件不存在,创建它):

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

📄 步骤 4: 获取并复制公钥内容

公钥是 .pub 文件的内容,用于上传到 GitHub。

运行:

cat ~/.ssh/id_ed25519.pub
  • 输出类似:
    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your_email@example.com
    
  • 全选并复制整个输出(包括 ssh-ed25519 开头和邮箱结尾)。

快速复制到剪贴板(macOS):

pbcopy < ~/.ssh/id_ed25519.pub
  • Linux:用 xclip -sel clip < ~/.ssh/id_ed25519.pub
  • Windows:用 clip < ~/.ssh\id_ed25519.pub

🌐 步骤 5: 在 GitHub 上添加 SSH 密钥

  1. 登录 GitHub,访问 SSH 设置页面
  2. Title:输入一个描述,如 “My MacBook Pro”。
  3. Key:粘贴刚才复制的公钥内容。
  4. 点击 Add SSH key。如果提示验证,输入 GitHub 密码。

🧪 步骤 6: 测试 SSH 连接

在终端运行:

ssh -T git@github.com
  • 预期输出:
    Hi username! You've successfully authenticated, but GitHub does not provide shell access.
    
  • 如果看到 “successfully authenticated”,恭喜!如果提示 “Permission denied”,检查密钥是否正确添加,或运行 ssh-add -l 确认密钥在代理中。

常见问题

  • “Host key verification failed”:运行 ssh-keyscan github.com >> ~/.ssh/known_hosts
  • Windows 用户:确保使用 Git Bash。

🚀 步骤 7: 配置 Git 仓库使用 SSH

对于现有仓库,切换远程 URL 为 SSH:

  1. 进入仓库目录:

    cd your-repo
    
  2. 查看当前远程:

    git remote -v
    
    • 如果是 HTTPS(如 https://github.com/username/repo.git),切换:
      git remote set-url origin git@github.com:username/repo.git
      
  3. 测试推送:

    git push origin main
    
    • 首次可能提示确认主机,按 “yes”。

对于新仓库:

完成!你的配置总结

项目 状态 详情
密钥类型 ✅ 生成 ED25519 (推荐) 或 RSA
密钥路径 ~/.ssh/id_ed25519 (私钥), ~/.ssh/id_ed25519.pub (公钥)
SSH 代理 已添加密钥,支持自动加载
GitHub 添加 密钥已上传,连接测试通过
Git 远程 已切换到 git@github.com:...

🎉 现在你可以做什么

  • 推送/拉取代码git push/pull 无需密码。
  • 克隆仓库:用 SSH URL git clone git@github.com:username/repo.git
  • 多设备同步:重复此教程生成新密钥并添加。
  • 高级:支持 GPG 签名(可选,搜索 “Git GPG tutorial”)。

如果遇到问题,运行 git config --global user.name "Your Name"git config --global user.email "your@email.com" 配置 Git 身份。参考官方文档:https://docs.github.com/en/authentication/connecting-to-github-with-ssh。

有疑问?随时问我!🚀

写文章用