Mac 環境下如何生成Git shh key

1. 進入ssh文件下

cd ~/.ssh

2. 生成ssh key

ssh-keygen -t rsa -C 你的油箱

3. 提示輸入目錄,目錄能夠不填,有默認路徑,直接回車

# ‘()’裏面是默認路徑
Enter file in which to save the key (/Users/user/.ssh/id_rsa):

4. 提示輸入密碼, 密碼能夠不填, 默認沒有密碼,直接回車

5. 提示再次輸入密碼, 直接回車,

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

6. 當出現如下圖案, 則表示生成 成功

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](mailto: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步裏添加目錄以便區分

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

# 由於已經在.ssh文件夾裏面,因此前面路徑不用寫

2.添加私鑰

# 通常私鑰會自動添加 

# 查看私鑰 
ssh-add -l

# 刪除私鑰 
ssh-add -D

# 添加私鑰 
ssh-add github_rsa
ssh-add gitlub_rsa

# 由於已經在.ssh文件夾裏面,因此前面路徑不用寫,不然要寫成ssh-add ~/.ssh/gitlub_rsa

3.配置config文件

# ssh文件裏面沒有config直接建立便可
# 建立/更改 
vim config

# 內容爲: 
# github
Host  [github.com](http:#github.com/)  
    HostName  [github.com](http:#github.com/)  
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_rsa
# gitlab
Host  [gitlab.readtv.cn](http:#gitlab.readtv.cn/)  
    HostName  [gitlab.readtv.cn](http:#gitlab.readtv.cn/)  
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitlab_rsa

💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕git

第四步設置了密碼
那麼每次執行git pull或者git push
將會提示Enter passphrase for key '/Users/user/.ssh/id_rsa':,解決辦法以下。github

1.命令行執行:

ssh-keygen -p

2.提示以下,不用管直接回車

Enter file in which the key is(/c/Users/YourUserName/.ssh/id_rsa):

3.輸入老密碼和新密碼,輸入新密碼時直接回車便可

相關文章
相關標籤/搜索