1. 程式人生 > >本地ssh key連線多個git賬號

本地ssh key連線多個git賬號

在開發過程中,可能需要在本地同時連線到多個git賬戶,如公司內部git和github,但是一個使用者的ssh key只能連線到一個git賬戶,這就需要建立多個ssh key,分別連線到不同的賬戶。具體步驟如下: 1.生成ssh key
ssh-keygen -t rsa -b 4096 -C "[email protected]"
1 1   1
ssh-keygen -t
rsa -b 4096 -C "[email protected]"
預設生成的key為 ~/.ssh/id_rsa,生成的兩個key名稱需要不同,這裡名稱設為id_rsa_inner和id_rsa_outer。 命令執行完後會在~/.ssh/目錄下生成id_rsa_xxx和id_rsa_xxx.pub檔案。
2.在git中新增ssh key 以github為例,點選settings —> SSH keys and GPG keys —> New SSH key,將id_rsa_outer檔案中的內容新增進來。 同理,在內部git中新增id_rsa_inner ssh key。
3.本地配置 在~/.ssh/目錄下建立config檔案,內容如下:
 Host github.com                                                                  
     HostName github.com                                                          
     PreferredAuthentications publickey                                           
     IdentityFile ~/.ssh/id_rsa_outer                                            
                                                                                 
Host git.xxx.com                                                              
     HostName git.xxx.com                                                    
     PreferredAuthentications publickey                                           
     IdentityFile ~/.ssh/id_rsa_inner   
9 9   1
 Host github.com                                                                  
2
     HostName github.com                                                          
3
     PreferredAuthentications publickey                                           
4
     IdentityFile ~/.ssh/id_rsa_outer                                            
5
                                                                                 
6
Host git.xxx.com                                                              
7
     HostName git.xxx.com                                                    
8
     PreferredAuthentications publickey                                           
9
     IdentityFile ~/.ssh/id_rsa_inner   
其中Host為別名,HostName為git的地址。即將本地的兩個ssh key與不同的git倉庫繫結起來。
4. 測試連線
ssh -T [email protected]
1 1   1
ssh -T git@github.com
如果出現以下內容,則表示連線成功。
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
  1
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
注:可以使用 ssh -vT [email protected] 開啟Debug,檢視具體的連線過程。

來自為知筆記(Wiz)