如何生成SSH key及查看SSH key


只適用於Mac和windows下的Git Bash操作界面。

一、檢查本地是否有SSH Key存在

在終端輸入

ls -al ~/.ssh

如果終端輸出的是:

No such file or directory

那么就說明本地沒有SSH key

如果已存在SSH key那么就會顯示 id_rsa 和 id_rsa.pub文件的存在以及它的創建日期。

二、生成新的SSH key

首先在終端輸入

ssh-keygen -t rsa -C "your_email@example.com"

your_email@example.com 為你在 GitHub或者GitLab 注冊時的郵箱

回車后終端會顯示:

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/xxx/.ssh/id_rsa):

提示你保存 .ssh/id_rsa 的路徑是/Users/xxx/.ssh/id_rsa,直接按回車。

這里有一點,如果已經存在SSH key你想要使用以上操作重新生成的話會提示一你不是要重新生成,直接輸入y並按回車。

然后終端會提示:

Created directory '/Users/xxx/.ssh'.

Enter passphrase (empty for no passphrase):

提示設置 passphrase,每次與 Git 通信都會要求輸入 passphrase,以避免某些錯誤的操作所導致的問題,建議設置一下。

成功后終端會提示:

Your identification has been saved in /Users/xxx/.ssh/id_rsa.

Your public key has been saved in /Users/xxx/.ssh/id_rsa.pub.

The key fingerprint is:

16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48 your_email@example.com

The key's randomart image is:

心形圖形

然后在終端輸入:

ssh-add ~/.ssh/id_rsa

此時會要求輸入上面步驟里所填的 passphrase

成功后,終端顯示:

Identity added: /Users/xxx/.ssh/id_rsa (/Users/xxx/.ssh/id_rsa)

最后,在 /Users/xxx/.ssh/ 生成兩個文件,id_rsa 和 id_rsa.pub

在終端輸入:

cat /Users/xxx/.ssh/id_rsa.pub

終端就會顯示你的SSH key了,直接復制就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM