[教學] 如何在同一台電腦上管理多個 GitHub 帳號的 SSH Key

2025-06-21 12:48:50 Category Icon 未分類

[教學] 如何在同一台電腦上管理多個 GitHub 帳號的 SSH Key

在軟體開發中,我們可能同時擁有多個身份:公司的開發者、個人專案的維護者、或是自由接案的工程師。這通常意味著我們需要管理多個不同的 GitHub 帳號。然而,在同一台電腦上為不同帳號設定 SSH Key 常常讓人頭痛。

這篇文章將引導你完成設定,讓你能夠輕鬆地在同一台機器上,無縫切換並使用多個 GitHub 帳號。

為什麼需要這樣做呢?

當你試圖用同一個 SSH Key 綁定到多個 GitHub 帳號時,GitHub 會提示「Key is already in use」。這是因為一個公鑰在 GitHub 全域範圍內是唯一的。為了解決這個問題,我們需要為每個帳號生成獨立的 SSH Key,並透過 SSH 設定檔來告訴系統在什麼情況下該使用哪一把鑰匙。

本文教學環境

  • 作業系統: Windows (使用 Git Bash) 或 macOS / Linux (使用內建終端機)
  • 工具: Git Bash (或任何 Unix-like shell), 文字編輯器, SourceTree (可選)

步驟一:為不同帳號生成專屬的 SSH Key

首先,我們要為每一個 GitHub 帳號生成一對獨一無二的 SSH Key (公鑰與私鑰)。我們使用 ssh-keygen 指令,並透過 -f 參數指定金鑰的儲存路徑與檔名,以避免覆蓋預設的 id_ed25519

打開你的 Git Bash 或終端機,執行以下指令:

# 為第一個帳號 (account1) 生成金鑰
ssh-keygen -t ed25519 -C "account1@example.com" -f ~/.ssh/id_ed25519_account1.rsa

# 為第二個帳號 (account2) 生成金鑰
ssh-keygen -t ed25519 -C "account2@example.com" -f ~/.ssh/id_ed25519_account2.rsa

# 為第三個帳號 (account3) 生成金鑰
ssh-keygen -t ed25519 -C "account3@example.com" -f ~/.ssh/id_ed25519_account3.rsa

指令說明:

  • -t ed25519: 指定使用 ed25519 加密演算法,這是目前推薦的演算法,比 RSA 更安全、更快速。
  • -C "your_email@example.com": 增加註解,通常會放對應的 Email,方便識別。
  • -f ~/.ssh/filename: 指定金鑰檔案的名稱與路徑。建議使用有意義的名稱,例如加上專案或帳號名。

執行後,~/.ssh 資料夾內會產生對應的私鑰 (如 id_ed25519_account1.rsa) 和公鑰 (如 id_ed25519_account1.pub)。


步驟二:設定 SSH Config 檔案

接下來,我們要建立一個設定檔,告訴 SSH 用戶端在連接到特定主機時,應該使用哪一個私鑰。

  1. 開啟或建立 ~/.ssh/config 檔案:

    nano ~/.ssh/config
  2. 將以下內容貼入檔案中,並根據你的設定進行修改:

    # ~/.ssh/config
    
    # 第一個帳號 (account1)
    Host id_ed25519_account1
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_account1.rsa
    	IdentitiesOnly yes
    
    # 第二個帳號 (account2)
    Host id_ed25519_account2
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_account2.rsa
    	IdentitiesOnly yes
        
    # 第三個帳號 (account3)
    Host id_ed25519_account3
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_account3.rsa
    	IdentitiesOnly yes

設定檔說明:

  • Host: 這是一個自訂的別名,我們在 clone 或 push 時會用到它。它將代替 github.com。建議使用 github-帳號名 這種好懂的格式。
  • HostName: 實際要連線的主機名稱,這裡固定是 github.com
  • User: 連線的使用者,固定是 git
  • IdentityFile: 指定這個別名要使用的私鑰路徑。

