天天看點

Git如何生成多個ssh key添加到ssh-agent管理項目

Git如何生成多個ssh key添加到ssh-agent管理項目

生成新的ssh密鑰

ssh-keygen -t rsa -b 4096 -C "[email protected]"           

不要一直Enter鍵,輸入新的名稱 id_rsa_new

Enter a file in which to save the key (/Users/you/.ssh/id_rsa):id_rsa_new           

啟動ssh-agent

$ eval "$(ssh-agent -s)"
> Agent pid 59566           

需要修改~/.ssh/config檔案以自動将密鑰加載到ssh-agent中并在密鑰鍊中存儲密碼

Mac系統如下

Host new
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_new
  User test

Host old
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa
  User test           

Win系統如下

Host new
HostName github.com
IdentityFile C:\\Users\Eric\.ssh\id_rsa_new
PreferredAuthentications publickey
User Eric

Host old
HostName github.com
IdentityFile C:\\Users\Eric\.ssh\id_rsa
PreferredAuthentications publickey
User Eric           

将SSH私鑰添加到ssh-agent并将密碼存儲在密鑰鍊中

ssh-add ~/.ssh/id_rsa_new           
ssh-add C:\\Users\Eric\.ssh\id_rsa_new           

Win系統 ssh-add 可能出現報錯

看這裡解決

回到GitHub賬号

添加ssh key看這裡

ssh -T git@new

測試連結,如下繼續連接配接 yes

> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
 > RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
 > Are you sure you want to continue connecting (yes/no)?           

> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
  > RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
  > Are you sure you want to continue connecting (yes/no)?           

如下,出現successfully表示成功

> Hi username! You've successfully authenticated, but GitHub does not
> provide shell access.           

添加倉庫位址 Host 對應config中的配置

git remote add origin [email protected]:xxxx/test.git

這樣就可以實作管理多個GitHub項目,多看官方文檔,結合官方文檔解決問題更高效

Win系統可能出現很多問題,Mac有時候更好用呢,熬了兩個晚上把Mac和Win系統問題解決分享出來,用你那高冷的方式點個贊吧

附官方文檔

Git GitHub
Git如何生成多個ssh key添加到ssh-agent管理項目