一锋爪、檢查SSH key是否存在
在終端輸入:
ls -al ~/.ssh
如果沒有屋摇,終端顯示如下:
No such file or directory
如果已經(jīng)存在,則會顯示id_rsa和id_rsa.pub
二波闹、生成新的SSH key
在終端輸入:
ssh-keygen -t rsa -C "your_email@example.com"
其中your_email@example.com為你在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 your_email@example.com
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已經(jīng)生成成功
四、添加SSH key到Github
1.復制id_rsa.pub中的所有內(nèi)容
打開id_rsa.pub力图,終端命令:
vim ~/.ssh/id_rsa.pub
手動復制以ssh-rsa到以your_email@example.com結(jié)尾的所有內(nèi)容
或者直接輸入命令復制id_rsa.pub中的所有內(nèi)容步绸,終端命令:
pbcopy < ~/.ssh/id_rsa.pub
2.登錄Github
打開個人Settings-->SSH keys-->Add SSH key
Title 隨便寫
Key 粘貼之前復制的內(nèi)容
這樣SSH key就添加的Github
五、檢測SSH key
輸入命令:
ssh git@github.com
此時會驗證SSH key是否可以訪問Gitbub
成功顯示如下:
Hi your_name! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
文/Mawb(簡書作者)
原文鏈接:http://www.reibang.com/p/21234432c94e