如何在极狐GitLab中添加 SSH Key?

本文分享如何生成 SSH Key 并添加到极狐GitLab 中,然后用 SSH Key 进行代码拉取。

极狐GitLab 是 GitLab 在中国的发行版,可以私有化部署,对中文的支持非常友好,是专为中国程序员和企业推出的企业级一体化 DevOps 平台,一键就能安装成功。安装详情可以查看官网指南

SSH Key 对于通过 ssh 来进行极狐GitLab 代码拉取是重要的,完成整个流程一般分为三步:

  • SSH Key 的生成
  • 添加 SSH Key 到极狐GitLab 中
  • 使用 ssh 克隆代码

第一步:生成 SSH Key

使用 ssh-keygen命令来生成 SSH Key:

复制代码
ssh-keygen -t rsa -C "jh-gitlab@devops.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_jh_gitlab
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa_jh_gitlab
Your public key has been saved in /root/.ssh/id_rsa_jh_gitlab.pub
The key fingerprint is:
SHA256:OuX13mP/EASg/KljuZXrWsq65WIvzFaU9T72tXisVPc jh-gitlab@devops.com
The key's randomart image is:
+---[RSA 3072]----+
|          ...    |
|       . ..  .   |
|        oo .  .  |
|        o. ...   |
|       .S +.  ...|
|       +.+ o+ ..+|
|     oo.B +..++.E|
|      B* * o.o+= |
|     oo*Boo .o+o+|
+----[SHA256]-----+

生成之后,可以在 ~/.ssh目录下面查看:

复制代码
ls -ltr ~/.ssh/
total 24
-rw------- 1 root root  676 Apr  3 14:49 authorized_keys
-rw-r--r-- 1 root root  284 Sep  5 14:15 known_hosts
-rw-r--r-- 1 root root  574 Sep  5 14:16 id_rsa_jh_gitlab.pub
-rw------- 1 root root 2610 Sep  5 14:16 id_rsa_jh_gitlab

可以看到最下面两个是刚才生成的 SSH Key pair。pub 是公钥,另外一个是私钥。可以用 cat命令查看两个 key 的内容。

第二步:将 SSH Key 添加到极狐GitLab

点击左上角的头像 --> 偏好设置 --> SSH 密钥

点击右上角的添加新密钥:

将第一步中生成的 pub 内容拷贝到密钥方框中,然后可以自定义标题,选择到期时间,直接点击添加密钥即可完成 SSH Key 的添加。

第三步:clone 仓库代码

添加完 SSH Key 就可以使用 ssh 来 clone 代码了:

复制代码
git clone git@jhma.jihulab.net:root/kubernetes-agent.git
Cloning into 'kubernetes-agent'...
remote: Enumerating objects: 21, done.
remote: Total 21 (delta 0), reused 0 (delta 0), pack-reused 21 (from 1)
Receiving objects: 100% (21/21), 4.09 KiB | 838.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.
相关推荐
Zadig1 天前
Terraform + Zadig:打造企业级基础设施自动化流程
自动化运维·devops
CC码码1 天前
管理你的多个 Git 密钥(多平台多账号)
git·gitlab·github
CC码码1 天前
管理你的多个 Git 密钥(单平台多账号)
git·gitlab·github
DevOps在路上1 天前
从 ”以应用为中心“ 的交付看DevOps平台的演进趋势
devops
兔老大RabbitMQ1 天前
GitLab详细分析
gitlab
真实的菜1 天前
Jenkins生态与拓展:构建现代化DevOps工具链的终极指南
servlet·jenkins·devops
198911 天前
【Dify精讲】第19章:开源贡献指南
运维·人工智能·python·架构·flask·开源·devops
山猪打不过家猪12 天前
Azure Devops
flask·azure·devops
198913 天前
【Dify精讲】第14章:部署架构与DevOps实践
运维·人工智能·python·ai·架构·flask·devops