在GitHub多個賬號上添加SSH公鑰

GitHub後臺能夠添加多個SSH Keys,可是同一個SSH Keys只能在添加在一個賬號上(添加時提示「Key is already in use」)。理由很容易想到,SSH公鑰使用時至關於用戶名密碼,不可能兩個不一樣的賬號使用同一個用戶名密碼。要想在多個GitHub賬號上添加公鑰,就要在本地生成多個SSH Keys,每一個GitHub賬號對應一個不一樣的SSH Keys。步驟以下:
一、生成一個新的SSH KEY
git

  1. larry@thk:~$ ssh-keygen -t rsa -C 'git@webmaster.me'
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa2 #這裏輸入一個新的ssh key文件名
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in ~/.ssh/id_rsa2.
  7. Your public key has been saved in ~/.ssh/id_rsa2.pub.
  8. The key fingerprint is:
  9. 3a:01:17:b3:f9:26:5b:53:b3:69:be:71:a8:66:f6:96 git@webmaster.me
  10. The key's randomart image is:
  11. +--[ RSA 2048]----+
  12. | o |
  13. | = |
  14. | . + o |
  15. | . . . + |
  16. | o S + |
  17. | B + . |
  18. | + .+ + |
  19. | .E..+ |
  20. | +.oo |
  21. +-----------------+
  22. larry@thk:~$ ssh-add ~/.ssh/id_rsa2
  23. Identity added: ~/.ssh/id_rsa2 (~/.ssh/id_rsa2)

~/.ssh/id_rsa2爲新SSH Keys文件名,根據實際狀況修改,保證每次不同便可。
二、打開新生成的~/.ssh/id_rsa2.pub文件,將裏面的內容添加到GitHub後臺。
三、打開~/.ssh/config文件(沒有則建立),添加一個Host:github

  1. #建一個github別名,新建的賬號使用這個別名作克隆和更新
  2. Host github2
  3. HostName github.com
  4. User git
  5. IdentityFile ~/.ssh/id_rsa2.pub

四、將GitHub SSH倉庫地址中的git@github.com替換成新建的Host別名。
如原地址是:git@github.com:freehost/mail.git,替換後應該是:github2:freehost/mail.git
若是是新建的倉庫,直接使用替換後的URL克隆便可。若是已經使用原地址克隆過了,可使用命令修改:web

  1. git remote set-url origin github2:freehost/mail.git
相關文章
相關標籤/搜索