如何在极狐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 "[email protected]"
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 [email protected]
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 密钥

如何在极狐GitLab中添加 SSH Key?_第1张图片

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

如何在极狐GitLab中添加 SSH Key?_第2张图片

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

第三步:clone 仓库代码

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

git clone [email protected]: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.

你可能感兴趣的:(DevOps,DevSecOps,GitLab,极狐GitLab,安全合规)