最近學習Python網路自動化時,需要同時使用gitlab(公司的private repository)和github(public repository),先前分別在二台電腦操作沒有這樣的問題。花點時間google和了解背後的原理,其實還蠻容易理解的。
~/.ssh/config
$ 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]-----+
$ cd ~/.ssh
$ ls git*
github-test github-test.pub gitlab-test gitlab-test.pub
Gitlab: User Settings -> SSH keys -> add
Github: User Setting -> SSH and GPG keys -> New SSH Key
# 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
$ 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.
$ 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.
說穿了就是弄清楚SSH key的配置概念
Manage GitHub, and Gitlab accounts on single machine with SSH keys on Mac