1.進入ssh文件下
cd ~/ .ssh
- 生成ssh key
ssh-keygen -t rsa -C 你的油箱
- 提示輸入目錄脂倦,目錄可以不填虾宇,有默認路徑抄瑟,直接回車
// '()'里面是默認路徑
Enter file in which to save the key (/Users/user/.ssh/id_rsa):
- 提示輸入密碼, 密碼可以不填, 默認沒有密碼,直接回車
- 提示再次輸入密碼, 直接回車,
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
- 當出現(xiàn)以下圖案, 則表示生成 成功
Your identification has been saved in /Users/user/.ssh/id_rsa.
Your public key has been saved in /Users/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:IQP0sSMslL9VG+HXO9oqlnFNTkeI1/m9fBiBoKIcdxk nanananamln@163.com
The key's randomart image is:
+---[RSA 2048]----+
| .oo . E..o + . |
| ... o +o+..+ = |
| ..+ O.*o.... o.|
| o.=.*.o o.o o|
| oo S =o..o.|
| . . .oo..o.|
| +. . .|
| + . |
| . .. |
+----[SHA256]-----+
7.打開.pub文件復制生成的ssh key粘貼到Git上即可
// 路徑是上面的默認路徑
sudo vi /Users/malina/.ssh/id_rsa.pub
擴展:生成多個ssh key
1.當要生成多個ssh key時(比如github藏鹊,gitlab等),則需要在==第3步==里添加目錄以便區(qū)分
Enter file in which to save the key (/Users/user/.ssh/id_rsa):github_rsa
Enter file in which to save the key (/Users/user/.ssh/id_rsa):gitlub_rsa
// 因為已經(jīng)在.ssh文件夾里面转锈,所以前面路徑不用寫
2.添加私鑰
// 一般私鑰會自動添加
// 查看私鑰
ssh-add -l
// 刪除私鑰
ssh-add -D
// 添加私鑰
ssh-add github_rsa
ssh-add gitlub_rsa
// 因為已經(jīng)在.ssh文件夾里面盘寡,所以前面路徑不用寫,否則要寫成ssh-add ~/.ssh/gitlub_rsa
3.配置config文件
// ssh文件里面沒有config直接創(chuàng)建即可
// 創(chuàng)建/更改
vim config
// 內(nèi)容為:
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_rsa
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_rsa