如何将 SSH Key 添加到 GitLab 中?

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

极狐GitLab 是 GitLab 在中国的发行版,可以一键私有化部署,对于中国开发者更友好。详细安装步骤可以查看官网 https://dl.gitlab.cn/qrrj8903

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 密钥

file

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

file

将第一步中生成的 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.
posted @ 2024-09-05 15:09  极狐GitLab  阅读(5)  评论(0编辑  收藏  举报