GitLab – SSH Key

Loading

CentOS 8 ( 虛擬機 )

第一次進到 GitLab 網頁管理介面會看到以下訊息

You won't be able to pull or push project code via SSH until you add an SSH key to your profile
在您個人資料中加入SSH金鑰之前,您無法通過SSH來拉取或推送版本庫。

先來說一下 Gitlab 主要是透過 SSH 與 HTTP ( HTTPS ) 的方式上傳下載.

  • HTTP ( HTTPS )
    預設我們只能透過 http 或是 https 來將 GitLab 檔案上傳與下載.

    PS C:\Users\Ben\test> git clone http://192.168.31.167/root/runner_test.git
    Cloning into 'runner_test'...
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (3/3), done.
    

    看權限需帳號與密碼.

  • SSH
    以下範例是在尚未匯入 SSH Key 的錯誤訊息.

    [root@localhost ~]# git clone git@192.168.31.167:ben/runner_test.git
    Cloning into 'runner_test'...
    The authenticity of host '192.168.31.140 (192.168.31.140)' can't be established.
    ECDSA key fingerprint is SHA256:mu3vJ57wIW2fHfX6QsXvLuIlMUalUkfRIlpOKRtB2fo.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    Warning: Permanently added '192.168.31.140' (ECDSA) to the list of known hosts.
    git@192.168.31.140's password:
    Permission denied, please try again.
    git@192.168.31.140's password:
    Permission denied, please try again.
    git@192.168.31.140's password:
    git@192.168.31.140: Permission denied (publickey,password).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    
    PS C:\Users\Ben\test1> git clone git@192.168.31.167:ben/runner_test.git
    Cloning into 'runner_test'...
    git@192.168.31.167's password:
    Permission denied, please try again.
    git@192.168.31.167's password:
    Permission denied, please try again.
    git@192.168.31.167's password:
    git@192.168.31.167: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

我們只需要把使用者端產生的 SSH 的 公鑰 放到 GitLab 網頁並儲存起來即可,關於 SSH 公/私金鑰 請參考 – https://benjr.tw/98344

透過 ssh-keygen 來產生 SSH 公私鑰 ,這邊不使用 passhrase (需要一串密碼來解開私鑰) , -C 後面名稱可以使用 E-Mail 或是姓名皆可.

以下指令在 Windows , Mac 和 Linux 環境皆相同.

[root@localhost ~]# ssh-keygen -t rsa -C ben@gmail.com
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jbZqlgzXm8VHXyGzUkjmn/huqmxm6XzB4wAcSB6yg2k ben@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
|   ..o.   .o.    |
|  o +...  o. + . |
| E o .. .  .. + .|
|.   .  o o .+.. .|
|       .S.oo.+ . |
|    . ...oo+o .  |
|     + ..++.o.   |
|      =.== o..   |
|     o. ==o.o.   |
+----[SHA256]-----+

SSH 產生的 公/私金鑰 皆儲存在家目錄的 .ssh 目錄裡面.

[root@localhost ~]# ll .ssh/
id_rsa       id_rsa.pub   known_hosts

id_rsa.pub 就是公鑰,內容要複製到 GitLab 網頁並儲存起來.

[root@localhost ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3zQ9kR8qsNLyTHF63XUP6VvRTmQLik8yLEH21LnEUfegJIka9qiJJ+vrh4hkT1zNIb1s7Ax7YaRGPA2BhNtXo9YIUAcvjaX5pZGMwQZt8DRGjvYV6/0= ben@gmail.com

我這邊使用 ben 身分登入 GitLab 網頁,並點選 User Settings / SSH Keys .

把剛剛的 id_rsa.pub 內容貼到網頁的 Key 裡面 ,並點選 Add Key 儲存起來.

這樣在使用者端就不再需要帳號密碼才能下載檔案了.

[root@localhost ~]# git clone git@192.168.31.140:ben/runner_test.git
Cloning into 'runner_test'...
remote: Enumerating objects: 7, done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 7
Receiving objects: 100% (7/7), done.
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料