步驟三:將公鑰新增至 GitHub

現在,我們需要將每個帳號對應的 公鑰 (.pub) 內容加到各自的 GitHub 帳號中。

  1. 複製公鑰內容。例如,複製 account1 的公鑰:

    cat ~/.ssh/id_ed25519_account1.pub

    將終端機輸出的整段 ssh-ed25519 ... 內容複製起來。

  2. 登入對應的 GitHub 帳號 (例如 account1 的帳號)。

  3. 前往 Settings > SSH and GPG keys > 點擊 New SSH key

  4. Title 欄位填入一個好辨識的名稱 (例如:My MacBook Pro),然後將剛剛複製的公鑰內容貼到 Key 欄位中。

  5. 點擊 Add SSH key

對每一個帳號重複以上操作,將各自的公鑰新增到對應的 GitHub 帳號設定中。


步驟四 (可選):設定 ssh-agent 自動載入金鑰

為了避免每次操作 Git 時都要輸入金鑰的密碼 (如果你有設定的話),我們可以讓 ssh-agent 在背景執行並自動載入所有金鑰。

  1. 編輯你的 shell 設定檔 (例如 ~/.bashrc~/.zshrc):

    nano ~/.bashrc
  2. 在檔案末尾加入以下內容:

    # 啟動 ssh-agent
    eval `ssh-agent -s` 
    
    # 將所有我們建立的私鑰加入 agent
    ssh-add ~/.ssh/id_ed25519_account1.rsa
    ssh-add ~/.ssh/id_ed25519_account2.rsa
    ssh-add ~/.ssh/id_ed25519_account3.rsa
  3. 儲存檔案後,重啟你的終端機,或執行 source ~/.bashrc 使設定生效。


步驟五:如何使用

設定完成後,我們在 git clone 或設定遠端儲存庫時,需要稍微修改一下 URL。

使用 Command Line

原本的 clone URL 長這樣: git@github.com:account1/my-project.git

你需要將 URL 中的 github.com 替換成你在 ~/.ssh/config 中設定的 Host 別名。

修改後: git@id_ed25519_account1:account1/my-project.git

這樣一來,SSH 就會知道要使用 id_ed25519_account1 這把鑰匙來進行身份驗證。

使用 SourceTree

在 SourceTree 中 clone 專案時,同樣需要修改來源路徑 URL。

  1. 在 “來源路徑 / URL” 欄位中,貼上修改後的 URL: git@id_ed25519_account1:account1/my-project.git

  2. 疑難排解: 如果 SourceTree 無法順利連接,可以手動指定 SSH Client。

    • 前往 Tools -> Options -> General 頁籤。
    • SSH Client 設定為 OpenSSH
    • (通常不需要) 如果還是有問題,可以在 SSH Key 欄位手動選擇要使用的 私鑰 檔案 (例如 ~/.ssh/id_ed25519_account1)。

步驟六:設定每個 user 的 name 及 email

  1. 在 .gitconfig 檔案中加入以下設定:
# --- 條件式載入規則 ---
[includeIf "gitdir/i:D:/Github/account1/**"]
    path = .gitconfig-account1
	
[includeIf "gitdir/i:D:/Github/account2/**"]
    path = .gitconfig-account2

[includeIf "gitdir/i:D:/Github/account3/**"]
    path = .gitconfig-account3
  1. 在 .gitconfig 相同的資料夾中,依序加入 .gitconfig-account1 .gitconfig-account2 .gitconfig-account3 以及各自的設定值:
[user]
    name = account1
    email = account1@example.com

總結

透過為每個 GitHub 帳號建立專屬的 SSH Key 並搭配 ~/.ssh/config 設定檔,我們成功地實現了在單一電腦上管理多個帳號的需求。現在,你可以流暢地在不同專案之間切換,而不用擔心身份驗證混淆的問題了。