首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
SAAS
ToB门户
了解全球最新的ToB事件
论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
微博
Follow
记录
Doing
博客
Blog
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
排行榜
Ranklist
相册
Album
应用中心
qidao123.com ToB IT社区-企服评测·应用市场
»
论坛
›
数据库
›
Oracle
›
天生 SSH Key 并设置 GitHub/GitLab 详细教程
返回列表
发新帖
天生 SSH Key 并设置 GitHub/GitLab 详细教程
[复制链接]
发表于 2025-10-1 20:43:33
|
显示全部楼层
|
阅读模式
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
×
🔑 天生 SSH Key 并设置 GitHub/GitLab 详细教程
🟢 第 1 步:查抄是否已有 SSH Key
在
Git Bash
(Windows)、
终端
(Linux/macOS) 运行以下下令:
ls -al ~/.ssh
复制
代码
🔹
大概的输出:
如果已有 SSH Key(如 id_rsa 和 id_rsa.pub)
分析你已经天生过 SSH Key,可以跳到
第 4 步
直接添加到 GitHub/GitLab。
如果 .ssh 目次不存在或没有 id_rsa 文件
分析你必要天生新的 SSH Key,请继承下一步。
🟢 第 2 步:天生新的 SSH Key
运行以下下令:
ssh-keygen -t rsa -b 4096 -C "你的邮箱"
复制
代码
示例:
ssh-keygen -t rsa -b 4096 -C "
[email protected]
"
复制
代码
🔹
参数分析:
-t rsa :使用 RSA 算法(GitHub/GitLab 保举)
-b 4096 :天生 4096 位密钥(比默认 2048 位更
安全
)
-C "你的邮箱" :添加解释(通常是你的 GitHub/GitLab 绑定邮箱)
💡
体系会扣问以下题目
:
Enter file in which to save the key (~/.ssh/id_rsa 默认回车)
Enter file in which to save the key (/home/your-user/.ssh/id_rsa):
复制代码
直接回车
使用默认路径(保举)
如果已有密钥
,可以换个名字,如 id_rsa_github
Enter passphrase(输入暗码,可留空)
Enter passphrase (empty for no passphrase):
复制代码
发起直接回车
(否则每次使用 SSH 都要输入暗码)
天生乐成后,会在 ~/.ssh/ 目次下创建两个文件:
~/.ssh/id_rsa # 私钥(不要分享)
~/.ssh/id_rsa.pub # 公钥(需要添加到 GitHub/GitLab)
复制代码
🟢 第 3 步:启动 SSH Agent 并添
加密
钥
1️⃣
启动 SSH 署理
(用于管理密钥):
eval "$(ssh-agent -s)"
复制代码
乐成时会返回:
Agent pid 12345
复制代码
2️⃣
将私钥添加到 SSH 署理
:
ssh-add ~/.ssh/id_rsa
复制代码
如果你的私钥文件名不是 id_rsa,请修改为现实名称:
ssh-add ~/.ssh/id_rsa
_github
复制代码
💡
如果报错
:
Could not open a connection to your authentication agent
复制代码
请先运行:
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
复制代码
🟢 第 4 步:复制 SSH 公钥
运行:
cat ~/.ssh/id_rsa.pub
复制代码
🔹
复制公钥的方法:
Windows Git Bash
:
clip < ~/.ssh/id_rsa.pub
复制代码
macOS
:
pbcopy < ~/.ssh/id_rsa.pub
复制代码
Linux
(手动复制):
cat ~/.ssh/id_rsa.pub
复制代码
🟢 第 5 步:添加 SSH Key 到 GitHub/GitLab
🔹
GitHub
:
打开 GitHub SSH 设置
点击 New SSH Key
Title:任意填(如 “My Laptop”)
Key:粘贴 id_rsa.pub 里的内容
点击 Add SSH Key
🔹
GitLab
:
打开 Preferences -> SSH Keys
粘贴 id_rsa.pub 的内容
点击 Add Key
🟢 第 6 步:测试 SSH 毗连
ssh -T
[email protected]
复制代码
如果乐成,会看到:
Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.
复制代码
🎉
SSH Key 设置乐成!
🟢 第 7 步:设置 Git 使用 SSH 方式拉取代码
默认情况下,Git 大概还在使用 HTTPS,必要手动改为 SSH。
✅ 设置全局 Git 远程 URL 方式:
git config --global url."
[email protected]
:".insteadOf "https://github.com/"
复制代码
如许你 git clone 或 git push 就不会要求输入 GitHub 账号和暗码了!
🟢 第 8 步:使用 SSH 克隆 GitHub/GitLab 堆栈
💡
HTTPS 方式(必要输入暗码):
git clone https://github.com/your-username/repository.git
复制代码
💡
SSH 方式(不必要输入暗码):
git clone
[email protected]
:your-username/repository.git
复制代码
如果使用 GitLab:
git clone
[email protected]
:your-username/repository.git
复制代码
🎯 总结
✔
查抄 SSH Key
✔
天生 SSH Key
✔
启动 SSH 署理 & 添
加密
钥
✔
复制公钥 & 添加到 GitHub/GitLab
✔
测试 SSH 毗连
✔
使用 SSH 克隆堆栈
💡
完成后,你的 Git 毗连将更加
安全
,且无需每次输入暗码!🚀
回复
使用道具
举报
返回列表
浏览过的版块
SAAS
Java
数据仓库与分析
云原生
分布式数据库
数据安全
前端开发
ERP
IT职场那些事
梦应逍遥
+ 我要发帖
登录后关闭弹窗
登录参与点评抽奖 加入IT实名职场社区
去登录
微信订阅号
微信服务号
微信客服(加群)
H5
小程序
快速回复
返回顶部
返回列表