CentOS7上GitHub/GitLab多帳號管理SSH Key


由於公司團隊使用 GitLab 來托管代碼,同時,個人在 Github 上還有一些代碼倉庫,可公司郵箱與個人郵箱是不同的,由此產生的 SSH key 也是不同的,這就造成了沖突 ,文章提供此類問題的解決方案:如何在一台機器上面同時使用 Github 與 Gitlab 的服務?

由於公司不允許訪問外網22端口,所以我們可以改走443端口

Using SSH over the HTTPS port

https://help.github.com/articles/using-ssh-over-the-https-port/

測試https端口連通性

# ssh -T -p 443 git@ssh.github.com

修改ssh配置文件

# vi ~/.ssh/config

Host github.com
  Hostname ssh.github.com
  Port 443

然后通過以下方式測試

# ssh -T git@github.com

1. 生成GitHub/GitLab 的 SSH Key

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "285006386@qq.com"
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitlab -C "admin@example.com"

檢查key是否生成

$ ls ~/.ssh

2. 添加private key

$ ssh-add ~/.ssh/id_rsa_github
$ ssh-add ~/.ssh/id_rsa_gitlab

如果執行ssh-add時提示"Could not open a connection to your authentication agent",可以先執行命令:

$ ssh-agent bash

然后再運行ssh-add命令。

# 可以通過 ssh-add -l 來確私鑰列表
$ ssh-add -l

# 可以通過 ssh-add -D 來清空私鑰列表
$ ssh-add -D

3. 修改配置文件

$ vi ~/.ssh/config

# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github

# gitlab
Host gitlab.example.com
    HostName gitlab.example.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlab

4. 上傳public key 到 GitHub/GitLab

GitHub設置過程如下:

登錄github,點擊右上方的圖標,點擊“Settings”

選擇“SSH and GPG keys”,點擊“New SSH key”,在出現的界面中填寫SSH key的名稱,填一個你自己喜歡的名稱即可,然后將上面拷貝的 ~/.ssh/id_isa_github.pub 文件內容粘帖到 key 一欄,在點擊“Add SSH key”按鈕就可以了。

添加過程github會提示你輸入一次你的github密碼 ,確認后即添加完畢。

GitLab設置過程如下:

點擊“Profile Settings”

點擊“SSH Keys”,添加SSH key

5. 測試

# 測試github
$ ssh -T git@github.com

# 測試gitlab
$ ssh -T git@gitlab.example.com

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM