背景:
在使用 git 進行代碼管理的時候,首先需要配置 SSH key ,以及添加用戶,可以是 github 用戶、gitLab 用戶、coding 用戶。所以會遇到需要在一臺電腦上配置不同 git 賬戶的 SSH key,下面就說說具體如何配置。
需求:
同時配置 github 和 coding 的 SSH key。
1. 生成 SSH 公鑰
ssh-keygen -m PEM -t ed25519 -C "your.email@example.com" // 創(chuàng)建新的 SSH 私鑰與公鑰秘鑰對,輸入你的郵箱作為標(biāo)簽
Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] // 推薦使用默認(rèn)地址
Enter passphrase (empty for no passphrase): // 此處直接回車即可;若設(shè)置密碼,則每次使用 SSH 方式推送代碼時都會要求輸入密碼
若你需要使用多個 SSH 密鑰對,在 Enter file in which to save the key 步驟時,輸入一個新的文件名稱。

sshkey.png
到此為止在我的
.ssh文件夾下面會有兩個 SSH key,id_ed25519 是對應(yīng)的 github 的 SSH key,id_ed25519_coding 是 coding 的 SSH key。我們需要將 id_ed25519.pub 添加到 github 賬戶的公鑰管理中即可,id_ed25519_coding.pub 添加到 coding 賬戶的公鑰管理中。

ssh.png
2. 公鑰管理
添加配置文件
vim ~/.ssh/config
文件內(nèi)容
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
# coding
Host e.coding.net
HostName e.coding.net
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_coding
Host 為站點,HostName 為站點名,PreferredAuthentications 為優(yōu)先使用公鑰連接,IdentityFile 是私鑰路徑
3. 賬戶驗證
ssh -T git@github.com
ssh -T git@e.coding.net
得到如下顯示即為成功

T.png