標籤:
GitHub後台可以添加多個SSH Keys,但是同一個SSH Keys只能在添加在一個帳號上(添加時提示“Key is already in use”)。理由很容易想到,SSH公開金鑰使用時相當於使用者名稱密碼,不可能兩個不同的帳號使用同一個使用者名稱密碼。要想在多個GitHub帳號上添加公開金鑰,就要在本地產生多個SSH Keys,每個GitHub帳號對應一個不同的SSH Keys。步驟如下:
1、產生一個新的SSH KEY
- [email protected]:~$ ssh-keygen -t rsa -C ‘[email protected]‘
- Generating public/private rsa key pair.
- Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa2 #這裡輸入一個新的ssh key檔案名稱
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in ~/.ssh/id_rsa2.
- Your public key has been saved in ~/.ssh/id_rsa2.pub.
- The key fingerprint is:
- 3a:01:17:b3:f9:26:5b:53:b3:69:be:71:a8:66:f6:96 [email protected].me
- The key‘s randomart image is:
- +--[ RSA 2048]----+
- | o |
- | = |
- | . + o |
- | . . . + |
- | o S + |
- | B + . |
- | + .+ + |
- | .E..+ |
- | +.oo |
- +-----------------+
- [email protected]:~$ ssh-add ~/.ssh/id_rsa2
- Identity added: ~/.ssh/id_rsa2 (~/.ssh/id_rsa2)
~/.ssh/id_rsa2為新SSH Keys檔案名稱,根據實際情況修改,保證每次不一樣即可。
2、開啟新產生的~/.ssh/id_rsa2.pub檔案,將裡面的內容添加到GitHub後台。
3、開啟~/.ssh/config檔案(沒有則建立),添加一個Host:
- #建一個github別名,建立的帳號使用這個別名做複製和更新
- Host github2
- HostName github.com
- User git
- IdentityFile ~/.ssh/id_rsa2.pub
4、將GitHub SSH倉庫地址中的[email protected]替換成建立的Host別名。
如原地址是:[email protected]:freehost/mail.git,替換後應該是:github2:freehost/mail.git
如果是建立的倉庫,直接使用替換後的URL複製即可。如果已經使用原地址複製過了,可以使用命令修改:
- git remote set-url origin github2:freehost/mail.git
原創文章,轉載請註明: 轉載自www.webmaster.me
本文連結地址: 在GitHub多個帳號上添加SSH公開金鑰
在GitHub多個帳號上添加SSH公開金鑰