使用環(huán)境:關(guān)于同一臺電腦MAC系統(tǒng)下使用多個(gè)SSH key 切換使用(或者多用戶使用ssh提交代碼)
要求,可以創(chuàng)建不同的 PUBLIC KEY ,根據(jù)下面步驟設(shè)置(建議進(jìn)入~/.ssh 目錄進(jìn)行備份.ssh文件夾簸喂,避免原有密鑰被錯誤覆蓋).若已經(jīng)生成密鑰并已經(jīng)添加了SSH KEY到對應(yīng)的平臺著蟹,可以直接從3開始看。都是在終端中操作秽褒。
(1)生成不同的ssh密鑰
LPT42147:~ rason$ cd ~/.ssh
LPT42147:.ssh rason$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/rason/.ssh/id_rsa): id_rsa_aaa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_aaa.
Your public key has been saved in id_rsa_aaa.pub.
The key fingerprint is:
SHA256:wUoh6g49FPEFBRg8fnR6z8tFsVdezovmh4c/iBa0nPo rason@LPT42147.local
The key's randomart image is:
+---[RSA 2048]----+
| .+++++ |
| ++.o.o . . .|
| .oo.o. o o o + |
| +. o... .o.. . o|
|. +. ..oS.o.o . .|
| o . o .= o . |
| . . o. = + |
| o. o = + |
| oE +..|
+----[SHA256]-----+
LPT42147:.ssh rason$
我們在 ~/.ssh目錄下創(chuàng)建 id_rsa_aaa 私鑰 和 id_rsa_aaa.pub 公鑰
注意這里
Enter file in which to save the key (/Users/rason/.ssh/id_rsa): #設(shè)置文件名,如果不設(shè)置默認(rèn)生成 id_rsa 和 id_rsa.pub
(2)按照上面的步驟生成了 id_rsa_aaa 對應(yīng)的公鑰和私鑰 ........
(3)查看系統(tǒng)ssh-key代理,執(zhí)行如下命令
ssh-add -l
Could not open a connection to your authentication agent.
如果發(fā)現(xiàn)上面的提示,說明系統(tǒng)代理里沒有任何key,執(zhí)行如下操作
exec ssh-agent bash
如果系統(tǒng)已經(jīng)有ssh-key 代理 ,執(zhí)行下面的命令可以刪除,當(dāng)然如果沒影響就不要亂刪除了。
ssh-add -D
(4)把 .ssh 目錄下的1個(gè)私鑰添加的 ssh-agent
ssh-add -K ~/.ssh/id_rsa_aaa
(5)打開github 或者 開源中國 ssh 管理頁面等把 對應(yīng)的公鑰提交保存到代碼管理服務(wù)器 (.pub 結(jié)尾)
查看ssh key(功能)
cat id_rsa_aaa.pub
(6)在 .ssh 目錄創(chuàng)建 config 配置文件
vim ~/.ssh/config
輸入如下配置信息
Host git.dev.sh.ctripcorp.com
User git
HostName git.dev.sh.ctripcorp.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/zhushou/id_rsa