iT邦幫忙

1

如何在同一台Linux主機同時使用github和gitlab

  • 分享至 

  • xImage
  •  

前言

最近學習Python網路自動化時,需要同時使用gitlab(公司的private repository)和github(public repository),先前分別在二台電腦操作沒有這樣的問題。花點時間google和了解背後的原理,其實還蠻容易理解的。

步驟

  1. 建立二組SSH Key pair
  2. 分別上傳public key到gitlab及github
  3. 編輯 ~/.ssh/config
  4. Enjoy

1.建立二組SSH Key pair

  • 首先分別建立SSH key pair
$ ssh-keygen -t ed25519 -C "test123@gitlab.com" (你的gitlab帳號)
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/pchan/.ssh/id_ed25519): gitlab-test (幫SSH key取一個易於辨識的名稱,並儲存到~/.ssh/)
Enter passphrase (empty for no passphrase): (輸入passphrase強化安全性,如果是測試可以省略)
Enter same passphrase again: (agin)
Your identification has been saved in gitlab-test
Your public key has been saved in gitlab-test.pub
The key fingerprint is:
SHA256:Eal7gCSNH20QIE5Gk04hvPpQVBamUajqVniu8seUWeA test123@gitlab.com
The key's randomart image is:
+--[ED25519 256]--+
|=*=BO=  ..       |
|==*== o ..       |
|o+o= = ..        |
|.o. E +  .       |
|o..  + oS        |
|+. o+ . .        |
|.o+o   .         |
|.o..o            |
|ooo.             |

$ ssh-keygen -t ed25519 -C "test123@github.com" (你的github帳號)
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/pchan/.ssh/id_ed25519): github-test
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in github-test
Your public key has been saved in github-test.pub
The key fingerprint is:
SHA256:jlZwPEQc3sOkWywPrVg41hSwh6ja50G4xLIOXup2szQ test123@github.com
The key's randomart image is:
+--[ED25519 256]--+
|      .+*o.      |
|     . X.B       |
|    . B % B      |
| . o . B O .     |
|. = . . S .      |
| * o   +         |
|+ oEo o .        |
|+.+=.o           |
|o=..+            |
+----[SHA256]-----+
  • 此時你的 ~/.ssh資料夾之中應該會有如下檔案。其中*.pub是你的公鑰,等會要把它們新增到網站上。gitlab-test和github-test就是你的私鑰了。請保護好你的私鑰,任何人拿到你的私鑰,就可以存取你的私人資訊。
$ cd ~/.ssh
$ ls git*
github-test  github-test.pub  gitlab-test  gitlab-test.pub

2.上傳公鑰到gitlab和github

  • Gitlab: User Settings -> SSH keys -> add
    https://ithelp.ithome.com.tw/upload/images/20221227/2015248815I8HrvjlE.jpg

  • Github: User Setting -> SSH and GPG keys -> New SSH Key
    https://ithelp.ithome.com.tw/upload/images/20221227/201524881C5YU6yIyj.jpg

3.編輯 ~/.ssh/config

# My personal gitlab account
Host gitlab.com
   HostName gitlab.com
   User test123
   IdentityFile ~/.ssh/gitlab-test 
   
# My personal github account
Host github.com
   HostName github.com
   User test123
   IdentityFile ~/.ssh/github-test 

4.測試

  • github
$ git clone git@github.com:protonpchan/git-fork-test.git
Cloning into 'git-fork-test'...
remote: Enumerating objects: 43, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 43 (delta 9), reused 6 (delta 4), pack-reused 31
Receiving objects: 100% (43/43), 5.61 KiB | 820.00 KiB/s, done.
Resolving deltas: 100% (14/14), done.
  • gitlab
$ git clone git@gitlab.xxx.xx:123/tools.git
Cloning into 'tools'...
remote: Enumerating objects: 2593, done.
remote: Counting objects: 100% (77/77), done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 2593 (delta 35), reused 38 (delta 17), pack-reused 2516
Receiving objects: 100% (2593/2593), 14.59 MiB | 771.00 KiB/s, done.
Resolving deltas: 100% (998/998), done.

5.結論

說穿了就是弄清楚SSH key的配置概念

6.參考文件

Manage GitHub, and Gitlab accounts on single machine with SSH keys on Mac


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言