GitHub後臺能夠添加多個SSH Keys,可是同一個SSH Keys只能在添加在一個賬號上(添加時提示「Key is already in use」)。理由很容易想到,SSH公鑰使用時至關於用戶名密碼,不可能兩個不一樣的賬號使用同一個用戶名密碼。要想在多個GitHub賬號上添加公鑰,就要在本地生成多個SSH Keys,每一個GitHub賬號對應一個不一樣的SSH Keys。步驟以下:
一、生成一個新的SSH KEY
git
- larry@thk:~$ ssh-keygen -t rsa -C 'git@webmaster.me'
- 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 git@webmaster.me
- The key's randomart image is:
- +--[ RSA 2048]----+
- | o |
- | = |
- | . + o |
- | . . . + |
- | o S + |
- | B + . |
- | + .+ + |
- | .E..+ |
- | +.oo |
- +-----------------+
- larry@thk:~$ ssh-add ~/.ssh/id_rsa2
- Identity added: ~/.ssh/id_rsa2 (~/.ssh/id_rsa2)
~/.ssh/id_rsa2爲新SSH Keys文件名,根據實際狀況修改,保證每次不同便可。
二、打開新生成的~/.ssh/id_rsa2.pub文件,將裏面的內容添加到GitHub後臺。
三、打開~/.ssh/config文件(沒有則建立),添加一個Host:github
- #建一個github別名,新建的賬號使用這個別名作克隆和更新
- Host github2
- HostName github.com
- User git
- IdentityFile ~/.ssh/id_rsa2.pub
四、將GitHub SSH倉庫地址中的git@github.com替換成新建的Host別名。
如原地址是:git@github.com:freehost/mail.git,替換後應該是:github2:freehost/mail.git
若是是新建的倉庫,直接使用替換後的URL克隆便可。若是已經使用原地址克隆過了,可使用命令修改:web
- git remote set-url origin github2:freehost/mail.git