如何配置ssh key 到gitlab, 实现git push

在 Ubuntu 客户端配置 GitLab SSH Key 的步骤如下:

1. 生成 SSH Key

在你的 Ubuntu 系统中,使用 ssh-keygen 命令生成 SSH 密钥对(公钥和私钥)。

bash 复制代码
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • -t rsa 指定了生成的密钥类型为 RSA。
  • -b 4096 表示密钥的长度为 4096 位。
  • -C "your_email@example.com" 为生成的密钥添加一个标签(通常是你的电子邮箱)。

你会被提示输入存储密钥的位置,默认保存在 ~/.ssh/id_rsa。按回车键使用默认路径。

2. 查看生成的公钥

生成密钥对后,查看公钥的内容,稍后需要将它添加到 GitLab。

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

这会输出一长串以 ssh-rsa 开头的公钥字符串。

3. 将公钥添加到 GitLab

  1. 登录到你的 GitLab 账户。
  2. 点击右上角头像,选择 Settings
  3. 在左侧导航栏中选择 SSH Keys
  4. 将刚才生成的公钥(id_rsa.pub 的内容)粘贴到 Key 输入框中。
  5. 给密钥起一个名字(可以根据用途命名,如 "Ubuntu SSH Key")。
  6. 点击 Add key 按钮。

4. 配置 SSH 使用新密钥

确保你的 Git 客户端使用正确的 SSH Key。在终端中编辑 ~/.ssh/config 文件(如果该文件不存在,可以新建):

bash 复制代码
nano ~/.ssh/config

在文件中添加以下内容:

bash 复制代码
Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa
  • Host gitlab.com 指定 GitLab 的主机名。
  • IdentityFile ~/.ssh/id_rsa 告诉 SSH 使用你刚刚生成的私钥。

保存并退出文件。

5. 测试 SSH 连接

现在可以测试 SSH 是否成功连接到 GitLab:

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

如果设置正确,你会看到类似下面的输出:

bash 复制代码
Welcome to GitLab, @your_username!

6. 克隆、拉取、推送代码

一旦配置成功,你就可以使用 SSH 方式克隆、拉取或推送代码了。例如:

bash 复制代码
git clone git@gitlab.com:your_username/your_repository.git

现在,你已经完成了在 Ubuntu 系统上配置 GitLab SSH Key 的全过程。

相关推荐
happyness444 小时前
Git:AI编程时代的“安全带“与“时光机“
git·ai编程
To_OC4 小时前
踩坑无数!终于捋顺Git基础核心工作流(新手必看)
git·程序员
一拳一个娘娘腔5 小时前
Linux SSH免密登录:从“刷卡进门”到“刷脸通行”的完整指南
linux·运维·ssh
认真的薛薛6 小时前
Terraform: AWS VPC+可SSH登录EC2
ssh·aws·terraform
xuhaoyu_cpp_java9 小时前
Git学习(三)
经验分享·笔记·git·学习
C137的本贾尼10 小时前
Git基本操作(二):add与commit,把文件交给Git管理
git
heimeiyingwang11 小时前
【架构实战】GitOps实践:让运维更优雅
架构·gitlab·jenkins
咸鱼永不翻身11 小时前
Git Hooks—提交Commit前检查本地时间是否不对
git·git-hooks·git钩子
John_ToDebug11 小时前
如何针对指定目录生成 Git Patch 并精准应用到其他分支
chrome·git
Joy T12 小时前
【Web3】Hardhat工程架构中Solidity与TypeChain的协作机制
git·架构·typescript·web3·智能合约·hardhat·typechain