生成 SSH Key 并配置 GitHub/GitLab 详细教程

🔑 生成 SSH Key 并配置 GitHub/GitLab 详细教程

🟢 第 1 步:检查是否已有 SSH Key

Git Bash (Windows)、终端 (Linux/macOS) 运行以下命令:

bash 复制代码
ls -al ~/.ssh

🔹 可能的输出:

  • 如果已有 SSH Key(如 id_rsaid_rsa.pub
    说明你已经生成过 SSH Key,可以跳到 第 4 步 直接添加到 GitHub/GitLab。
  • 如果 .ssh 目录不存在或没有 id_rsa 文件
    说明你需要生成新的 SSH Key,请继续下一步。

🟢 第 2 步:生成新的 SSH Key

运行以下命令:

bash 复制代码
ssh-keygen -t rsa -b 4096 -C "你的邮箱"

示例:

bash 复制代码
ssh-keygen -t rsa -b 4096 -C "yaoyuzhuo6@gmail.com"

🔹 参数说明:

  • -t rsa :使用 RSA 算法(GitHub/GitLab 推荐)
  • -b 4096 :生成 4096 位密钥(比默认 2048 位更安全)
  • -C "你的邮箱" :添加注释(通常是你的 GitHub/GitLab 绑定邮箱)

💡 系统会询问以下问题

  1. Enter file in which to save the key (~/.ssh/id_rsa 默认回车)

    bash 复制代码
    Enter file in which to save the key (/home/your-user/.ssh/id_rsa):
    • 直接回车 使用默认路径(推荐)
    • 如果已有密钥 ,可以换个名字,如 id_rsa_github
  2. Enter passphrase(输入密码,可留空)

    bash 复制代码
    Enter passphrase (empty for no passphrase):
    • 建议直接回车(否则每次使用 SSH 都要输入密码)

生成成功后,会在 ~/.ssh/ 目录下创建两个文件:

复制代码
~/.ssh/id_rsa      # 私钥(不要分享)
~/.ssh/id_rsa.pub  # 公钥(需要添加到 GitHub/GitLab)

🟢 第 3 步:启动 SSH Agent 并添加密钥

1️⃣ 启动 SSH 代理(用于管理密钥):

bash 复制代码
eval "$(ssh-agent -s)"

成功时会返回:

bash 复制代码
Agent pid 12345

2️⃣ 将私钥添加到 SSH 代理

bash 复制代码
ssh-add ~/.ssh/id_rsa

如果你的私钥文件名不是 id_rsa,请修改为实际名称:

bash 复制代码
ssh-add ~/.ssh/id_rsa_github

💡 如果报错

复制代码
Could not open a connection to your authentication agent

请先运行:

bash 复制代码
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa

🟢 第 4 步:复制 SSH 公钥

运行:

bash 复制代码
cat ~/.ssh/id_rsa.pub

🔹 复制公钥的方法:

  • Windows Git Bash

    bash 复制代码
    clip < ~/.ssh/id_rsa.pub
  • macOS

    bash 复制代码
    pbcopy < ~/.ssh/id_rsa.pub
  • Linux(手动复制):

    bash 复制代码
    cat ~/.ssh/id_rsa.pub

🟢 第 5 步:添加 SSH Key 到 GitHub/GitLab

🔹 GitHub

  1. 打开 GitHub SSH 设置
  2. 点击 New SSH Key
  3. Title:随便填(如 "My Laptop")
  4. Key:粘贴 id_rsa.pub 里的内容
  5. 点击 Add SSH Key

🔹 GitLab

  1. 打开 Preferences -> SSH Keys
  2. 粘贴 id_rsa.pub 的内容
  3. 点击 Add Key

🟢 第 6 步:测试 SSH 连接

bash 复制代码
ssh -T git@github.com

如果成功,会看到:

bash 复制代码
Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.

🎉 SSH Key 配置成功!


🟢 第 7 步:设置 Git 使用 SSH 方式拉取代码

默认情况下,Git 可能还在使用 HTTPS,需要手动改为 SSH。

✅ 设置全局 Git 远程 URL 方式:

bash 复制代码
git config --global url."git@github.com:".insteadOf "https://github.com/"

这样你 git clonegit push 就不会要求输入 GitHub 账号和密码了!


🟢 第 8 步:使用 SSH 克隆 GitHub/GitLab 仓库

💡 HTTPS 方式(需要输入密码):

bash 复制代码
git clone https://github.com/your-username/repository.git

💡 SSH 方式(不需要输入密码):

bash 复制代码
git clone git@github.com:your-username/repository.git

如果使用 GitLab:

bash 复制代码
git clone git@gitlab.com:your-username/repository.git

🎯 总结

检查 SSH Key

生成 SSH Key

启动 SSH 代理 & 添加密钥

复制公钥 & 添加到 GitHub/GitLab

测试 SSH 连接

使用 SSH 克隆仓库

💡 完成后,你的 Git 连接将更加安全,且无需每次输入密码!🚀

相关推荐
油泼辣子多加7 小时前
2025年06月30日Github流行趋势
github
ai小鬼头8 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
Lois_Luo9 小时前
使用 DigitalPlat 免费搭配 Cloudflare Tunnel 实现飞牛系统、服务及 SSH 内网穿透教程
ssh·内网穿透
裁二尺秋风11 小时前
CI/CD — Pipeline的使用以及Blue Ocean多分支流水线的使用方法
ci/cd·gitlab·jenkins
寻月隐君12 小时前
Rust 异步编程实践:从 Tokio 基础到阻塞任务处理模式
后端·rust·github
bingGO5499112 小时前
github 集成CICD自动化部署
github
超龄超能程序猿13 小时前
Bitvisse SSH Client 安装配置文档
运维·ssh·github
Natsume171016 小时前
嵌入式开发:GPIO、UART、SPI、I2C 驱动开发详解与实战案例
c语言·驱动开发·stm32·嵌入式硬件·mcu·架构·github
荔枝吻17 小时前
【AI总结】Git vs GitHub vs GitLab:深度解析三者联系与核心区别
人工智能·git·github
幻凡ss17 小时前
github pages使用免费CDN加速-netlify
github·github pages·github cdn·github pages加速·netlify加速github·hexo博客免费cdn加速·个人博客免费cdn加速