使用ssh key驗證github的好處就是不用每次提交代碼的時候都要輸入用戶名和密碼,由於着在必定程度上對效率有很大的影響,雖然這麼作能夠防止代碼提交的次數過多,但這也看我的的習慣吧。git
今天嘗試了一下配置ssh keygithub
OS:mac osshell
打開控制檯,建一個目錄,好比~/.ssh(其實叫什麼都無所謂,網上搜的基本上都這麼配置)dom
在該目錄下輸入命令:ssh
ssh-keygen -t rsa -C "youname@example.com"
注意:雙引號換成本身的郵箱,若是遇到權限問題,只需在前面加上sudoide
Generating public/private rsa key pair. Enter file in which to save the key (/var/root/.ssh/id_rsa): ./id_rsa#這裏填的是保存文件的路徑 Enter passphrase (empty for no passphrase):#直接enter跳過 Enter same passphrase again: #同上 Your identification has been saved in ./id_rsa. Your public key has been saved in ./id_rsa.pub. The key fingerprint is: 25:68:54:4a:f1:03:ab:78:15:3f:f7:9d:f0:bf:08:ee bsn.huang@gmail.com The key's randomart image is: +--[ RSA 2048]----+ | *o. | | o O | | * * o . | | . + * . + . | | . o S . + | | . . | | . .| | . . . .| | .E . . | +-----------------+
這個時候,在.ssh目錄下有兩個文件測試
id_rsa id_rsa.pub
其中id_rsa是私鑰 id_rsa.pub是公鑰spa
而後,執行下面的命令,將生成的key添加code
ssh-add id_rsa
而後將id_rsa.pub裏面的內容複製下來,在github上的settings裏面找到add keys,將其粘貼到key便可,title隨便填blog
這個時候能夠在控制檯上測試一下
$ ssh -T git@github.com The authenticity of host 'github.com (192.30.252.128)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Failed to add the host to the list of known hosts (/Users/Bsn/.ssh/known_hosts). Hi bsnwong! You've successfully authenticated, but GitHub does not provide shell access.
其餘不用管,只要看到最後一行,說明驗證成功了
後話:剛開始忘記執行ssh-add id_rsa 命令,一直驗證不成功,困在這裏很久