1. 程式人生 > 其它 >同一臺電腦,github、阿里雲code git ssh key 登入

同一臺電腦,github、阿里雲code git ssh key 登入

Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
 /c/Users/Administrator/.ssh/id_rsa_user2

問題:如果是多使用者如user1,user2,在同一臺伺服器上,那麼id_rsa_pub就不能用在user2的身上了,怎麼辦呢?

一、新建user1、user2的SSH Key

#新建user1 SSH key:

$ cd ~/.ssh    
ssh-keygen -t rsa -C "[email protected]"

一路回車即可;

# 新建user2的SSH key

#ssh-keygen -t rsa -C "[email protected]"

# 設定名稱為

Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
 /c/Users/Administrator/.ssh/id_rsa_al

id_rsa_al,這裡起了一個新的檔名,ssh目錄下已經有預設的id_rsa檔案。

二、金鑰新增到SSH agent中

因為預設只讀取id_rsa,為了讓SSH識別新的私鑰,需將其新增到SSH agent中:

ssh-add  ~/.ssh/id_rsa_al

如果出現Could not open a connection to your authentication agent的錯誤,就試著用以下命令:

 ssh-agent  bash 
 ssh-add  ~/.ssh/id_rsa_user2 

三、修改config檔案

在~/.ssh目錄下找到config檔案,如果沒有就建立:

touch config

然後修改我的config配置如下:

Host code.aliyun.com
       HostName code.aliyun.com
       PreferredAuthentications publickey 
       IdentityFile ~/.ssh/id_rsa_al
Host github.com
       HostName github.com 
       PreferredAuthentications publickey 
       IdentityFile 
~/.ssh/id_rsa

其規則就是:從上至下讀取config的內容,在每個Host下尋找對應的私鑰。

四、

開啟新生成的~/.ssh/id_rsa_al.pub檔案,將裡面的內容新增到GitHub後臺。(第二個新賬號)

第一個github帳號下的SSH Key中也要記得新增。(新增到第一個賬號)