一,配置公祕鑰
1,基本操作
以碼云爲例,找到碼雲個人賬戶右上角的設置->SSH公匙
打開右側怎樣生成公鑰的鏈接,按照命令生成,查看,粘貼到SSH公匙區(確定),測試。
http://git.mydoc.io/?t=154712
- 生成 sshkey:
ssh-keygen -t rsa -C "[email protected]"
# Generating public/private rsa key pair...
# 三次回車即可生成 ssh key
- 查看你的 public key,並把他添加到碼雲(Gitee.com) SSH key添加地址:https://gitee.com/profile/sshkeys)
cat ~/.ssh/id_rsa.pub
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....
- 添加後,在終端(Terminal)中輸入
ssh -T git@gitee.com
若返回
Welcome to Gitee.com, yourname!
則證明添加成功。
2,爲 git 添加多個公祕鑰
由於在其他平臺設置過git公祕鑰,如果按照基本操作,會提示你是否要覆蓋id_rsa,所以你需要建一個新名字的公鑰
(1)生成新的 rsa key
在命令行輸入如下命令,將命令中的 [email protected]
改爲你的 Email
地址,gitchina_rsa
爲新公匙名字
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/gitchina_rsa
生成過程中會出現以下信息,按屏幕提示操作輸入 passphrase 口令,也可以直接回車設爲空。
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/jiesong/.ssh/gitchina_rsa.
Your public key has been saved in /Users/jiesong/.ssh/gitchina_rsa.pub.
The key fingerprint is:
SHA256:r0bNnxUn4mC07fp9NapzGu0C4Ao0fjAKzxsjYnKRhYU [email protected]
此時在 ~/.ssh/
目錄下會生成 gitchina_rsa
和 gitchina_rsa.pub
兩個文件。
(2)配置新的 rsa key
在 SSH 用戶配置文件 ~/.ssh/config
中指定對應服務所使用的公祕鑰名稱,如果沒有 config
文件的話就新建一個vi config
,並輸入以下內容(保存:ESC+:wq保存退出):
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/gitchina_rsa
再進行查看,粘貼到SSH公匙區,測試。此時再運行ssh -T [email protected]
時,系統就會根據config文件找到gitee.com對應的公匙了。
3,參考自
https://www.jianshu.com/p/4f76371dfa5f
https://www.zhihu.com/question/21402411