如何生成SSH key及訪問Github
一、檢查SSH key是否存在
在終端輸入:
ls -al ~/.ssh
如果沒有,終端顯示如下:
No such file or directory
如果已經存在,則會顯示id_rsa和id_rsa.pub
二、生成新的SSH key
在終端輸入:
ssh-keygen -t rsa -C "[email protected]"
其中[email protected]為你在Github註冊時的郵箱
成功後終端顯示如下:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa):
提示你儲存.ssh/id_rsa的路徑,這裡直接enter
Created directory '/Users/xxx/.ssh'.
Enter passphrase (empty for no passphrase):
提示輸入passphrase,每次與Github通訊都會要求輸入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 [email protected]
The key's randomart image is:(後面圖形省略)
三、新增key到SSH
輸入命令:
ssh-add ~/.ssh/id_rsa
此時會要求輸入passphrase,輸入步驟二中填的passphrase
成功後,終端顯示:
Identity added: /Users/xxx/.ssh/id_rsa (/Users/xxx/.ssh/id_rsa)
最後,在/Users/xxx/.ssh/生成兩個檔案,id_rsa和id_rsa.pub
此時,SSH key已經生成成功
四、新增SSH key到Github
1.複製id_rsa.pub中的所有內容
開啟id_rsa.pub,終端命令:
vim ~/.ssh/id_rsa.pub
手動複製以ssh-rsa到以[email protected]結尾的所有內容
或者直接輸入命令複製id_rsa.pub中的所有內容,終端命令:
pbcopy < ~/.ssh/id_rsa.pub
2.登入Github
打開個人Settings-->SSH keys-->Add SSH key
Title 隨便寫
Key 貼上之前複製的內容
這樣SSH key就新增的Github
五、檢測SSH key
輸入命令:
此時會驗證SSH key是否可以訪問Gitbub
成功顯示如下:
Hi your_name! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.