SSH key生成
ssh key生成
ssh-keygen -t rsa -C "xxx@xxx.com"
此時(shí)會(huì)提示保存key的名字及路徑,我們只需要自定義key的名字(路徑)即可。
Enter file in which to save the key (/Users/caichenghan/.ssh/id_rsa):
填寫自定義路徑
/Users/caichenghan/.ssh/id_rsa_github
輸入密碼生成 ssh key ,可直接按Enter進(jìn)入生成。
查看生成的ssh key
cd /Users/caichenghan/.ssh
ls
目錄下會(huì)多出2個(gè)文件,分別為 id_rsa_github 、 id_rsa_github.pub ,前者為私鑰,后者為公鑰,我們只需要把公鑰上傳到github賬戶的公鑰管理中即可。
我們還可以使用Finder查看該路徑:Finder->右鍵->前往文件夾-> ~/.ssh
查看公鑰
cat ~/.ssh/id_rsa_github.pub
可查看名為 id_rsa_github.pub 的公鑰,將其復(fù)制下來。
公鑰管理
打開Github或者自己的git管理頁面,在個(gè)人中心找到公鑰管理,添加制作的公鑰,保存。
ssh key 配置文件制作
配置文件制作
vim ~/.ssh/config
配置 github 的ssh key
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# oschina
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
# 其它
...
其中 Host 為站點(diǎn); HostName 為站點(diǎn)名; PreferredAuthentications 為首選認(rèn)證方法; IdentityFile ,私鑰路徑。
如果站點(diǎn)為 ip地址 ,則用 ip地址 代替 域名 即可。
賬戶驗(yàn)證
ssh -T git@github.com
格式為git@域名
Hi xxx! You've successfully ...
配置成功
如果有如下提示
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?
鍵入yes即可。
如果仍舊報(bào)錯(cuò),可刪除 ~/.ssh 目錄下的 known_host 文件里對(duì)應(yīng)的站點(diǎn)公鑰信息,然后重新賬戶驗(yàn)證。
同理,我們還可以繼續(xù)添加其它賬號(hào)的 ssh key ,這樣就可以同一臺(tái)電腦,使用多個(gè) ssh key 免密碼的git操作了。